Scissors/patches/server/0036-Disable-running-commands-in-books-by-default.patch

97 lines
4.2 KiB
Diff
Raw Normal View History

2022-07-28 03:57:50 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Allink <arclicious@vivaldi.net>
Date: Sun, 10 Jul 2022 10:29:03 +0100
Subject: [PATCH] Disable running commands in books by default
diff --git a/src/main/java/me/totalfreedom/scissors/ScissorsConfig.java b/src/main/java/me/totalfreedom/scissors/ScissorsConfig.java
index e08f502fc7165f9f466217910210edb5059d39a8..1aa418ef5e400aabdf17dbe81da6cee6a1f63d96 100644
--- a/src/main/java/me/totalfreedom/scissors/ScissorsConfig.java
+++ b/src/main/java/me/totalfreedom/scissors/ScissorsConfig.java
@@ -151,7 +151,8 @@ public class ScissorsConfig
public static boolean runCommandsInBooks = false;
- private static void runCommandsInBooks() {
+ private static void runCommandsInBooks()
+ {
runCommandsInBooks = getBoolean("runCommandsInBooks", false);
}
2022-07-28 03:57:50 +00:00
diff --git a/src/main/java/net/minecraft/world/item/WrittenBookItem.java b/src/main/java/net/minecraft/world/item/WrittenBookItem.java
index 31911c09fe15753ae32fa39417bdc9e9de552a88..8ef33e2e2374c456cb9d4aab8ed6f1742951f402 100644
--- a/src/main/java/net/minecraft/world/item/WrittenBookItem.java
+++ b/src/main/java/net/minecraft/world/item/WrittenBookItem.java
@@ -2,6 +2,8 @@ package net.minecraft.world.item;
import java.util.List;
import javax.annotation.Nullable;
+
+import me.totalfreedom.scissors.ScissorsConfig;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.BlockPos;
@@ -9,8 +11,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.*;
import net.minecraft.stats.Stats;
import net.minecraft.util.StringUtil;
import net.minecraft.world.InteractionHand;
@@ -111,8 +112,7 @@ public class WrittenBookItem extends Item {
public static boolean resolveBookComponents(ItemStack book, @Nullable CommandSourceStack commandSource, @Nullable Player player) {
CompoundTag compoundTag = book.getTag();
- if (io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.resolveSelectorsInBooks && compoundTag != null && !compoundTag.getBoolean("resolved")) { // Paper
- compoundTag.putBoolean("resolved", true);
+ if (compoundTag != null) { // Paper
if (!makeSureTagIsValid(compoundTag)) {
return false;
} else {
@@ -161,8 +161,41 @@ public class WrittenBookItem extends Item {
component2 = Component.literal(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<Component> 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) {