Minecraft Forge

编程入门 行业动态 更新时间:2024-10-28 01:27:44
Minecraft Forge - 将块放置在物品上使用(Minecraft Forge - Place block onItemUse)

我试图让我的自定义项目在使用时放置水:

public class Beaker extends Item implements IHasModel { public Beaker(String name, CreativeTabs tab, int maxStackSize) { setUnlocalizedName(name); setRegistryName(name); setCreativeTab(tab); setMaxStackSize(maxStackSize); ItemInit.ITEMS.add(this); } @Override public void registerModels() { Main.proxy.registerItemRenderer(this, 0, "inventory"); } @Override public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { BlockPos clickedBlock = new BlockPos(hitX, hitY, hitZ); worldIn.setBlockState(clickedBlock, Blocks.WATER.getDefaultState()); return EnumActionResult.SUCCESS; } }

然而,当我点击右键时,该项目被使用(动画播放),但没有放置水,我使用的是Minecraft 1.12.2和Forge 14.23.2.2613。

I'm trying to make my custom item place water when used:

public class Beaker extends Item implements IHasModel { public Beaker(String name, CreativeTabs tab, int maxStackSize) { setUnlocalizedName(name); setRegistryName(name); setCreativeTab(tab); setMaxStackSize(maxStackSize); ItemInit.ITEMS.add(this); } @Override public void registerModels() { Main.proxy.registerItemRenderer(this, 0, "inventory"); } @Override public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { BlockPos clickedBlock = new BlockPos(hitX, hitY, hitZ); worldIn.setBlockState(clickedBlock, Blocks.WATER.getDefaultState()); return EnumActionResult.SUCCESS; } }

However, when I right click, the item gets used (animation plays) but the water is not placed, I'm using Minecraft 1.12.2 and Forge 14.23.2.2613.

最满意答案

hitX hitY和hitZ与执行操作的世界位置hitZ 。 它们是单击块内的部分值,用于根据块被点击的位置(例如数字键盘上的哪个按钮)执行操作。

如果我们看ItemBlockSpecial (处理Cake,Repeater,Brewing Stands和Cauldrons等项目),我们可以找到这个代码:

public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { IBlockState iblockstate = worldIn.getBlockState(pos); Block block = iblockstate.getBlock(); // snow layer stuff we don't care about if (!block.isReplaceable(worldIn, pos)) { pos = pos.offset(facing); } // placement code }

这里需要注意的重要一点是pos参数直接使用,而在被点击的块不可替换的情况下通过使用pos参数修改pos参数(例如,高草是可替换的,Stone不是)。

如果ItemBlockSpecial对于你来说不够(即你不能让你的项目成为ItemBlockSpecial一个实例),那么它用来做事情的代码可能仍然会是。

hitX hitY and hitZ have no relation to the world location where the action was performed. They are partial values within the clicked block for performing actions based on where the block was clicked (e.g. which button on a number pad).

If we look at ItemBlockSpecial (which handles items like Cake, Repeaters, Brewing Stands, and Cauldrons) we find this code:

public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { IBlockState iblockstate = worldIn.getBlockState(pos); Block block = iblockstate.getBlock(); // snow layer stuff we don't care about if (!block.isReplaceable(worldIn, pos)) { pos = pos.offset(facing); } // placement code }

The important thing to note here is that the pos parameter is used directly, while being modified by the facing parameter in the event that the block clicked is not replaceable (e.g. Tall Grass is replaceable, Stone is not).

If ItemBlockSpecial isn't sufficient for you (i.e. you can't make your item an instance of ItemBlockSpecial), then the code it uses to do things probably still will be.

更多推荐

本文发布于:2023-08-04 13:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Minecraft   Forge

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!