From d382eb52e7989ea859730373612b5e4581c159dc Mon Sep 17 00:00:00 2001 From: Allink <44676012+allinkdev@users.noreply.github.com> Date: Sun, 10 Jul 2022 16:49:07 +0100 Subject: [PATCH] Add scissors config, command & disable command books (1.18.2) (#40) * Add Scissors configuration file & command * Disable running commands in books by default --- ...0003-Add-Scissors-configuration-file.patch | 24 + ...-Scissors-configuration-file-command.patch | 435 ++++++++++++++++++ ...running-commands-in-books-by-default.patch | 86 ++++ 3 files changed, 545 insertions(+) create mode 100644 patches/api/0003-Add-Scissors-configuration-file.patch create mode 100644 patches/server/0037-Add-Scissors-configuration-file-command.patch create mode 100644 patches/server/0038-Disable-running-commands-in-books-by-default.patch diff --git a/patches/api/0003-Add-Scissors-configuration-file.patch b/patches/api/0003-Add-Scissors-configuration-file.patch new file mode 100644 index 0000000..ff18c50 --- /dev/null +++ b/patches/api/0003-Add-Scissors-configuration-file.patch @@ -0,0 +1,24 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Allink +Date: Sun, 10 Jul 2022 09:56:34 +0100 +Subject: [PATCH] Add Scissors configuration file + + +diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java +index 79b26045a68ebb9b01e5bd06abbccaaef5489777..eb66a2cf4785c9f8ef4bdebee59f29777bf9fa50 100644 +--- a/src/main/java/org/bukkit/Server.java ++++ b/src/main/java/org/bukkit/Server.java +@@ -1897,6 +1897,13 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi + } + // Paper end + ++ // Scissors start ++ @NotNull ++ public org.bukkit.configuration.file.YamlConfiguration getScissorsConfig() { ++ throw new UnsupportedOperationException("Not supported yet."); ++ } ++ // Scissors end ++ + /** + * Sends the component to the player + * diff --git a/patches/server/0037-Add-Scissors-configuration-file-command.patch b/patches/server/0037-Add-Scissors-configuration-file-command.patch new file mode 100644 index 0000000..46c9a62 --- /dev/null +++ b/patches/server/0037-Add-Scissors-configuration-file-command.patch @@ -0,0 +1,435 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Allink +Date: Sun, 10 Jul 2022 09:57:10 +0100 +Subject: [PATCH] Add Scissors configuration file & command + + +diff --git a/src/main/java/co/aikar/timings/TimingsExport.java b/src/main/java/co/aikar/timings/TimingsExport.java +index 06bff37e4c1fddd3be6343049a66787c63fb420c..d00f35538602151bc8eb564541417a5324eefd18 100644 +--- a/src/main/java/co/aikar/timings/TimingsExport.java ++++ b/src/main/java/co/aikar/timings/TimingsExport.java +@@ -241,7 +241,8 @@ public class TimingsExport extends Thread { + parent.put("config", createObject( + pair("spigot", mapAsJSON(Bukkit.spigot().getSpigotConfig(), null)), + pair("bukkit", mapAsJSON(Bukkit.spigot().getBukkitConfig(), null)), +- pair("paper", mapAsJSON(Bukkit.spigot().getPaperConfig(), null)) ++ pair("paper", mapAsJSON(Bukkit.spigot().getPaperConfig(), null)), ++ pair("scissors", mapAsJSON(Bukkit.spigot().getScissorsConfig(), null)) // Scissors + )); + + new TimingsExport(listeners, parent, history).start(); +diff --git a/src/main/java/com/github/atlasmediagroup/scissors/ScissorsCommand.java b/src/main/java/com/github/atlasmediagroup/scissors/ScissorsCommand.java +new file mode 100644 +index 0000000000000000000000000000000000000000..2085c48fa4dda81bf275aee0b92c9e8206dba8e2 +--- /dev/null ++++ b/src/main/java/com/github/atlasmediagroup/scissors/ScissorsCommand.java +@@ -0,0 +1,156 @@ ++package com.github.atlasmediagroup.scissors; ++ ++import com.google.common.base.Functions; ++import com.google.common.base.Joiner; ++import com.google.common.collect.ImmutableSet; ++import com.google.common.collect.Iterables; ++import com.google.common.collect.Lists; ++import java.io.File; ++import java.util.ArrayList; ++import java.util.Arrays; ++import java.util.Collection; ++import java.util.Collections; ++import java.util.Iterator; ++import java.util.List; ++import java.util.Locale; ++import java.util.stream.Collectors; ++import net.minecraft.resources.ResourceLocation; ++import net.minecraft.server.MinecraftServer; ++import org.bukkit.Bukkit; ++import org.bukkit.ChatColor; ++import org.bukkit.Location; ++import org.bukkit.command.Command; ++import org.bukkit.command.CommandSender; ++ ++public class ScissorsCommand extends Command ++{ ++ ++ private static final String BASE_PERM = "bukkit.command.scissors."; ++ private static final ImmutableSet SUBCOMMANDS = ImmutableSet.builder().add("reload", "version").build(); ++ ++ public ScissorsCommand(String name) ++ { ++ super(name); ++ this.description = "Scissors related commands"; ++ this.usageMessage = "/scissors [" + Joiner.on(" | ").join(SUBCOMMANDS) + "]"; ++ this.setPermission("bukkit.command.scissors;" + Joiner.on(';').join(SUBCOMMANDS.stream().map(s -> BASE_PERM + s).collect( ++ Collectors.toSet()))); ++ } ++ ++ private static boolean testPermission(CommandSender commandSender, String permission) ++ { ++ if (commandSender.hasPermission(BASE_PERM + permission) || commandSender.hasPermission("bukkit.command.scissors")) ++ return true; ++ commandSender.sendMessage(Bukkit.getPermissionMessage()); // Sorry, kashike ++ return false; ++ } ++ ++ // Code from Mojang - copyright them ++ public static List getListMatchingLast(CommandSender sender, String[] args, String... matches) ++ { ++ return getListMatchingLast(sender, args, Arrays.asList(matches)); ++ } ++ ++ public static boolean matches(String s, String s1) ++ { ++ return s1.regionMatches(true, 0, s, 0, s.length()); ++ } ++ ++ public static List getListMatchingLast(CommandSender sender, String[] strings, Collection collection) ++ { ++ String last = strings[strings.length - 1]; ++ ArrayList results = Lists.newArrayList(); ++ ++ if (!collection.isEmpty()) ++ { ++ Iterator iterator = Iterables.transform(collection, Functions.toStringFunction()).iterator(); ++ ++ while (iterator.hasNext()) ++ { ++ String s1 = (String) iterator.next(); ++ ++ if (matches(last, s1) && (sender.hasPermission(BASE_PERM + s1) || sender.hasPermission("bukkit.command.scissors"))) ++ { ++ results.add(s1); ++ } ++ } ++ ++ if (results.isEmpty()) ++ { ++ iterator = collection.iterator(); ++ ++ while (iterator.hasNext()) ++ { ++ Object object = iterator.next(); ++ ++ if (object instanceof ResourceLocation && matches(last, ((ResourceLocation) object).getPath())) ++ { ++ results.add(String.valueOf(object)); ++ } ++ } ++ } ++ } ++ ++ return results; ++ } ++ ++ @Override ++ public List tabComplete(CommandSender sender, String alias, String[] args, Location location) throws IllegalArgumentException ++ { ++ if (args.length <= 1) ++ return getListMatchingLast(sender, args, SUBCOMMANDS); ++ ++ return Collections.emptyList(); ++ } ++ // end copy stuff ++ ++ @Override ++ public boolean execute(CommandSender sender, String commandLabel, String[] args) ++ { ++ if (!testPermission(sender)) return true; ++ ++ if (args.length == 0) ++ { ++ sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); ++ return false; ++ } ++ if (SUBCOMMANDS.contains(args[0].toLowerCase(Locale.ENGLISH))) ++ { ++ if (!testPermission(sender, args[0].toLowerCase(Locale.ENGLISH))) return true; ++ } ++ switch (args[0].toLowerCase(Locale.ENGLISH)) ++ { ++ case "reload": ++ doReload(sender); ++ break; ++ case "ver": ++ if (!testPermission(sender, "version")) ++ break; // "ver" needs a special check because it's an alias. All other commands are checked up before the switch statement (because they are present in the SUBCOMMANDS set) ++ case "version": ++ Command ver = MinecraftServer.getServer().server.getCommandMap().getCommand("version"); ++ if (ver != null) ++ { ++ ver.execute(sender, commandLabel, new String[0]); ++ break; ++ } ++ // else - fall through to default ++ default: ++ sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); ++ return false; ++ } ++ ++ return true; ++ } ++ ++ private void doReload(CommandSender sender) ++ { ++ Command.broadcastCommandMessage(sender, ChatColor.RED + "Please note that this command is not supported and may cause issues."); ++ Command.broadcastCommandMessage(sender, ChatColor.RED + "If you encounter any issues please use the /stop command to restart your server."); ++ ++ MinecraftServer console = MinecraftServer.getServer(); ++ ScissorsConfig.init((File) console.options.valueOf("scissors-settings")); ++ console.server.reloadCount++; ++ ++ Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Scissors config reload complete."); ++ } ++} +diff --git a/src/main/java/com/github/atlasmediagroup/scissors/ScissorsConfig.java b/src/main/java/com/github/atlasmediagroup/scissors/ScissorsConfig.java +new file mode 100644 +index 0000000000000000000000000000000000000000..9612d02814d6ae45d7087c34e0c8549e6c9aecf9 +--- /dev/null ++++ b/src/main/java/com/github/atlasmediagroup/scissors/ScissorsConfig.java +@@ -0,0 +1,175 @@ ++package com.github.atlasmediagroup.scissors; ++ ++import com.google.common.base.Throwables; ++import java.io.File; ++import java.io.IOException; ++import java.lang.reflect.InvocationTargetException; ++import java.lang.reflect.Method; ++import java.lang.reflect.Modifier; ++import java.util.HashMap; ++import java.util.List; ++import java.util.Map; ++import java.util.logging.Level; ++import java.util.regex.Pattern; ++import net.minecraft.server.MinecraftServer; ++import org.bukkit.Bukkit; ++import org.bukkit.command.Command; ++import org.bukkit.configuration.InvalidConfigurationException; ++import org.bukkit.configuration.file.YamlConfiguration; ++ ++public class ScissorsConfig ++{ ++ ++ private static final String HEADER = """ ++ This is the main configuration file for Scissors. ++ As you can see, there's tons to configure. Some options may impact gameplay, so use ++ with caution, and make sure you know what each option does before configuring. ++ ++ If you need help with the configuration or have any questions related to Scissors, ++ join us in our Discord. ++ ++ Discord: https://discord.com/invite/mtVQcHn58h ++ Website: https://scissors.gg/\s ++ Docs: https://scissors.gg/javadoc/1.18.2/\s ++ """; ++ private static final Pattern SPACE = Pattern.compile(" "); ++ private static final Pattern NOT_NUMERIC = Pattern.compile("[^-\\d.]"); ++ /*========================================================================*/ ++ public static YamlConfiguration config; ++ static int version; ++ /*========================================================================*/ ++ static Map commands; ++ private static File CONFIG_FILE; ++ ++ public static void init(File configFile) ++ { ++ CONFIG_FILE = configFile; ++ config = new YamlConfiguration(); ++ try ++ { ++ config.load(CONFIG_FILE); ++ } ++ catch (IOException ex) ++ { ++ } ++ catch (InvalidConfigurationException ex) ++ { ++ Bukkit.getLogger().log(Level.SEVERE, "Could not load scissors.yml, please correct your syntax errors", ex); ++ throw Throwables.propagate(ex); ++ } ++ ++ commands = new HashMap<>(); ++ commands.put("scissors", new ScissorsCommand("scissors")); ++ ++ config.options().header(HEADER); ++ config.options().copyDefaults(true); ++ ++ version = getInt("config-version", 1); ++ set("config-version", 1); ++ readConfig(ScissorsConfig.class, null); ++ } ++ ++ protected static void logError(String s) ++ { ++ Bukkit.getLogger().severe(s); ++ } ++ ++ protected static void fatal(String s) ++ { ++ throw new RuntimeException("Fatal scissors.yml config error: " + s); ++ } ++ ++ public static void registerCommands() ++ { ++ for (Map.Entry entry : commands.entrySet()) ++ { ++ MinecraftServer.getServer().server.getCommandMap().register(entry.getKey(), "Scissors", entry.getValue()); ++ } ++ } ++ ++ static void readConfig(Class clazz, Object instance) ++ { ++ for (Method method : clazz.getDeclaredMethods()) ++ { ++ if (Modifier.isPrivate(method.getModifiers())) ++ { ++ if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) ++ { ++ try ++ { ++ method.setAccessible(true); ++ method.invoke(instance); ++ } ++ catch (InvocationTargetException ex) ++ { ++ throw Throwables.propagate(ex.getCause()); ++ } ++ catch (Exception ex) ++ { ++ Bukkit.getLogger().log(Level.SEVERE, "Error invoking " + method, ex); ++ } ++ } ++ } ++ } ++ saveConfig(); ++ } ++ ++ static void saveConfig() ++ { ++ try ++ { ++ config.save(CONFIG_FILE); ++ } ++ catch (IOException ex) ++ { ++ Bukkit.getLogger().log(Level.SEVERE, "Could not save " + CONFIG_FILE, ex); ++ } ++ } ++ ++ public static boolean runCommandsInBooks = false; ++ ++ private static void runCommandsInBooks() { ++ runCommandsInBooks = getBoolean("runCommandsInBooks", false); ++ } ++ ++ private static void set(String path, Object val) ++ { ++ config.set(path, val); ++ } ++ ++ private static boolean getBoolean(String path, boolean def) ++ { ++ config.addDefault(path, def); ++ return config.getBoolean(path, config.getBoolean(path)); ++ } ++ ++ private static double getDouble(String path, double def) ++ { ++ config.addDefault(path, def); ++ return config.getDouble(path, config.getDouble(path)); ++ } ++ ++ private static float getFloat(String path, float def) ++ { ++ // TODO: Figure out why getFloat() always returns the default value. ++ return (float) getDouble(path, def); ++ } ++ ++ private static int getInt(String path, int def) ++ { ++ config.addDefault(path, def); ++ return config.getInt(path, config.getInt(path)); ++ } ++ ++ private static List getList(String path, T def) ++ { ++ config.addDefault(path, def); ++ return config.getList(path, config.getList(path)); ++ } ++ ++ private static String getString(String path, String def) ++ { ++ config.addDefault(path, def); ++ return config.getString(path, config.getString(path)); ++ } ++} +diff --git a/src/main/java/net/minecraft/server/Main.java b/src/main/java/net/minecraft/server/Main.java +index 1e0d261439255091a6f61485c0747231fbd5b1db..63861ff0c0e942dfafad7ceedfa17864c100b301 100644 +--- a/src/main/java/net/minecraft/server/Main.java ++++ b/src/main/java/net/minecraft/server/Main.java +@@ -114,6 +114,7 @@ public class Main { + org.bukkit.configuration.file.YamlConfiguration bukkitConfiguration = loadConfigFile((File) optionset.valueOf("bukkit-settings")); + org.bukkit.configuration.file.YamlConfiguration spigotConfiguration = loadConfigFile((File) optionset.valueOf("spigot-settings")); + org.bukkit.configuration.file.YamlConfiguration paperConfiguration = loadConfigFile((File) optionset.valueOf("paper-settings")); ++ org.bukkit.configuration.file.YamlConfiguration scissorsConfiguration = loadConfigFile((File) optionset.valueOf("scissors-settings")); // Scissors + // Paper end + + Path path1 = Paths.get("eula.txt"); +diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java +index 257c94f7c1cb00c9a91ab82e311dfd8eca29c538..605602ca03bd0d974b559881ab0ad4da66640002 100644 +--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java ++++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java +@@ -235,7 +235,15 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface + io.papermc.paper.util.ObfHelper.INSTANCE.getClass(); // load mappings for stacktrace deobf and etc. + io.papermc.paper.brigadier.PaperBrigadierProviderImpl.INSTANCE.getClass(); // init PaperBrigadierProvider + // Paper end +- ++ // Scissors start ++ try { ++ com.github.atlasmediagroup.scissors.ScissorsConfig.init((java.io.File) options.valueOf("scissors-settings")); ++ } catch (Exception e) { ++ DedicatedServer.LOGGER.error("Unable to load server configuration", e); ++ return false; ++ } ++ com.github.atlasmediagroup.scissors.ScissorsConfig.registerCommands(); ++ // Scissors end + this.setPvpAllowed(dedicatedserverproperties.pvp); + this.setFlightAllowed(dedicatedserverproperties.allowFlight); + this.setResourcePack(dedicatedserverproperties.resourcePack, this.getPackHash()); +diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java +index ebd5aeceb4ac1a6300bbfa70218421f0d91b7614..7b341c8648f41eecff3de65985edbdb05cbf2651 100644 +--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java ++++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java +@@ -956,6 +956,7 @@ public final class CraftServer implements Server { + + org.spigotmc.SpigotConfig.init((File) console.options.valueOf("spigot-settings")); // Spigot + com.destroystokyo.paper.PaperConfig.init((File) console.options.valueOf("paper-settings")); // Paper ++ com.github.atlasmediagroup.scissors.ScissorsConfig.init(((File) console.options.valueOf("scissors-settings"))); // Scissors + for (ServerLevel world : this.console.getAllLevels()) { + // world.serverLevelData.setDifficulty(config.difficulty); // Paper - per level difficulty + world.setSpawnSettings(world.serverLevelData.getDifficulty() != Difficulty.PEACEFUL && config.spawnMonsters, config.spawnAnimals); // Paper - per level difficulty (from MinecraftServer#setDifficulty(ServerLevel, Difficulty, boolean)) +@@ -987,6 +988,7 @@ public final class CraftServer implements Server { + this.reloadData(); + org.spigotmc.SpigotConfig.registerCommands(); // Spigot + com.destroystokyo.paper.PaperConfig.registerCommands(); // Paper ++ com.github.atlasmediagroup.scissors.ScissorsConfig.registerCommands(); // Scissors + this.overrideAllCommandBlockCommands = this.commandsConfiguration.getStringList("command-block-overrides").contains("*"); + this.ignoreVanillaPermissions = this.commandsConfiguration.getBoolean("ignore-vanilla-permissions"); + +diff --git a/src/main/java/org/bukkit/craftbukkit/Main.java b/src/main/java/org/bukkit/craftbukkit/Main.java +index 40313b014b4ed5f7e9505bde2827383c19d4377a..d88a755e18c1da5e5a64ff88d96be33e3dd3dcc3 100644 +--- a/src/main/java/org/bukkit/craftbukkit/Main.java ++++ b/src/main/java/org/bukkit/craftbukkit/Main.java +@@ -160,6 +160,14 @@ public class Main { + .defaultsTo("Unknown Server") + .describedAs("Name"); + // Paper end ++ ++ // Scissors start ++ acceptsAll(asList("scissors", "scissors-settings"), "File for scissors settings") ++ .withRequiredArg() ++ .ofType(File.class) ++ .defaultsTo(new File("scissors.yml")) ++ .describedAs("Yml file"); ++ // Scissors end + } + }; + diff --git a/patches/server/0038-Disable-running-commands-in-books-by-default.patch b/patches/server/0038-Disable-running-commands-in-books-by-default.patch new file mode 100644 index 0000000..6b83f5c --- /dev/null +++ b/patches/server/0038-Disable-running-commands-in-books-by-default.patch @@ -0,0 +1,86 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Allink +Date: Sun, 10 Jul 2022 10:06:58 +0100 +Subject: [PATCH] Disable running commands in books by default + + +diff --git a/src/main/java/net/minecraft/world/item/WrittenBookItem.java b/src/main/java/net/minecraft/world/item/WrittenBookItem.java +index 26a9a15cc630113cd8d2c8287c6b0f1067ce53f0..15419727d37fc75bc18177cf3ea2daa54f97ec47 100644 +--- a/src/main/java/net/minecraft/world/item/WrittenBookItem.java ++++ b/src/main/java/net/minecraft/world/item/WrittenBookItem.java +@@ -1,7 +1,10 @@ + package net.minecraft.world.item; + + import java.util.List; ++import java.util.stream.Collectors; + import javax.annotation.Nullable; ++ ++import com.github.atlasmediagroup.scissors.ScissorsConfig; + import net.minecraft.ChatFormatting; + import net.minecraft.commands.CommandSourceStack; + import net.minecraft.core.BlockPos; +@@ -9,10 +12,7 @@ import net.minecraft.nbt.CompoundTag; + import net.minecraft.nbt.ListTag; + import net.minecraft.nbt.StringTag; + import net.minecraft.nbt.Tag; +-import net.minecraft.network.chat.Component; +-import net.minecraft.network.chat.ComponentUtils; +-import net.minecraft.network.chat.TextComponent; +-import net.minecraft.network.chat.TranslatableComponent; ++import net.minecraft.network.chat.*; + import net.minecraft.stats.Stats; + import net.minecraft.util.StringUtil; + import net.minecraft.world.InteractionHand; +@@ -113,8 +113,7 @@ public class WrittenBookItem extends Item { + + public static boolean resolveBookComponents(ItemStack book, @Nullable CommandSourceStack commandSource, @Nullable Player player) { + CompoundTag compoundTag = book.getTag(); +- if (com.destroystokyo.paper.PaperConfig.resolveSelectorsInBooks && compoundTag != null && !compoundTag.getBoolean("resolved")) { // Paper +- compoundTag.putBoolean("resolved", true); ++ if (compoundTag != null) { // Paper + if (!makeSureTagIsValid(compoundTag)) { + return false; + } else { +@@ -165,8 +164,41 @@ public class WrittenBookItem extends Item { + component2 = new TextComponent(text); + } + +- return Component.Serializer.toJson(component2); ++ return Component.Serializer.toJson(!ScissorsConfig.runCommandsInBooks ? sanitize(component2, 0) : component2); // Scissors - Allow server owners to disable run command in books ++ } ++ ++ // Scissors start - Allow server owners to disable run command in books ++ public static Component sanitize(Component component, int depth) { ++ if (depth > 128) { ++ return Component.nullToEmpty("Sanitization function depth limit exceeded"); ++ } ++ ++ MutableComponent component2 = component.copy(); ++ ++ final Style style = component2.getStyle(); ++ final ClickEvent clickEvent = style.getClickEvent(); ++ ++ if(clickEvent != null && clickEvent.getAction().equals(ClickEvent.Action.RUN_COMMAND)) ++ { ++ final String clickEventValue = clickEvent.getValue(); ++ ++ component2 = component2.copy().setStyle(style ++ .withClickEvent(null) ++ .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.nullToEmpty("Would've " + (clickEventValue.startsWith("/") ? "ran": "said") + ": " + clickEvent.getValue()))) ++ ); ++ } ++ ++ final List processedExtra = component2.getSiblings() ++ .stream() ++ .map(comp -> sanitize(comp, depth + 1)) ++ .toList(); ++ ++ component2.getSiblings().clear(); ++ component2.getSiblings().addAll(processedExtra); ++ ++ return component2; + } ++ // Scissors end + + @Override + public boolean isFoil(ItemStack stack) {