Fix conjured items not disappearing in offhand (actually fixes #493)

This commit is contained in:
Electroblob77
2021-01-04 00:18:37 +00:00
parent f81ad670ed
commit 0e815c676d
8 changed files with 40 additions and 8 deletions
@@ -1,5 +1,6 @@
package electroblob.wizardry.util;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
@@ -10,6 +11,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumHandSide;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import java.util.ArrayList;
import java.util.Arrays;
@@ -142,4 +144,28 @@ public final class InventoryUtils {
&& ItemStack.areItemStackTagsEqual(stack1, stack2);
}
/**
* A version of {@link Entity#replaceItemInInventory(int, ItemStack)} that takes a slot index specific to the main,
* armour and offhand inventories, as passed to {@link Item#onUpdate(ItemStack, World, Entity, int, boolean)},
* rather than the proper slot index used everywhere else (just <i>why</i>, Mojang?).
* @param entity The entity to replace the item for
* @param slot The slot index to replace
* @param original The item stack currently in the slot (required for technical reasons)
* @param replacement The new item stack
* @return True if an item was replaced, false if not
*/
public static boolean replaceItemInInventory(Entity entity, int slot, ItemStack original, ItemStack replacement){
// Check slots that aren't in the main inventory first by comparing with the existing item
if(entity instanceof EntityLivingBase){
for(EntityEquipmentSlot eslot : EntityEquipmentSlot.values()){
if(((EntityLivingBase)entity).getItemStackFromSlot(eslot) == original){
entity.setItemStackToSlot(eslot, replacement);
return true;
}
}
}
// Otherwise use the normal behaviour
return entity.replaceItemInInventory(slot, replacement);
}
}