Fix invalid index when ticking itemstacks in a player nventory by adding a new onInventoryTick event. Closes #9453

This commit is contained in:
LexManos committed 2023-07-14 15:42:13 -07:00
1 parent 0c81a71308
commit b741df5f18
2 files changed
+28

No files matched your search

@@ -330,10 +330,28 @@ public interface IForgeItem
/**
* Called to tick armor in the armor slot. Override to do something
*/
@Deprecated(forRemoval = true, since = "1.20.1") // Use onInventoryTick
default void onArmorTick(ItemStack stack, Level level, Player player)
{
}
/**
* Called to tick this items in a players inventory, the indexes are the global slot index.
*/
default void onInventoryTick(ItemStack stack, Level level, Player player, int slotIndex, int selectedIndex)
{
// For compatibility reasons we have to use non-local index values, I think this is a vanilla bug but lets maintain compatibility
int vanillaIndex = slotIndex;
if (slotIndex >= 36) {
vanillaIndex -= 36;
if (vanillaIndex >= 4)
vanillaIndex -= 4;
else
onArmorTick(stack, level, player);
}
stack.inventoryTick(level, player, vanillaIndex, selectedIndex == vanillaIndex);
}
/**
* Determines if the specific ItemStack can be placed in the specified armor
* slot, for the entity.
@@ -292,11 +292,21 @@ public interface IForgeItemStack extends ICapabilitySerializable<CompoundTag>
/**
* Called to tick armor in the armor slot. Override to do something
*/
@Deprecated(forRemoval = true, since = "1.20.1") // Use onInventoryTick
default void onArmorTick(Level level, Player player)
{
self().getItem().onArmorTick(self(), level, player);
}
/**
* Called to tick this items in a players inventory, the indexes are the global slot index.
*/
default void onInventoryTick(Level level, Player player, int slotIndex, int selectedIndex)
{
self().getItem().onInventoryTick(self(), level, player, slotIndex, selectedIndex);
}
/**
* Called every tick from {@code Horse#playGallopSound(SoundEvent)} on the item in the
* armor slot.