当我按下按钮时,让 Minecraft 1.12.2 中的物品做一些事情

Making items in Minecraft 1.12.2 do something when I press a button

所以我一直在尝试制作这样的靴子,当您按下 F 键时,它会让您向前冲几个街区,所以我尝试制作一个自定义键绑定。问题是,按键绑定不起作用。另一种方法是在按 Ctrl 时执行操作,但我也不知道该怎么做。 这是我的 ClientProxy:

public class ClientProxy extends CommonProxy 
{
    public static KeyBinding[] keyBindings;

    public void registerItemRenderer(Item item, int meta, String id) 
    {
        ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(item.getRegistryName(), id));
    }
}

这是我的 Main 中的初始化方法 class:

@EventHandler
public static void init(FMLInitializationEvent event)
{
    ModRecipes.init();
    NetworkRegistry.INSTANCE.registerGuiHandler(Main.instance, new GuiHandler());
    
    KeyBinding[] keyBindings = new KeyBinding[2];
    
    keyBindings[0] = new KeyBinding("key.structure.desc", Keyboard.KEY_F, "key.gameplay.category");
    keyBindings[1] = new KeyBinding("key.structure.desc", Keyboard.KEY_SPACE, "key.gameplay.category");
    
    for (int i = 0; i < keyBindings.length; ++i)
    {
        ClientRegistry.registerKeyBinding(keyBindings[i]);
    }
}

这是我的商品的 class:

public class HermesBoots extends ItemArmor implements IHasModel 
{

    static boolean FIsDown;
    KeyBinding[] keyBindings = ClientProxy.keyBindings;
    
    public HermesBoots(String name, ArmorMaterial materialIn, int renderIndexIn, EntityEquipmentSlot equipmentSlotIn) 
    {
        super(materialIn, renderIndexIn, equipmentSlotIn);
        setUnlocalizedName(name);
        setRegistryName(name);
        setCreativeTab(Main.MODDEDRESOURCESTAB);
        
        ModItems.ITEMS.add(this);
    }
    
    public static void onEvent(KeyInputEvent event, EntityPlayer player)
    {
        KeyBinding[] keyBindings = ClientProxy.keyBindings;
        if (keyBindings[0].isPressed()) 
        {
            Vec3d look = player.getLookVec();
            double goToX = look.x * 1;
            double goToY = 0.4;
            double goToZ = look.z * 1;
            if(player.isAirBorne|| player.onGround)
            {
                player.setVelocity(goToX, goToY, goToZ);
            }
        }
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer player, EnumHand handIn) 
    {
        Vec3d look = player.getLookVec();
        player.getCooldownTracker().setCooldown(this, 23);
        double goToX = look.x * 1;
        double goToY = 0.4;
        double goToZ = look.z * 1;
        if(player.isAirBorne || player.onGround)
        {
            player.setVelocity(goToX, goToY, goToZ);
        }
        return super.onItemRightClick(worldIn, player, handIn);
    }
    
    @Override
    public void registerModels() 
    {
        Main.proxy.registerItemRenderer(this, 0, "inventory");
    }
}

有人有什么建议吗?或者我需要再给一个 class?

你犯了两个主要错误:

  1. KeyInputEvent 的使用不正确

事件处理程序方法必须有一个参数 - 正在处理的事件。

事件处理程序方法必须具有@SubscribeEvent 注释。

事件处理程序 class 必须具有 @EventBusSubscriber 注释。

示例:

@EventBusSubscriber("<your modid>")
public class HermesBoots extends ItemArmor implements IHasModel 
{    
    ...

    @SubscribeEvent
    public static void onEvent(KeyInputEvent event)
    {
        //handle keyboard event
    }

    ...
}

更多文档:https://mcforge.readthedocs.io/en/1.15.x/events/intro/

  1. 在客户端设置玩家速度

这是因为按键是客户端相关的,而游戏中的一些效果的应用是服务端相关的。 为了让服务器端知道按键,您需要从客户端向服务器发送消息(数据包)。

更多文档:https://mcforge.readthedocs.io/en/1.15.x/networking/