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) {