Skip to content

Instantly share code, notes, and snippets.

@matshou
Created October 17, 2016 23:35
Show Gist options
  • Save matshou/e117ade23d3f59595287928cd1cb8b5a to your computer and use it in GitHub Desktop.
Save matshou/e117ade23d3f59595287928cd1cb8b5a to your computer and use it in GitHub Desktop.
Sample code change after ItemStack#setItem deprecation
public static boolean extinguishItemTorch(ItemStack stack, Entity owner, int itemSlot, boolean extinguishByWater)
{
if (extinguishByWater == true)
setItemHumidity(stack.getTagCompound(), SharedDefines.TORCH_HUMIDITY_THRESHOLD);
if (ItemTorch.isItemTorch(stack.getItem(), false))
{
ItemStack unlitTorch = new ItemStack(ResourceLibrary.TORCH_UNLIT, 1);
ItemTorch.createCustomItemNBTFromExisting(unlitTorch, owner.worldObj, stack.getTagCompound());
if (owner instanceof EntityPlayer)
{
EntityPlayer playerOwner = (EntityPlayer) owner;
final int mainInventorySize = playerOwner.inventory.mainInventory.length;
if (itemSlot < 0) // Search all player inventories to find the stack.
{
if (ItemStack.areItemStacksEqual(playerOwner.getHeldItemOffhand(), stack))
{
playerOwner.setHeldItem(EnumHand.OFF_HAND, unlitTorch);
return true;
}
else for (int i = 0; i < mainInventorySize; i++)
{
if (ItemStack.areItemStacksEqual(playerOwner.inventory.getStackInSlot(i), stack))
{
if (playerOwner.replaceItemInInventory(i, unlitTorch))
return true;
}
}
}
else if (itemSlot < mainInventorySize) // Check for specific item slot
{
if (itemSlot == 0) // Could be an OFF_HAND slot
{
if (ItemStack.areItemStacksEqual(playerOwner.getHeldItemOffhand(), stack))
{
playerOwner.setHeldItem(EnumHand.OFF_HAND, unlitTorch);
return true;
}
}
if (ItemStack.areItemStacksEqual(playerOwner.inventory.getStackInSlot(itemSlot), stack))
{
if (owner.replaceItemInInventory(itemSlot, unlitTorch))
return true;
}
else Logger.error("Failed to extinguish ItemTorch, item was not found in inventory.", new java.util.NoSuchElementException());
}
else Logger.error("Failed to extinguish ItemTorch, itemSlot value is invalid.", new java.lang.IllegalArgumentException());
}
else if (owner instanceof EntityItem)
{
((EntityItem) owner).setEntityItemStack(unlitTorch);
return true;
}
else Logger.error("Failed to extinguish ItemTorch, owner is not EntityPlayer or EntityItem.", new java.lang.IllegalArgumentException());
}
else Logger.error("Failed to extinguish ItemTorch, item is not a torch.", new java.lang.TypeNotPresentException("ItemTorch", null));
return false;
}
public static void extinguishItemTorch(ItemStack stack, boolean extinguishByWater)
{
if (stack != null && ItemTorch.isItemTorch(stack.getItem(), false))
stack.setItem(Item.getItemFromBlock(ResourceLibrary.TORCH_UNLIT));
else Logger.error("Failed to extinguish ItemTorch.", stack == null ?
new NullPointerException("ItemStack was passed as null,") : new java.lang.TypeNotPresentException("ItemTorch", null));
if (extinguishByWater == true)
setItemHumidity(stack.getTagCompound(), SharedDefines.TORCH_HUMIDITY_THRESHOLD);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment