当我按下按钮时,使Minecraft 1.12.2中的项目执行某些操作

问题描述

因此,我一直在尝试制作靴子,使您在按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类中的init方法

@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]);
    }
}

这是我的物品的课程:

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,goToZ);
        }
        return super.onItemRightClick(worldIn,player,handIn);
    }
    
    @Override
    public void registerModels() 
    {
        Main.proxy.registerItemRenderer(this,"inventory");
    }
}

有人有什么建议吗?还是需要再上一堂课?

解决方法

您犯了两个主要错误:

  1. 不正确使用KeyInputEvent

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

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

事件处理程序类必须具有@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/