From 4c1251a23d02d9d45fb89a365db072be8dcbb354 Mon Sep 17 00:00:00 2001 From: Gugle Date: Sun, 3 Nov 2024 14:21:14 +0800 Subject: [PATCH] =?UTF-8?q?openFakePlayerEnderChest=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AE=BE=E5=AE=9A=E5=80=BC=20ender=5Fchest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/dubhe/gugle/carpet/GcaSetting.java | 6 +++-- .../dev/dubhe/gugle/carpet/GcaValidators.java | 23 +++++++++++++++++++ .../gugle/carpet/commands/BotCommand.java | 2 +- .../dubhe/gugle/carpet/mixin/PlayerMixin.java | 10 ++++---- .../dubhe/gugle/carpet/tools/GcaUtils.java | 16 +++++++++++++ 5 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 src/main/java/dev/dubhe/gugle/carpet/GcaValidators.java create mode 100644 src/main/java/dev/dubhe/gugle/carpet/tools/GcaUtils.java diff --git a/src/main/java/dev/dubhe/gugle/carpet/GcaSetting.java b/src/main/java/dev/dubhe/gugle/carpet/GcaSetting.java index 539e899..9bd2563 100644 --- a/src/main/java/dev/dubhe/gugle/carpet/GcaSetting.java +++ b/src/main/java/dev/dubhe/gugle/carpet/GcaSetting.java @@ -18,9 +18,11 @@ public class GcaSetting { // 允许玩家打开假人末影箱 @Rule( - categories = {GCA, EXPERIMENTAL, BOT} + categories = {GCA, EXPERIMENTAL, BOT}, + options = {"ender_chest", "true", "false"}, + validators = GcaValidators.EnderChest.class ) - public static boolean openFakePlayerEnderChest = false; + public static String openFakePlayerEnderChest = "false"; // 退出存档时保留假人 @Rule( diff --git a/src/main/java/dev/dubhe/gugle/carpet/GcaValidators.java b/src/main/java/dev/dubhe/gugle/carpet/GcaValidators.java new file mode 100644 index 0000000..e7f7760 --- /dev/null +++ b/src/main/java/dev/dubhe/gugle/carpet/GcaValidators.java @@ -0,0 +1,23 @@ +package dev.dubhe.gugle.carpet; + +import carpet.api.settings.CarpetRule; +import carpet.api.settings.Validator; +import net.minecraft.commands.CommandSourceStack; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class GcaValidators { + public static class EnderChest extends Validator { + public static final List OPTIONS = List.of("true", "false", "ender_chest"); + + @Override + public String validate(@Nullable CommandSourceStack commandSourceStack, CarpetRule carpetRule, String newValue, String userString) { + return !OPTIONS.contains(newValue) ? null : newValue; + } + + public String description() { + return "Can be limited to 'ender_chest' for use EnderChest open only, true/false for open directly/unable"; + } + } +} diff --git a/src/main/java/dev/dubhe/gugle/carpet/commands/BotCommand.java b/src/main/java/dev/dubhe/gugle/carpet/commands/BotCommand.java index 8ae8d0a..8fee722 100644 --- a/src/main/java/dev/dubhe/gugle/carpet/commands/BotCommand.java +++ b/src/main/java/dev/dubhe/gugle/carpet/commands/BotCommand.java @@ -131,7 +131,7 @@ private static int list(CommandContext context) { return 1; } - private static MutableComponent botToComponent(BotInfo botInfo) { + private static @NotNull MutableComponent botToComponent(@NotNull BotInfo botInfo) { MutableComponent desc = Component.literal(botInfo.desc).withStyle( Style.EMPTY .applyFormat(ChatFormatting.GRAY) diff --git a/src/main/java/dev/dubhe/gugle/carpet/mixin/PlayerMixin.java b/src/main/java/dev/dubhe/gugle/carpet/mixin/PlayerMixin.java index eb07e59..977c0b9 100644 --- a/src/main/java/dev/dubhe/gugle/carpet/mixin/PlayerMixin.java +++ b/src/main/java/dev/dubhe/gugle/carpet/mixin/PlayerMixin.java @@ -5,6 +5,7 @@ import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import dev.dubhe.gugle.carpet.GcaExtension; import dev.dubhe.gugle.carpet.GcaSetting; +import dev.dubhe.gugle.carpet.tools.GcaUtils; import dev.dubhe.gugle.carpet.api.tools.text.ComponentTranslate; import dev.dubhe.gugle.carpet.tools.ClientUtils; import dev.dubhe.gugle.carpet.tools.FakePlayerEnderChestContainer; @@ -16,6 +17,7 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.ChestMenu; +import org.jetbrains.annotations.NotNull; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; @@ -40,14 +42,14 @@ private void tick(CallbackInfo ci) { } @WrapOperation(method = "interactOn", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;interact(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/InteractionHand;)Lnet/minecraft/world/InteractionResult;")) - private InteractionResult interactOn(Entity entity, Player player, InteractionHand hand, Operation original) { + private InteractionResult interactOn(Entity entity, @NotNull Player player, InteractionHand hand, Operation original) { if (player.level().isClientSide()) { // 客户端在交互前要先判断一下当前交互的实体是不是玩家,这用来防止意外的使用物品功能 if (entity instanceof Player && ClientUtils.isFakePlayer(player)) { return InteractionResult.CONSUME; } } else { - if ((GcaSetting.openFakePlayerInventory || GcaSetting.openFakePlayerEnderChest) && entity instanceof EntityPlayerMPFake fakePlayer) { + if ((GcaSetting.openFakePlayerInventory || GcaUtils.openFakePlayerEnderChest(player)) && entity instanceof EntityPlayerMPFake fakePlayer) { // 打开物品栏 InteractionResult result = this.openInventory(player, fakePlayer); if (result != InteractionResult.PASS) { @@ -60,11 +62,11 @@ private InteractionResult interactOn(Entity entity, Player player, InteractionHa } @Unique - private InteractionResult openInventory(Player player, EntityPlayerMPFake fakePlayer) { + private InteractionResult openInventory(@NotNull Player player, EntityPlayerMPFake fakePlayer) { SimpleMenuProvider provider; if (player.isShiftKeyDown()) { // 打开末影箱 - if (GcaSetting.openFakePlayerEnderChest) { + if (GcaUtils.openFakePlayerEnderChest(player)) { provider = new SimpleMenuProvider( (i, inventory, p) -> ChestMenu.sixRows( i, inventory, diff --git a/src/main/java/dev/dubhe/gugle/carpet/tools/GcaUtils.java b/src/main/java/dev/dubhe/gugle/carpet/tools/GcaUtils.java new file mode 100644 index 0000000..b4d51e8 --- /dev/null +++ b/src/main/java/dev/dubhe/gugle/carpet/tools/GcaUtils.java @@ -0,0 +1,16 @@ +package dev.dubhe.gugle.carpet.tools; + +import dev.dubhe.gugle.carpet.GcaSetting; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Items; + +public class GcaUtils { + public static boolean openFakePlayerEnderChest(Player player) { + if ("true".equals(GcaSetting.openFakePlayerEnderChest)) return true; + return "ender_chest".equals(GcaSetting.openFakePlayerEnderChest) && + ( + player.getMainHandItem().is(Items.ENDER_CHEST) || + player.getOffhandItem().is(Items.ENDER_CHEST) + ); + } +}