Merge pull request #907 from GirafiStudios/flower_basket

Fixed Flower Basket
This commit is contained in:
Adubbz 2016-11-22 10:21:59 +11:00 committed by GitHub
commit 1218bfe35a
5 changed files with 39 additions and 29 deletions

View File

@ -28,7 +28,7 @@ public class ItemEventHandler
if (ItemFlowerBasket.isBasketOpen(stack))
{
//Remove the itemstack from the inventory now to prevent a loop
player.inventory.setItemStack(null);
player.inventory.setItemStack(ItemStack.EMPTY);
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
//to the basket
if (basketStack != null && ItemFlowerBasket.isStackSuitableForBasket(stack))
if (!basketStack.isEmpty() && ItemFlowerBasket.isStackSuitableForBasket(stack))
{
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
if (inventory.addItem(stack) == null)
if (inventory.addItem(stack).isEmpty())
{
//Set stack size to 0 to cause it to be removed
stack.setCount(0);

View File

@ -18,6 +18,8 @@ import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull;
public class ContainerFlowerBasket extends Container
{
private static final int PLAYER_ROWS = 3;
@ -76,9 +78,10 @@ public class ContainerFlowerBasket extends Container
}
@Override
@Nonnull
public ItemStack transferStackInSlot(EntityPlayer player, int index)
{
ItemStack oldStack = null;
ItemStack oldStack = ItemStack.EMPTY;
Slot slot = (Slot)this.inventorySlots.get(index);
//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))
{
return null;
return ItemStack.EMPTY;
}
}
else if (!this.mergeItemStack(mergedStack, 0, INVENTORY_ROWS * 9, false))
{
return null;
return ItemStack.EMPTY;
}
if (mergedStack.getCount() == 0)
{
slot.putStack((ItemStack)null);
slot.putStack(ItemStack.EMPTY);
}
else
{
@ -120,7 +123,7 @@ public class ContainerFlowerBasket extends Container
}
@Override
public boolean isItemValid(ItemStack stack)
public boolean isItemValid(@Nonnull ItemStack stack)
{
return ItemFlowerBasket.isStackSuitableForBasket(stack);
}

View File

@ -19,6 +19,8 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;
import javax.annotation.Nonnull;
public class InventoryFlowerBasket extends InventoryBasic
{
public static final int INVENTORY_ROWS = 2;
@ -26,9 +28,9 @@ public class InventoryFlowerBasket extends InventoryBasic
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);
@ -37,7 +39,7 @@ public class InventoryFlowerBasket extends InventoryBasic
//Load only on the server
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;
NBTTagCompound invData = NBTUtil.getOrCreateStackNBT(basketStack);
@ -46,7 +48,7 @@ public class InventoryFlowerBasket extends InventoryBasic
public InventoryFlowerBasket(EntityPlayer player)
{
this(null, player);
this(ItemStack.EMPTY, player);
}
@Override
@ -56,7 +58,7 @@ public class InventoryFlowerBasket extends InventoryBasic
ItemStack basketStack = getBasketStack();
//There's no point continuing if there's nothing to save to
if (basketStack != null)
if (!basketStack.isEmpty())
{
NBTTagCompound currentData = new NBTTagCompound();
//Overwrite relevant data in the compound with updated data
@ -99,7 +101,7 @@ public class InventoryFlowerBasket extends InventoryBasic
//Iterate over all valid slot indexes
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
NBTTagCompound itemTag = new NBTTagCompound();
@ -121,9 +123,10 @@ public class InventoryFlowerBasket extends InventoryBasic
//outside of the gui
compound.setBoolean("BasketOpen", ItemFlowerBasket.isBasketOpen(getBasketStack()));
}
@Nonnull
private ItemStack getBasketStack()
{
return this.ownerStack != null ? this.ownerStack : ItemFlowerBasket.findOpenBasketStack(this.player);
return !this.ownerStack.isEmpty() ? this.ownerStack : ItemFlowerBasket.findOpenBasketStack(this.player);
}
}

View File

@ -30,6 +30,8 @@ import net.minecraftforge.common.IShearable;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nonnull;
public class ItemFlowerBasket extends Item
{
public ItemFlowerBasket()
@ -38,14 +40,14 @@ public class ItemFlowerBasket extends Item
{
@Override
@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);
boolean filled = false;
for (int index = 0; index < inventory.getSizeInventory(); ++index)
{
if (inventory.getStackInSlot(index) != null)
if (!inventory.getStackInSlot(index).isEmpty())
{
filled = true;
break;
@ -78,21 +80,23 @@ public class ItemFlowerBasket extends Item
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
}
@Nonnull
public static ItemStack findBasketStack(EntityPlayer player)
{
//Search every item in the player's main inventory for a basket
for (ItemStack stack : player.inventory.mainInventory)
{
if (stack != null && stack.getItem() instanceof ItemFlowerBasket)
if (!stack.isEmpty() && stack.getItem() instanceof ItemFlowerBasket)
{
return stack;
}
}
return null;
return ItemStack.EMPTY;
}
@Nonnull
public static ItemStack findOpenBasketStack(EntityPlayer player)
{
//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();
@ -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
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();
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);
}
}

View File

@ -26,7 +26,7 @@ public class PlayerUtil
{
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;
}