1
0
Fork 0

personal/q3k: add minecraft plugins

Also drive-by modify WORKSPACE to add required deps.

Also drive-by update deps in WORKSPACE.

Also drive-by remove old stackb/proto library from WORKSPACE (only used
in cccampix, which is dead, and stackb/proto should be replaceable by
the main grpc lib by this point).

Change-Id: I7ac7fe2237e859dc1c45bf41a016174ed8e9ee71
master
q3k 2020-05-18 00:27:23 +02:00 committed by q3k
parent 12ab93ac7e
commit 32f8a58236
11 changed files with 485 additions and 77 deletions

165
WORKSPACE
View File

@ -16,6 +16,19 @@ http_archive(
sha256 = "2ef429f5d7ce7111263289644d233707dba35e39696377ebab8b0bc701f7818e",
)
# zlib
http_archive(
name = "zlib",
build_file = "@com_google_protobuf//:third_party/zlib.BUILD",
sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1",
strip_prefix = "zlib-1.2.11",
urls = [
"https://mirror.bazel.build/zlib.net/zlib-1.2.11.tar.gz",
"https://zlib.net/zlib-1.2.11.tar.gz",
],
)
# subpar
git_repository(
@ -29,9 +42,9 @@ git_repository(
http_archive(
name = "io_bazel_rules_docker",
sha256 = "87fc6a2b128147a0a3039a2fd0b53cc1f2ed5adb8716f50756544a572999ae9a",
strip_prefix = "rules_docker-0.8.1",
urls = ["https://github.com/bazelbuild/rules_docker/archive/v0.8.1.tar.gz"],
sha256 = "dc97fccceacd4c6be14e800b2a00693d5e8d07f69ee187babfd04a80a9f8e250",
strip_prefix = "rules_docker-0.14.1",
urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.14.1/rules_docker-v0.14.1.tar.gz"],
)
load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl", docker_toolchain_configure = "toolchain_configure")
@ -97,22 +110,6 @@ load("@pydeps//:requirements.bzl", "pip_install")
pip_install()
# stackb/rules_proto (for Python proto compilation)
http_archive(
name = "build_stack_rules_proto",
urls = ["https://github.com/stackb/rules_proto/archive/b93b544f851fdcd3fc5c3d47aee3b7ca158a8841.tar.gz"],
sha256 = "c62f0b442e82a6152fcd5b1c0b7c4028233a9e314078952b6b04253421d56d61",
strip_prefix = "rules_proto-b93b544f851fdcd3fc5c3d47aee3b7ca158a8841",
)
load("@build_stack_rules_proto//python:deps.bzl", "python_grpc_compile")
python_grpc_compile()
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()
# Docker base images
load("@io_bazel_rules_docker//container:container.bzl", "container_pull")
@ -180,22 +177,24 @@ http_file(
# Go rules
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
sha256 = "6a68e269802911fa419abb940c850734086869d7fe9bc8e12aaf60a09641c818",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.22.2/rules_go-v0.22.2.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/v0.22.2/rules_go-v0.22.2.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.0/rules_go-v0.23.0.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/v0.23.0/rules_go-v0.23.0.tar.gz",
],
sha256 = "142dd33e38b563605f0d20e89d9ef9eda0fc3cb539a14be1bdb1350de2eda659",
)
http_archive(
name = "bazel_gazelle",
sha256 = "bfd86b3cbe855d6c16c6fce60d76bd51f5c8dbc9cfcaef7a2bb5c1aafd0710e8",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/bazel-gazelle/releases/download/v0.20.0/bazel-gazelle-v0.20.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.20.0/bazel-gazelle-v0.20.0.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.0/bazel-gazelle-v0.21.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.0/bazel-gazelle-v0.21.0.tar.gz",
],
sha256 = "d8c45ee70ec39a57e7a05e5027c32b1576cc7f16d9dd37135b0eddde45cf1b10",
)
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
@ -206,14 +205,13 @@ go_repository(
importpath = "golang.org/x/net",
)
# Invoke go_rules_dependencies depending on host platform.
load("//tools:go_sdk.bzl", "gen_imports")
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
gen_imports(name = "go_sdk_imports")
go_rules_dependencies()
load("@go_sdk_imports//:imports.bzl", "load_go_sdk")
go_register_toolchains()
load_go_sdk()
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
gazelle_dependencies()
@ -232,63 +230,63 @@ load(
gerrit_api()
load("@com_googlesource_gerrit_bazlets//tools:maven_jar.bzl", "maven_jar", "GERRIT")
load("@com_googlesource_gerrit_bazlets//tools:maven_jar.bzl", gerrit_maven_jar="maven_jar", GERRIT="GERRIT")
PROLOG_VERS = "1.4.3"
JACKSON_VER = "2.9.7"
maven_jar(
gerrit_maven_jar(
name = "scribe",
artifact = "org.scribe:scribe:1.3.7",
sha1 = "583921bed46635d9f529ef5f14f7c9e83367bc6e",
)
maven_jar(
gerrit_maven_jar(
name = "commons-codec",
artifact = "commons-codec:commons-codec:1.4",
sha1 = "4216af16d38465bbab0f3dff8efa14204f7a399a",
)
maven_jar(
gerrit_maven_jar(
name = "jackson-core",
artifact = "com.fasterxml.jackson.core:jackson-core:" + JACKSON_VER,
sha1 = "4b7f0e0dc527fab032e9800ed231080fdc3ac015",
)
maven_jar(
gerrit_maven_jar(
name = "jackson-databind",
artifact = "com.fasterxml.jackson.core:jackson-databind:" + JACKSON_VER,
sha1 = "e6faad47abd3179666e89068485a1b88a195ceb7",
)
maven_jar(
gerrit_maven_jar(
name = "jackson-annotations",
artifact = "com.fasterxml.jackson.core:jackson-annotations:" + JACKSON_VER,
sha1 = "4b838e5c4fc17ac02f3293e9a558bb781a51c46d",
)
maven_jar(
gerrit_maven_jar(
name = "jackson-dataformat-yaml",
artifact = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:" + JACKSON_VER,
sha1 = "a428edc4bb34a2da98a50eb759c26941d4e85960",
)
maven_jar(
gerrit_maven_jar(
name = "snakeyaml",
artifact = "org.yaml:snakeyaml:1.23",
sha1 = "ec62d74fe50689c28c0ff5b35d3aebcaa8b5be68",
)
maven_jar(
gerrit_maven_jar(
name = "prolog-runtime",
artifact = "com.googlecode.prolog-cafe:prolog-runtime:" + PROLOG_VERS,
attach_source = False,
repository = GERRIT,
sha1 = "d5206556cbc76ffeab21313ffc47b586a1efbcbb",
)
maven_jar(
gerrit_maven_jar(
name = "prolog-compiler",
artifact = "com.googlecode.prolog-cafe:prolog-compiler:" + PROLOG_VERS,
attach_source = False,
repository = GERRIT,
sha1 = "f37032cf1dec3e064427745bc59da5a12757a3b2",
)
maven_jar(
gerrit_maven_jar(
name = "prolog-io",
artifact = "com.googlecode.prolog-cafe:prolog-io:" + PROLOG_VERS,
attach_source = False,
@ -296,6 +294,55 @@ maven_jar(
sha1 = "d02b2640b26f64036b6ba2b45e4acc79281cea17",
)
# minecraft spigot/bukkit deps
# this uses rules_jvm_external vs gerrit's maven_jar because we need SNAPSHOT support
http_archive(
name = "io_grpc_grpc_java",
sha256 = "446ad7a2e85bbd05406dbf95232c7c49ed90de83b3b60cb2048b0c4c9f254d29",
strip_prefix = "grpc-java-1.29.0",
url = "https://github.com/grpc/grpc-java/archive/v1.29.0.zip",
)
RULES_JVM_EXTERNAL_TAG = "3.0"
RULES_JVM_EXTERNAL_SHA = "62133c125bf4109dfd9d2af64830208356ce4ef8b165a6ef15bbff7460b35c3a"
http_archive(
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
sha256 = RULES_JVM_EXTERNAL_SHA,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)
load("@rules_jvm_external//:defs.bzl", "maven_install")
load("@io_grpc_grpc_java//:repositories.bzl", "IO_GRPC_GRPC_JAVA_ARTIFACTS")
load("@io_grpc_grpc_java//:repositories.bzl", "IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS")
maven_install(
artifacts = [
"org.spigotmc:spigot-api:1.15.2-R0.1-SNAPSHOT",
"io.grpc:grpc-netty-shaded:1.29.0",
"io.grpc:grpc-services:1.29.0",
] + IO_GRPC_GRPC_JAVA_ARTIFACTS,
generate_compat_repositories = True,
override_targets = IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS,
repositories = [
"https://hub.spigotmc.org/nexus/content/repositories/snapshots",
"https://oss.sonatype.org/content/repositories/snapshots",
"https://repo1.maven.org/maven2/",
],
maven_install_json = "//third_party/java:maven_install.json",
)
load("@maven//:defs.bzl", "pinned_maven_install")
pinned_maven_install()
load("@maven//:compat.bzl", "compat_repositories")
compat_repositories()
load("@io_grpc_grpc_java//:repositories.bzl", "grpc_java_repositories")
grpc_java_repositories()
# Gerrit OWNERS plugins external repositories
git_repository(
@ -1315,42 +1362,6 @@ go_repository(
importpath = "github.com/armon/consul-api",
)
go_repository(
name = "com_github_azure_go_autorest_autorest",
importpath = "github.com/Azure/go-autorest/autorest",
tag = "v0.9.0",
)
go_repository(
name = "com_github_azure_go_autorest_autorest_adal",
importpath = "github.com/Azure/go-autorest/autorest/adal",
tag = "v0.5.0",
)
go_repository(
name = "com_github_azure_go_autorest_autorest_date",
importpath = "github.com/Azure/go-autorest/autorest/date",
tag = "v0.1.0",
)
go_repository(
name = "com_github_azure_go_autorest_autorest_mocks",
importpath = "github.com/Azure/go-autorest/autorest/mocks",
tag = "v0.2.0",
)
go_repository(
name = "com_github_azure_go_autorest_logger",
importpath = "github.com/Azure/go-autorest/logger",
tag = "v0.1.0",
)
go_repository(
name = "com_github_azure_go_autorest_tracing",
importpath = "github.com/Azure/go-autorest/tracing",
tag = "v0.5.0",
)
go_repository(
name = "com_github_bgentry_speakeasy",
importpath = "github.com/bgentry/speakeasy",

View File

@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
go_library(
name = "go_default_library",
srcs = ["genpluginyml.go"],
importpath = "code.hackerspace.pl/personal/q3k/minecraft/plugins/genpluginyml",
visibility = ["//visibility:private"],
)
go_binary(
name = "genpluginyml",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,64 @@
load("//bzl:rules.bzl", copy_binary="copy_go_binary")
def _plugin_yml_gen_impl(ctx):
ctx.actions.run(
mnemonic = "PluginYmlGen",
progress_message = "Generating plugin.yml",
inputs = [ctx.info_file],
outputs = [ctx.outputs.out],
executable = ctx.executable._genpluginyml,
arguments = [
"-name", ctx.label.name,
"-author", ctx.attr.author,
"-main", ctx.attr.main,
"-version", ctx.attr.version,
"-status_file", ctx.info_file.path,
"-out_file", ctx.outputs.out.path,
],
)
plugin_yml_gen = rule(
implementation = _plugin_yml_gen_impl,
attrs = {
"main": attr.string(mandatory = True),
"version": attr.string(default = ""),
"author": attr.string(default = "bazel"),
"_genpluginyml": attr.label(
default = Label("//personal/q3k/minecraft/plugin:genpluginyml"),
cfg = "host",
executable = True,
allow_files = True,
),
},
outputs = {
"out": "plugin.yml",
},
)
def bukkit_plugin(name, srcs, deps, main, author="", version=""):
ymlname = name + "-yml"
plugin_yml_gen(
name = ymlname,
author = author,
version = version,
main = main,
)
jarname = name + "-jar"
native.java_binary(
name = jarname,
create_executable = False,
srcs = srcs,
deps = deps,
classpath_resources = [":" + ymlname],
)
copy_binary(
name = name + ".jar",
src = ":" + jarname + "_deploy.jar",
)
native.alias(
name = name,
actual = ":" + name + ".jar",
)

View File

@ -0,0 +1,11 @@
load("//personal/q3k/minecraft/plugin:defs.bzl", "bukkit_plugin")
bukkit_plugin(
name = "example",
main = "hscloud.personal.q3k.minecraft.plugin.example.Main",
srcs = [
"Main.java"
],
deps = [
"@maven//:org_spigotmc_spigot_api",
],
)

View File

@ -0,0 +1,15 @@
package hscloud.personal.q3k.minecraft.plugin.example;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
@Override
public void onEnable() {
System.out.println("enabled");
}
@Override
public void onDisable() {
System.out.println("disabled");
}
}

View File

@ -0,0 +1,76 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"strings"
)
var (
flagName string
flagAuthor string
flagMain string
flagVersion string
flagStatusFile string
flagOutFile string
)
func main() {
flag.StringVar(&flagName, "name", "", "plugin name")
flag.StringVar(&flagAuthor, "author", "", "plugin author")
flag.StringVar(&flagMain, "main", "", "plugin main class")
flag.StringVar(&flagVersion, "version", "", "plugin version, if not given, workspace status will be used")
flag.StringVar(&flagStatusFile, "status_file", "", "path to workspace status file for version generation")
flag.StringVar(&flagOutFile, "out_file", "plugin.yml", "path to output plugin.yml")
flag.Parse()
if flagVersion == "" {
status, err := ioutil.ReadFile(flagStatusFile)
if err != nil {
log.Fatalf("ReadFile(%q): %v", flagStatusFile, err)
}
for _, line := range strings.Split(string(status), "\n") {
line = strings.TrimSpace(line)
parts := strings.Fields(line)
if len(parts) != 2 {
continue
}
if parts[0] == "STABLE_GIT_VERSION" {
flagVersion = fmt.Sprintf("hscloud-git-%s", parts[1])
}
}
}
if flagVersion == "" {
log.Fatalf("could not determine version from status")
}
// a yaml is a json, a json is a yaml
yml := struct {
Name string `json:"name"`
Version string `json:"version"`
Author string `json:"author"`
Main string `json:"main"`
APIVersion string `json:"api-version"`
}{
Name: flagName,
Version: flagVersion,
Author: flagAuthor,
Main: flagMain,
APIVersion: "1.15",
}
out, err := json.Marshal(&yml)
if err != nil {
log.Fatalf("marshal: %v", err)
}
err = ioutil.WriteFile(flagOutFile, out, 0644)
if err != nil {
log.Fatalf("WriteFile(%q): %v", out, err)
}
}

View File

@ -0,0 +1,19 @@
load("//personal/q3k/minecraft/plugin:defs.bzl", "bukkit_plugin")
bukkit_plugin(
name = "hscloud",
main = "hscloud.personal.q3k.minecraft.plugin.hscloud.Main",
author = "q3k",
srcs = [
"Main.java",
"StateSynchronizer.java",
],
deps = [
"//personal/q3k/minecraft/plugin/hscloud/proto:hscloud_java_grpc",
"//personal/q3k/minecraft/plugin/hscloud/proto:hscloud_java_proto",
"@maven//:org_spigotmc_spigot_api",
"@io_grpc_grpc_java//api",
"@io_grpc_grpc_java//stub",
"@maven//:io_grpc_grpc_netty_shaded",
"@maven//:io_grpc_grpc_services",
],
)

View File

@ -0,0 +1,108 @@
package hscloud.personal.q3k.minecraft.plugin.hscloud;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import java.util.logging.Logger;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.protobuf.services.ProtoReflectionService;
import io.grpc.stub.StreamObserver;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import hscloud.personal.q3k.minecraft.plugin.hscloud.proto.IntrospectorGrpc;
import hscloud.personal.q3k.minecraft.plugin.hscloud.proto.Proto;
public class Main extends JavaPlugin {
private Server server_;
private static final Logger logger_ = Logger.getLogger(Main.class.getName());
private static final int port_ = 2137;
public StateSynchronizer synchronizer_;
public Main() {
synchronizer_ = new StateSynchronizer();
server_ = ServerBuilder
.forPort(port_)
.addService(new IntrospectorService(synchronizer_))
.addService(ProtoReflectionService.newInstance())
.build();
}
@Override
public void onEnable() {
BukkitScheduler scheduler = getServer().getScheduler();
scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
@Override
public void run() {
ArrayList<StateSynchronizer.Player> players = new ArrayList<StateSynchronizer.Player>();
for (Player p : Bukkit.getOnlinePlayers()) {
StateSynchronizer.Player pp = new StateSynchronizer.Player(p.getPlayerListName(), p.getUniqueId().toString());
players.add(pp);
}
synchronizer_.setPlayers(players.toArray(new StateSynchronizer.Player[0]));
World world = Bukkit.getWorld("world");
if (world != null) {
synchronizer_.setTime(world.getTime());
}
}
}, 0, 20L);
try {
server_.start();
} catch (IOException e) {
logger_.severe("could not start gRPC");
e.printStackTrace(System.err);
return;
}
logger_.info("gRPC started, listening on " + port_);
}
@Override
public void onDisable() {
if (server_ != null) {
try {
server_.shutdown().awaitTermination(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
logger_.severe("could not stop gRPC");
e.printStackTrace(System.err);
}
}
server_ = null;
}
public static class IntrospectorService extends IntrospectorGrpc.IntrospectorImplBase {
private StateSynchronizer synchronizer_;
public IntrospectorService(StateSynchronizer synchronizer) {
synchronizer_ = synchronizer;
}
@Override
public void status(Proto.StatusRequest request, StreamObserver<Proto.StatusResponse> responseObserver) {
StateSynchronizer.Player[] players = synchronizer_.getPlayers();
Proto.StatusResponse.Builder builder = Proto.StatusResponse.newBuilder();
builder.setTime(synchronizer_.getTime());
for (StateSynchronizer.Player p : players) {
builder.addPlayers(Proto.Player.newBuilder()
.setUsername(p.getName())
.setUuid(p.getUUID())
.build());
}
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
}
}
}

View File

@ -0,0 +1,49 @@
package hscloud.personal.q3k.minecraft.plugin.hscloud;
public class StateSynchronizer {
private Player[] players_;
private long time_;
private Object lock_ = new Object();
public void setPlayers(Player[] players) {
synchronized(lock_) {
players_ = players;
}
}
public void setTime(long time) {
synchronized(lock_) {
time_ = time;
}
}
public Player[] getPlayers() {
synchronized(lock_) {
return players_;
}
}
public long getTime() {
synchronized(lock_) {
return time_;
}
}
public static class Player {
private String name_;
private String uuid_;
public Player(String name, String uuid) {
name_ = name;
uuid_ = uuid;
}
public String getName() {
return name_;
}
public String getUUID() {
return uuid_;
}
}
}

View File

@ -0,0 +1,20 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_grpc_grpc_java//:java_grpc_library.bzl", "java_grpc_library")
proto_library(
name = "hscloud_proto",
srcs = ["hscloud.proto"],
)
java_proto_library(
name = "hscloud_java_proto",
deps = [":hscloud_proto"],
visibility = ["//visibility:public"],
)
java_grpc_library(
name = "hscloud_java_grpc",
srcs = [":hscloud_proto"],
deps = [":hscloud_java_proto"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,21 @@
syntax = "proto3";
package hscloud.personal.q3k.minecraft.plugin.hscloud;
option java_package = "hscloud.personal.q3k.minecraft.plugin.hscloud.proto";
option java_outer_classname = "Proto";
service Introspector {
rpc Status(StatusRequest) returns (StatusResponse);
}
message StatusRequest {
}
message StatusResponse {
repeated Player players = 1;
int64 time = 2;
}
message Player {
string username = 1;
string uuid = 2;
}