尝试检测玩家在我的 Minecraft 中的物品栏中是否有特定物品 mod
Trying to detect if the player has a specific item in their inventory in my Minecraft mod
我正在制作我的 Minecraft mod,我正在尝试检测玩家是否有特定物品,如果你有,它会给你带来效果。我试图通过使用 PlayerTickEvent 来做到这一点,但我不知道如何使用它,因为我以前没有使用过它。我使用了一个功能来检查之前哪个插槽被更改为附魔,所以我试过了,但它没有用。有任何想法吗?这是我最初尝试的代码:
@SubscribeEvent
public void testItemFunction(LivingEquipmentChangeEvent event)
{
boolean itemIsInInventory;
Object player = event.getEntityLiving();
if (event.getEntityLiving() instanceof EntityLivingBase)
{
EntityEquipmentSlot slotChanged = event.getSlot();
if (slotChanged.getSlotIndex() > -1 && slotChanged.getSlotIndex() < 36)
{
if(slotChanged != null && slotChanged.() == ModItems.TEST_ITEM)
{
itemIsInInventory = true;
}
}
else
{
itemIsInInventory = false;
}
}
我没有设置和删除效果,而是更改了变量 itemIsInInventory,因为我在我的 util 包的 class 中制作了这个方法,所以我可以随时调用它而不是复制粘贴方法
我找到了一个更简单的答案,但它可能不适用于某些事情。我不久前发现了这个,但它似乎没有用,所以我寻找另一种解决方案,但现在我回来了,我意识到我做错了。这是代码:
@Override
public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag)
{
//Runs the method every tick the item is in the player's inventory
((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.INVISIBILITY, 10, 0, false, false));
//Gives the entity with the item the invisibility effect for half a second every tick
}
简单地说,它不检测玩家是否拥有物品,而是只在玩家拥有物品时运行代码。我所说的“我做错了”的意思是,这段代码应该放在物品的 class 中,这样它只会在物品在您的库存中时触发。我不确定,但我不认为如果将物品交给僵尸,这会起作用。
我正在制作我的 Minecraft mod,我正在尝试检测玩家是否有特定物品,如果你有,它会给你带来效果。我试图通过使用 PlayerTickEvent 来做到这一点,但我不知道如何使用它,因为我以前没有使用过它。我使用了一个功能来检查之前哪个插槽被更改为附魔,所以我试过了,但它没有用。有任何想法吗?这是我最初尝试的代码:
@SubscribeEvent
public void testItemFunction(LivingEquipmentChangeEvent event)
{
boolean itemIsInInventory;
Object player = event.getEntityLiving();
if (event.getEntityLiving() instanceof EntityLivingBase)
{
EntityEquipmentSlot slotChanged = event.getSlot();
if (slotChanged.getSlotIndex() > -1 && slotChanged.getSlotIndex() < 36)
{
if(slotChanged != null && slotChanged.() == ModItems.TEST_ITEM)
{
itemIsInInventory = true;
}
}
else
{
itemIsInInventory = false;
}
}
我没有设置和删除效果,而是更改了变量 itemIsInInventory,因为我在我的 util 包的 class 中制作了这个方法,所以我可以随时调用它而不是复制粘贴方法
我找到了一个更简单的答案,但它可能不适用于某些事情。我不久前发现了这个,但它似乎没有用,所以我寻找另一种解决方案,但现在我回来了,我意识到我做错了。这是代码:
@Override
public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag)
{
//Runs the method every tick the item is in the player's inventory
((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.INVISIBILITY, 10, 0, false, false));
//Gives the entity with the item the invisibility effect for half a second every tick
}
简单地说,它不检测玩家是否拥有物品,而是只在玩家拥有物品时运行代码。我所说的“我做错了”的意思是,这段代码应该放在物品的 class 中,这样它只会在物品在您的库存中时触发。我不确定,但我不认为如果将物品交给僵尸,这会起作用。