From efa666445f4c0e3a4a988e50e0790bb3934ea5d3 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 15 Jul 2018 09:10:42 -0700 Subject: [PATCH] Initial support for Brigadier (1.13 commands) on Bukkit servers --- bukkit/pom.xml | 22 +++ .../luckperms/bukkit/LPBukkitPlugin.java | 25 +++- .../bukkit/compat/LuckPermsBrigadier.java | 140 ++++++++++++++++++ .../resources/luckperms-brigadier.json.gz | Bin 0 -> 2580 bytes .../common/dependencies/Dependency.java | 7 + 5 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 bukkit/src/main/java/me/lucko/luckperms/bukkit/compat/LuckPermsBrigadier.java create mode 100644 bukkit/src/main/resources/luckperms-brigadier.json.gz diff --git a/bukkit/pom.xml b/bukkit/pom.xml index 28c2a297..18989f43 100644 --- a/bukkit/pom.xml +++ b/bukkit/pom.xml @@ -19,6 +19,16 @@ src/main/resources true + + *.yml + + + + src/main/resources + false + + luckperms-brigadier.json.gz + @@ -68,6 +78,10 @@ okhttp3 me.lucko.luckperms.lib.okhttp3 + + me.lucko.commodore + me.lucko.luckperms.lib.commodore + org.mariadb.jdbc me.lucko.luckperms.lib.mariadb @@ -129,6 +143,14 @@ provided + + + me.lucko + commodore + 1.0 + provided + + diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java index b72e4953..b22c28b3 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -29,6 +29,7 @@ import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.LuckPermsApi; import me.lucko.luckperms.api.event.user.UserDataRecalculateEvent; import me.lucko.luckperms.bukkit.calculators.BukkitCalculatorFactory; +import me.lucko.luckperms.bukkit.compat.LuckPermsBrigadier; import me.lucko.luckperms.bukkit.contexts.BukkitContextManager; import me.lucko.luckperms.bukkit.contexts.WorldCalculator; import me.lucko.luckperms.bukkit.listeners.BukkitConnectionListener; @@ -111,9 +112,22 @@ public class LPBukkitPlugin extends AbstractLuckPermsPlugin { this.senderFactory = new BukkitSenderFactory(this); } + private static boolean isBrigadierSupported() { + try { + Class.forName("com.mojang.brigadier.CommandDispatcher"); + return true; + } catch (ClassNotFoundException var1) { + return false; + } + } + @Override protected Set getGlobalDependencies() { - return EnumSet.of(Dependency.TEXT, Dependency.CAFFEINE, Dependency.OKIO, Dependency.OKHTTP); + EnumSet dependencies = EnumSet.of(Dependency.TEXT, Dependency.CAFFEINE, Dependency.OKIO, Dependency.OKHTTP); + if (isBrigadierSupported()) { + dependencies.add(Dependency.COMMODORE); + } + return dependencies; } @Override @@ -139,6 +153,15 @@ public class LPBukkitPlugin extends AbstractLuckPermsPlugin { PluginCommand cmd = this.bootstrap.getCommand("luckperms"); cmd.setExecutor(this.commandManager); cmd.setTabCompleter(this.commandManager); + + // setup brigadier + if (isBrigadierSupported()) { + try { + LuckPermsBrigadier.register(this, cmd); + } catch (Exception e) { + e.printStackTrace(); + } + } } @Override diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/compat/LuckPermsBrigadier.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/compat/LuckPermsBrigadier.java new file mode 100644 index 00000000..377ae523 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/compat/LuckPermsBrigadier.java @@ -0,0 +1,140 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.bukkit.compat; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.arguments.BoolArgumentType; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.ArgumentBuilder; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; + +import me.lucko.commodore.Commodore; +import me.lucko.commodore.CommodoreProvider; +import me.lucko.luckperms.bukkit.LPBukkitPlugin; + +import org.bukkit.command.Command; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Objects; +import java.util.zip.GZIPInputStream; + +public class LuckPermsBrigadier { + + public static void register(LPBukkitPlugin plugin, Command pluginCommand) throws Exception { + Commodore commodore = CommodoreProvider.getCommodore(plugin.getBootstrap()); + try (InputStream is = plugin.getBootstrap().getResourceStream("luckperms-brigadier.json.gz")) { + if (is == null) { + throw new Exception("Brigadier command data missing from jar!"); + } + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(is), StandardCharsets.UTF_8))) { + JsonObject data = new JsonParser().parse(reader).getAsJsonObject(); + LiteralArgumentBuilder command = deserializeLiteral(data); + commodore.register(pluginCommand, command); + } + } + } + + private static ArgumentBuilder deserialize(JsonObject data) { + String type = data.get("type").getAsString(); + switch (type) { + case "literal": { + return deserializeLiteral(data); + } + case "argument": { + return deserializeArgument(data); + } + default: + throw new IllegalArgumentException("type: " + type); + } + } + + private static LiteralArgumentBuilder deserializeLiteral(JsonObject data) { + String name = data.get("name").getAsString(); + LiteralArgumentBuilder arg = LiteralArgumentBuilder.literal(name); + return deserializeChildren(data, arg); + } + + private static RequiredArgumentBuilder deserializeArgument(JsonObject data) { + String name = data.get("name").getAsString(); + ArgumentType argumentType = deserializeArgumentType(data); + + //noinspection unchecked + RequiredArgumentBuilder arg = RequiredArgumentBuilder.argument(name, argumentType); + return deserializeChildren(data, arg); + } + + private static ArgumentType deserializeArgumentType(JsonObject data) { + String parser = data.get("parser").getAsString(); + String properties = null; + if (data.has("properties")) { + properties = data.get("properties").getAsString(); + } + + switch (parser) { + case "brigadier:string": { + Objects.requireNonNull(properties, "string properties"); + switch (properties) { + case "SINGLE_WORD": + return StringArgumentType.word(); + case "QUOTABLE_PHRASE": + return StringArgumentType.string(); + case "GREEDY_PHRASE": + return StringArgumentType.greedyString(); + default: + throw new IllegalArgumentException("string property: " + properties); + } + } + case "brigadier:bool": + return BoolArgumentType.bool(); + case "brigadier:integer": + return IntegerArgumentType.integer(); + default: + throw new IllegalArgumentException("parser: " + parser); + } + } + + private static T deserializeChildren(JsonObject data, T builder) { + if (data.has("children")) { + JsonArray children = data.get("children").getAsJsonArray(); + for (JsonElement child : children) { + //noinspection unchecked + builder.then(deserialize(child.getAsJsonObject())); + } + } + return builder; + } + +} diff --git a/bukkit/src/main/resources/luckperms-brigadier.json.gz b/bukkit/src/main/resources/luckperms-brigadier.json.gz new file mode 100644 index 0000000000000000000000000000000000000000..92b5463d6414c33ab27f1de5a0eca6b92308a518 GIT binary patch literal 2580 zcmZXTc{mhm1I06zAqHba)={$EWH(_56Cq?L%OqQhB-ef|xt8o}M0O%O*D#tP`&e!! zJ7H)rBSx0ej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QY3JF*)k!mo8idDd)?es22xP)Vr(1&Uf)NW}=6I zZ_S4;IjTP%q%@qn15y?~3+E4Pjp)}V;h6Z{$5j5C{=dGM{{aywCBeLN_9F)D1Y@sf zdidWK%(!3V;7-Zm<~l79b6;NL?MEHWP|*h{>y+KHuhih52y5Ey+O6n-P?L=^8h%Yj zZGUH^XRn)n*H{9>Fc$~1k3ZBN>_cs*^6NgI3Vh z>@zGaq^F{6(-m<-^!K7x!&Pqar0sQDKrStJWSjq{WaHD}!ObB#7$#rIl{{JwW>EpoH#E z0`um@uj*oYk-ZUvFOGjgzL!~|^EG$VB$n(m4V6>H%qK&K)ep43eHy2jTcc4|nU0cM zo1sr`8FP=2xE1>R+gaQaC~pOoE!3eYH{W3DuQ%#ttT*B{My^N*q8I2EKALSF2x{EW zc8fVqtoU$zVpk8H|HQ>0de$DvGy_!n(|)?e14cNZ`y1dlVD$N zJR(04fRK`lQ9YzjXvuPzVU467CRse@K~=2gVFYZ5BQQIqKv~bEsu5-(6YX5f(17yf z{h$i-zpGo|m>=T>ZS#U(ry}DA!n5<@II{9~YeI(S=YLKdoe)m!OM!*FhIHrt0c;Ka Am;e9( literal 0 HcmV?d00001 diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java index db332e96..c4971e19 100644 --- a/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/Dependency.java @@ -91,6 +91,13 @@ public enum Dependency { Relocation.of(RelocationHelper.OKIO_STRING, RelocationHelper.OKIO_STRING) ) ), + COMMODORE( + "me{}lucko", + "commodore", + "1.0", + "Cu7m0zEOTts51dHix2RLKkeUapaek7rKnnxgRQeIlJE=", + Relocation.of("commodore", "me{}lucko{}commodore") + ), MARIADB_DRIVER( "org{}mariadb{}jdbc", "mariadb-java-client",