Search code examples
javaeclipseminecraftkey-bindingsminecraft-forge

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


So I've been trying to make boots that make you dash forward a few blocks when you press F, so I was trying to make a custom keybind. thing is, the keybinds don't work. An alternative was to do the action when pressing Ctrl, but I don't know how to do that either. This is my 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));
    }
}

This is the init method in my 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]);
    }
}

And this is the class for my item:

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");
    }
}

Anybody have any suggestions? or do I need to give another class?


Solution

  • You made two main mistakes:

    1. Incorrect using of KeyInputEvent

    Event handler method must have one argument - event being handled.

    Event handler method must have @SubscribeEvent annotation.

    Event handler class must have @EventBusSubscriber annotation.

    Example:

    @EventBusSubscriber("<your modid>")
    public class HermesBoots extends ItemArmor implements IHasModel 
    {    
        ...
    
        @SubscribeEvent
        public static void onEvent(KeyInputEvent event)
        {
            //handle keyboard event
        }
    
        ...
    }
    

    More in docs: https://mcforge.readthedocs.io/en/1.15.x/events/intro/

    1. Setting player velocity on client side

    This is due to the fact that the keypress is related to the client side, and the application of some effect in the game relates to the server side. In order for the server side can know about the keypress, you need to send the message(packet) from client to server.

    More in docs: https://mcforge.readthedocs.io/en/1.15.x/networking/