Skip to content

Commit

Permalink
更新: 可以通过gui设置命令碎片的命令
Browse files Browse the repository at this point in the history
Took 23 seconds
  • Loading branch information
xkball committed Oct 4, 2024
1 parent 6a8a54d commit db00e21
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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));
}
}
27 changes: 23 additions & 4 deletions src/main/java/org/teacon/powertool/item/CommandRune.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand All @@ -32,13 +40,17 @@ public int getUseDuration(ItemStack stack, LivingEntity entity) {
}

@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand usedHand) {
ItemStack held = player.getItemInHand(usedHand);
public InteractionResultHolder<ItemStack> 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);
}

Expand All @@ -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<Screen> getScreenSupplier(ItemStack stack, EquipmentSlot slot) {
return () -> new SetCommandScreen(stack,slot);
}
}

0 comments on commit db00e21

Please sign in to comment.