在Minecraft 1.12.2中,当我按下按钮时使物品产生作用。

huangapple go评论90阅读模式
英文:

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

有人有什么建议吗?还是我需要提供另一个类?

英文:

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(&quot;key.structure.desc&quot;, Keyboard.KEY_F, &quot;key.gameplay.category&quot;);
	keyBindings[1] = new KeyBinding(&quot;key.structure.desc&quot;, Keyboard.KEY_SPACE, &quot;key.gameplay.category&quot;);
	
	for (int i = 0; i &lt; 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&lt;ItemStack&gt; 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, &quot;inventory&quot;);
	}
}

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

答案1

得分: 0

你犯了两个主要错误:

  1. 错误使用KeyInputEvent

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

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

事件处理类必须有@EventBusSubscriber注释。

示例:

@EventBusSubscriber("你的modid")
public class HermesBoots extends ItemArmor implements IHasModel 
{    
    ...

    @SubscribeEvent
    public static void onEvent(KeyInputEvent event)
    {
        //处理键盘事件
    }

    ...
}

更多内容请参阅文档:https://mcforge.readthedocs.io/en/1.15.x/events/intro/

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

这是因为按键操作涉及客户端,而在游戏中应用某些效果涉及服务器端。
为了使服务器端能够了解按键操作,您需要从客户端向服务器发送消息(数据包)。

更多内容请参阅文档:https://mcforge.readthedocs.io/en/1.15.x/networking/

英文:

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(&quot;&lt;your modid&gt;&quot;)
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/

huangapple
  • 本文由 发表于 2020年9月27日 00:07:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64079805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定