Fixed Flower Basket
This commit is contained in:
parent
112018950c
commit
6d9cd6e9a7
5 changed files with 39 additions and 29 deletions
|
@ -28,7 +28,7 @@ public class ItemEventHandler
|
||||||
if (ItemFlowerBasket.isBasketOpen(stack))
|
if (ItemFlowerBasket.isBasketOpen(stack))
|
||||||
{
|
{
|
||||||
//Remove the itemstack from the inventory now to prevent a loop
|
//Remove the itemstack from the inventory now to prevent a loop
|
||||||
player.inventory.setItemStack(null);
|
player.inventory.setItemStack(ItemStack.EMPTY);
|
||||||
player.closeScreen();
|
player.closeScreen();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,12 +50,12 @@ public class ItemEventHandler
|
||||||
{
|
{
|
||||||
//Check if the player has a basket in their inventory, and if the stack is suitable for adding
|
//Check if the player has a basket in their inventory, and if the stack is suitable for adding
|
||||||
//to the basket
|
//to the basket
|
||||||
if (basketStack != null && ItemFlowerBasket.isStackSuitableForBasket(stack))
|
if (!basketStack.isEmpty() && ItemFlowerBasket.isStackSuitableForBasket(stack))
|
||||||
{
|
{
|
||||||
InventoryFlowerBasket inventory = new InventoryFlowerBasket(basketStack, player);
|
InventoryFlowerBasket inventory = new InventoryFlowerBasket(basketStack, player);
|
||||||
|
|
||||||
//Add the stack to the basket's inventory, if successful, don't add it to the player's regular inventory
|
//Add the stack to the basket's inventory, if successful, don't add it to the player's regular inventory
|
||||||
if (inventory.addItem(stack) == null)
|
if (inventory.addItem(stack).isEmpty())
|
||||||
{
|
{
|
||||||
//Set stack size to 0 to cause it to be removed
|
//Set stack size to 0 to cause it to be removed
|
||||||
stack.setCount(0);
|
stack.setCount(0);
|
||||||
|
|
|
@ -18,6 +18,8 @@ import net.minecraft.inventory.IInventory;
|
||||||
import net.minecraft.inventory.Slot;
|
import net.minecraft.inventory.Slot;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
public class ContainerFlowerBasket extends Container
|
public class ContainerFlowerBasket extends Container
|
||||||
{
|
{
|
||||||
private static final int PLAYER_ROWS = 3;
|
private static final int PLAYER_ROWS = 3;
|
||||||
|
@ -76,9 +78,10 @@ public class ContainerFlowerBasket extends Container
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Nonnull
|
||||||
public ItemStack transferStackInSlot(EntityPlayer player, int index)
|
public ItemStack transferStackInSlot(EntityPlayer player, int index)
|
||||||
{
|
{
|
||||||
ItemStack oldStack = null;
|
ItemStack oldStack = ItemStack.EMPTY;
|
||||||
Slot slot = (Slot)this.inventorySlots.get(index);
|
Slot slot = (Slot)this.inventorySlots.get(index);
|
||||||
|
|
||||||
//Ensure there is a slot at this index and it has an item in it
|
//Ensure there is a slot at this index and it has an item in it
|
||||||
|
@ -91,17 +94,17 @@ public class ContainerFlowerBasket extends Container
|
||||||
{
|
{
|
||||||
if (!this.mergeItemStack(mergedStack, INVENTORY_ROWS * 9, this.inventorySlots.size(), true))
|
if (!this.mergeItemStack(mergedStack, INVENTORY_ROWS * 9, this.inventorySlots.size(), true))
|
||||||
{
|
{
|
||||||
return null;
|
return ItemStack.EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!this.mergeItemStack(mergedStack, 0, INVENTORY_ROWS * 9, false))
|
else if (!this.mergeItemStack(mergedStack, 0, INVENTORY_ROWS * 9, false))
|
||||||
{
|
{
|
||||||
return null;
|
return ItemStack.EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mergedStack.getCount() == 0)
|
if (mergedStack.getCount() == 0)
|
||||||
{
|
{
|
||||||
slot.putStack((ItemStack)null);
|
slot.putStack(ItemStack.EMPTY);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -120,7 +123,7 @@ public class ContainerFlowerBasket extends Container
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isItemValid(ItemStack stack)
|
public boolean isItemValid(@Nonnull ItemStack stack)
|
||||||
{
|
{
|
||||||
return ItemFlowerBasket.isStackSuitableForBasket(stack);
|
return ItemFlowerBasket.isStackSuitableForBasket(stack);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,8 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraft.nbt.NBTTagList;
|
import net.minecraft.nbt.NBTTagList;
|
||||||
import net.minecraftforge.common.util.Constants;
|
import net.minecraftforge.common.util.Constants;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
public class InventoryFlowerBasket extends InventoryBasic
|
public class InventoryFlowerBasket extends InventoryBasic
|
||||||
{
|
{
|
||||||
public static final int INVENTORY_ROWS = 2;
|
public static final int INVENTORY_ROWS = 2;
|
||||||
|
@ -26,9 +28,9 @@ public class InventoryFlowerBasket extends InventoryBasic
|
||||||
|
|
||||||
private EntityPlayer player;
|
private EntityPlayer player;
|
||||||
|
|
||||||
private ItemStack ownerStack;
|
private ItemStack ownerStack = ItemStack.EMPTY;
|
||||||
|
|
||||||
public InventoryFlowerBasket(ItemStack ownerStack, EntityPlayer player)
|
public InventoryFlowerBasket(@Nonnull ItemStack ownerStack, EntityPlayer player)
|
||||||
{
|
{
|
||||||
super("container.flower_basket", false, INVENTORY_ROWS * INVENTORY_COLUMNS);
|
super("container.flower_basket", false, INVENTORY_ROWS * INVENTORY_COLUMNS);
|
||||||
|
|
||||||
|
@ -37,7 +39,7 @@ public class InventoryFlowerBasket extends InventoryBasic
|
||||||
//Load only on the server
|
//Load only on the server
|
||||||
ItemStack basketStack = ownerStack;
|
ItemStack basketStack = ownerStack;
|
||||||
|
|
||||||
if (basketStack == null) basketStack = player.getHeldItem(PlayerUtil.getHandForItemAndMeta(player, BOPItems.flower_basket, 0));
|
if (basketStack.isEmpty()) basketStack = player.getHeldItem(PlayerUtil.getHandForItemAndMeta(player, BOPItems.flower_basket, 0));
|
||||||
else this.ownerStack = basketStack;
|
else this.ownerStack = basketStack;
|
||||||
|
|
||||||
NBTTagCompound invData = NBTUtil.getOrCreateStackNBT(basketStack);
|
NBTTagCompound invData = NBTUtil.getOrCreateStackNBT(basketStack);
|
||||||
|
@ -46,7 +48,7 @@ public class InventoryFlowerBasket extends InventoryBasic
|
||||||
|
|
||||||
public InventoryFlowerBasket(EntityPlayer player)
|
public InventoryFlowerBasket(EntityPlayer player)
|
||||||
{
|
{
|
||||||
this(null, player);
|
this(ItemStack.EMPTY, player);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -56,7 +58,7 @@ public class InventoryFlowerBasket extends InventoryBasic
|
||||||
ItemStack basketStack = getBasketStack();
|
ItemStack basketStack = getBasketStack();
|
||||||
|
|
||||||
//There's no point continuing if there's nothing to save to
|
//There's no point continuing if there's nothing to save to
|
||||||
if (basketStack != null)
|
if (!basketStack.isEmpty())
|
||||||
{
|
{
|
||||||
NBTTagCompound currentData = new NBTTagCompound();
|
NBTTagCompound currentData = new NBTTagCompound();
|
||||||
//Overwrite relevant data in the compound with updated data
|
//Overwrite relevant data in the compound with updated data
|
||||||
|
@ -99,7 +101,7 @@ public class InventoryFlowerBasket extends InventoryBasic
|
||||||
//Iterate over all valid slot indexes
|
//Iterate over all valid slot indexes
|
||||||
for (int slotIndex = 0; slotIndex < this.getSizeInventory(); ++slotIndex)
|
for (int slotIndex = 0; slotIndex < this.getSizeInventory(); ++slotIndex)
|
||||||
{
|
{
|
||||||
if (this.getStackInSlot(slotIndex) != null)
|
if (!this.getStackInSlot(slotIndex).isEmpty())
|
||||||
{
|
{
|
||||||
//Create a new item tag and populate it with data
|
//Create a new item tag and populate it with data
|
||||||
NBTTagCompound itemTag = new NBTTagCompound();
|
NBTTagCompound itemTag = new NBTTagCompound();
|
||||||
|
@ -122,8 +124,9 @@ public class InventoryFlowerBasket extends InventoryBasic
|
||||||
compound.setBoolean("BasketOpen", ItemFlowerBasket.isBasketOpen(getBasketStack()));
|
compound.setBoolean("BasketOpen", ItemFlowerBasket.isBasketOpen(getBasketStack()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
private ItemStack getBasketStack()
|
private ItemStack getBasketStack()
|
||||||
{
|
{
|
||||||
return this.ownerStack != null ? this.ownerStack : ItemFlowerBasket.findOpenBasketStack(this.player);
|
return !this.ownerStack.isEmpty() ? this.ownerStack : ItemFlowerBasket.findOpenBasketStack(this.player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,8 @@ import net.minecraftforge.common.IShearable;
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
import net.minecraftforge.fml.relauncher.Side;
|
||||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
public class ItemFlowerBasket extends Item
|
public class ItemFlowerBasket extends Item
|
||||||
{
|
{
|
||||||
public ItemFlowerBasket()
|
public ItemFlowerBasket()
|
||||||
|
@ -38,14 +40,14 @@ public class ItemFlowerBasket extends Item
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
@SideOnly(Side.CLIENT)
|
@SideOnly(Side.CLIENT)
|
||||||
public float apply(ItemStack stack, World world, EntityLivingBase entity)
|
public float apply(@Nonnull ItemStack stack, World world, EntityLivingBase entity)
|
||||||
{
|
{
|
||||||
InventoryFlowerBasket inventory = new InventoryFlowerBasket(stack, null);
|
InventoryFlowerBasket inventory = new InventoryFlowerBasket(stack, null);
|
||||||
boolean filled = false;
|
boolean filled = false;
|
||||||
|
|
||||||
for (int index = 0; index < inventory.getSizeInventory(); ++index)
|
for (int index = 0; index < inventory.getSizeInventory(); ++index)
|
||||||
{
|
{
|
||||||
if (inventory.getStackInSlot(index) != null)
|
if (!inventory.getStackInSlot(index).isEmpty())
|
||||||
{
|
{
|
||||||
filled = true;
|
filled = true;
|
||||||
break;
|
break;
|
||||||
|
@ -79,20 +81,22 @@ public class ItemFlowerBasket extends Item
|
||||||
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
|
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
public static ItemStack findBasketStack(EntityPlayer player)
|
public static ItemStack findBasketStack(EntityPlayer player)
|
||||||
{
|
{
|
||||||
//Search every item in the player's main inventory for a basket
|
//Search every item in the player's main inventory for a basket
|
||||||
for (ItemStack stack : player.inventory.mainInventory)
|
for (ItemStack stack : player.inventory.mainInventory)
|
||||||
{
|
{
|
||||||
if (stack != null && stack.getItem() instanceof ItemFlowerBasket)
|
if (!stack.isEmpty() && stack.getItem() instanceof ItemFlowerBasket)
|
||||||
{
|
{
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return ItemStack.EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
public static ItemStack findOpenBasketStack(EntityPlayer player)
|
public static ItemStack findOpenBasketStack(EntityPlayer player)
|
||||||
{
|
{
|
||||||
//Search every item in the player's main inventory for a basket
|
//Search every item in the player's main inventory for a basket
|
||||||
|
@ -104,12 +108,12 @@ public class ItemFlowerBasket extends Item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return ItemStack.EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isBasketOpen(ItemStack stack)
|
public static boolean isBasketOpen(@Nonnull ItemStack stack)
|
||||||
{
|
{
|
||||||
if (stack != null && stack.getItem() instanceof ItemFlowerBasket && stack.hasTagCompound())
|
if (!stack.isEmpty() && stack.getItem() instanceof ItemFlowerBasket && stack.hasTagCompound())
|
||||||
{
|
{
|
||||||
NBTTagCompound compound = stack.getTagCompound();
|
NBTTagCompound compound = stack.getTagCompound();
|
||||||
|
|
||||||
|
@ -131,7 +135,7 @@ public class ItemFlowerBasket extends Item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void closeIfBasket(ItemStack stack)
|
public static void closeIfBasket(@Nonnull ItemStack stack)
|
||||||
{
|
{
|
||||||
//Validate to ensure the stack is a basket and it is open
|
//Validate to ensure the stack is a basket and it is open
|
||||||
if (isBasketOpen(stack))
|
if (isBasketOpen(stack))
|
||||||
|
@ -142,11 +146,11 @@ public class ItemFlowerBasket extends Item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isStackSuitableForBasket(ItemStack stack)
|
public static boolean isStackSuitableForBasket(@Nonnull ItemStack stack)
|
||||||
{
|
{
|
||||||
Item item = stack.getItem();
|
Item item = stack.getItem();
|
||||||
Block block = Block.getBlockFromItem(item);
|
Block block = Block.getBlockFromItem(item);
|
||||||
|
|
||||||
return !(item instanceof ItemFlowerBasket) && block != null && (block instanceof IPlantable || block instanceof IGrowable || block instanceof IShearable);
|
return !(item instanceof ItemFlowerBasket) && (block instanceof IPlantable || block instanceof IGrowable || block instanceof IShearable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ public class PlayerUtil
|
||||||
{
|
{
|
||||||
ItemStack heldStack = player.getHeldItem(hand);
|
ItemStack heldStack = player.getHeldItem(hand);
|
||||||
|
|
||||||
if (heldStack != null && heldStack.getItem() == item && heldStack.getMetadata() == meta)
|
if (!heldStack.isEmpty() && heldStack.getItem() == item && heldStack.getMetadata() == meta)
|
||||||
return hand;
|
return hand;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue