Fix #641 Item Inventory Dupe Bug. Refactor Inventories

This commit is contained in:
mezz committed 2015-05-21 00:30:35 -07:00
1 parent 6b824f47cd
commit 93a6c77eba
82 files changed
+719 -1258

No files matched your search

@@ -10,7 +10,11 @@
******************************************************************************/
package forestry.storage;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ContainerPlayer;
import net.minecraft.item.ItemStack;
@@ -18,9 +22,7 @@ import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.eventhandler.Event;
import forestry.api.storage.BackpackManager;
import forestry.api.storage.BackpackResupplyEvent;
import forestry.api.storage.IBackpackDefinition;
import forestry.core.interfaces.IResupplyHandler;
import forestry.core.inventory.ItemInventory;
import forestry.core.inventory.ItemInventoryBackpack;
@@ -28,6 +30,16 @@ import forestry.storage.items.ItemBackpack;
public class ResupplyHandler implements IResupplyHandler {
private static List<ItemStack> backpacks(InventoryPlayer playerInventory) {
List<ItemStack> backpacks = new ArrayList<ItemStack>();
for (ItemStack itemStack : playerInventory.mainInventory) {
if (itemStack != null && itemStack.stackSize > 0 && (itemStack.getItem() instanceof ItemBackpack)) {
backpacks.add(itemStack);
}
}
return backpacks;
}
@Override
public void resupply(EntityPlayer player) {
@@ -36,24 +48,7 @@ public class ResupplyHandler implements IResupplyHandler {
return;
}
for (IBackpackDefinition backpack : BackpackManager.definitions.values()) {
resupply(backpack, player);
}
}
public void resupply(IBackpackDefinition backpackItem, EntityPlayer player) {
// Get all backpacks of this type in the player's inventory
for (ItemStack backpack : player.inventory.mainInventory) {
if (backpack == null || backpack.stackSize <= 0) {
continue;
}
if (!(backpack.getItem() instanceof ItemBackpack)) {
continue;
}
for (ItemStack backpack : backpacks(player.inventory)) {
// Only handle those in resupply mode
if (ItemBackpack.getMode(backpack) != BackpackMode.RESUPPLY) {
@@ -67,49 +62,37 @@ public class ResupplyHandler implements IResupplyHandler {
}
// Load their inventory
ItemBackpack packItem = ((ItemBackpack) backpack.getItem());
ItemInventory backpackinventory = new ItemInventoryBackpack(ItemBackpack.class, packItem.getBackpackSize(), backpack);
Event event = new BackpackResupplyEvent(player, packItem.getDefinition(), backpackinventory);
ItemBackpack backpackItem = ((ItemBackpack) backpack.getItem());
ItemInventory backpackInventory = new ItemInventoryBackpack(player, backpackItem.getBackpackSize(), backpack);
Event event = new BackpackResupplyEvent(player, backpackItem.getDefinition(), backpackInventory);
MinecraftForge.EVENT_BUS.post(event);
if (event.isCanceled()) {
continue;
}
boolean inventoryChanged = false;
// Cycle through their contents
for (int i = 0; i < backpackinventory.getSizeInventory(); i++) {
for (int i = 0; i < backpackInventory.getSizeInventory(); i++) {
ItemStack packstack = backpackinventory.getStackInSlot(i);
if (packstack == null) {
continue;
}
if (packstack.stackSize <= 0) {
ItemStack itemStack = backpackInventory.getStackInSlot(i);
if (itemStack == null || itemStack.stackSize <= 0) {
continue;
}
// Try to add it to the player's inventory and note any change
boolean change = topOffPlayerInventory(player, packstack);
if (!inventoryChanged) {
inventoryChanged = change;
}
boolean change = topOffPlayerInventory(player, itemStack);
// Clear consumed stacks
if (packstack.stackSize <= 0) {
backpackinventory.setInventorySlotContents(i, null);
if (change) {
backpackInventory.setInventorySlotContents(i, itemStack);
}
}
// Save the backpack inventory if it changed
if (inventoryChanged) {
backpackinventory.onGuiSaved(player);
}
}
}
/**
* This tops off existing stacks in the player's inventory.
*/
private boolean topOffPlayerInventory(EntityPlayer player, ItemStack itemstack) {
private static boolean topOffPlayerInventory(EntityPlayer player, ItemStack itemstack) {
// Add to player inventory first, if there is an incomplete stack in
// there.