From db00e211efeac48e75e3730c42cb4efb3da3ce8d Mon Sep 17 00:00:00 2001 From: xkball <45330674+xkball@users.noreply.github.com> Date: Sat, 5 Oct 2024 00:06:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0:=20=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E9=80=9A=E8=BF=87gui=E8=AE=BE=E7=BD=AE=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E7=A2=8E=E7=89=87=E7=9A=84=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Took 23 seconds --- .../client/gui/SetCommandScreen.java | 64 +++++++++++++++++++ .../teacon/powertool/item/CommandRune.java | 27 ++++++-- 2 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/teacon/powertool/client/gui/SetCommandScreen.java diff --git a/src/main/java/org/teacon/powertool/client/gui/SetCommandScreen.java b/src/main/java/org/teacon/powertool/client/gui/SetCommandScreen.java new file mode 100644 index 0000000..8a61485 --- /dev/null +++ b/src/main/java/org/teacon/powertool/client/gui/SetCommandScreen.java @@ -0,0 +1,64 @@ +package org.teacon.powertool.client.gui; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.components.Button; +import net.minecraft.client.gui.components.EditBox; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.core.component.DataComponentPatch; +import net.minecraft.network.chat.CommonComponents; +import net.minecraft.network.chat.Component; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.item.ItemStack; +import net.neoforged.neoforge.network.PacketDistributor; +import org.teacon.powertool.item.PowerToolItems; +import org.teacon.powertool.network.server.UpdateItemStackData; + +public class SetCommandScreen extends Screen { + + protected final ItemStack itemStack; + protected final EquipmentSlot slot; + protected EditBox input; + + public SetCommandScreen(ItemStack stack, EquipmentSlot slot) { + super(Component.translatable("powertool.setcommand.gui")); + this.itemStack = stack; + this.slot = slot; + } + + @Override + protected void init() { + var mc = Minecraft.getInstance(); + var font = mc.font; + this.addRenderableWidget(new Button.Builder(CommonComponents.GUI_DONE, btn -> this.onDone()) + .pos(this.width / 2 - 100, this.height / 4 + 120) + .size(200, 20).build()); + var box_l = (int)Math.max(100,width*0.4); + this.input = new EditBox(font,width/2-box_l/2,height/2-35,box_l,20,Component.empty()); + this.input.setMaxLength(114514); + this.input.setFocused(false); + this.input.setCanLoseFocus(true); + + String command = itemStack.get(PowerToolItems.COMMAND); + if (command != null) { + this.input.setValue(command); + } + + this.addRenderableWidget(this.input); + super.init(); + } + + protected void onDone() { + if (this.minecraft != null) { + this.minecraft.setScreen(null); + } + } + + @Override + public void removed() { + if(input == null) return; + var patch = DataComponentPatch.builder() + .set(PowerToolItems.COMMAND.get(),input.getValue()) + .build(); + PacketDistributor.sendToServer(new UpdateItemStackData(slot,patch)); + } +} diff --git a/src/main/java/org/teacon/powertool/item/CommandRune.java b/src/main/java/org/teacon/powertool/item/CommandRune.java index a8fc543..2e62ac7 100644 --- a/src/main/java/org/teacon/powertool/item/CommandRune.java +++ b/src/main/java/org/teacon/powertool/item/CommandRune.java @@ -1,6 +1,8 @@ package org.teacon.powertool.item; import net.minecraft.MethodsReturnNonnullByDefault; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.EquipmentSlot; @@ -10,13 +12,19 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.UseAnim; import net.minecraft.world.level.Level; +import net.neoforged.api.distmarker.Dist; +import net.neoforged.api.distmarker.OnlyIn; +import net.neoforged.neoforge.network.PacketDistributor; +import org.teacon.powertool.client.gui.SetCommandScreen; +import org.teacon.powertool.network.client.OpenItemScreen; import org.teacon.powertool.utils.VanillaUtils; import javax.annotation.ParametersAreNonnullByDefault; +import java.util.function.Supplier; @MethodsReturnNonnullByDefault @ParametersAreNonnullByDefault -public class CommandRune extends Item { +public class CommandRune extends Item implements IScreenProviderItem{ public CommandRune(Properties properties) { super(properties); } @@ -32,13 +40,17 @@ public int getUseDuration(ItemStack stack, LivingEntity entity) { } @Override - public InteractionResultHolder use(Level level, Player player, InteractionHand usedHand) { - ItemStack held = player.getItemInHand(usedHand); + public InteractionResultHolder use(Level level, Player player, InteractionHand hand) { + ItemStack held = player.getItemInHand(hand); String command = held.get(PowerToolItems.COMMAND); if (command == null) { + if(player instanceof ServerPlayer serverPlayer){ + PacketDistributor.sendToPlayer(serverPlayer, + new OpenItemScreen(player.getItemInHand(hand),hand == InteractionHand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND)); + } return InteractionResultHolder.pass(held); } - player.startUsingItem(usedHand); + player.startUsingItem(hand); return InteractionResultHolder.consume(held); } @@ -54,7 +66,14 @@ public ItemStack finishUsingItem(ItemStack stack, Level level, LivingEntity livi ? EquipmentSlot.OFFHAND : EquipmentSlot.MAINHAND; stack.hurtAndBreak(1, livingEntity, slot); } + } return stack; } + + @Override + @OnlyIn(Dist.CLIENT) + public Supplier getScreenSupplier(ItemStack stack, EquipmentSlot slot) { + return () -> new SetCommandScreen(stack,slot); + } }