Fixed shift clicking crash, now saves the inventory of the chest

This commit is contained in:
modmuss50 2015-04-11 11:53:27 +01:00
parent b41adffa60
commit 0e6d635e8d
7 changed files with 130 additions and 16 deletions

View file

@ -1,14 +1,12 @@
package techreborn.client.container; package techreborn.client.container;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot; import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import techreborn.client.SlotFake; import techreborn.client.SlotFake;
import techreborn.client.SlotOutput; import techreborn.client.SlotOutput;
import techreborn.tiles.TileQuantumChest; import techreborn.tiles.TileQuantumChest;
public class ContainerQuantumChest extends Container { public class ContainerQuantumChest extends TechRebornContainer {
public TileQuantumChest tileQuantumChest; public TileQuantumChest tileQuantumChest;
public EntityPlayer player; public EntityPlayer player;
@ -39,9 +37,4 @@ public class ContainerQuantumChest extends Container {
return true; return true;
} }
//TODO enable shift-clicking
public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
ItemStack stack = null;
return stack;
}
} }

View file

@ -1,13 +1,12 @@
package techreborn.client.container; package techreborn.client.container;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot; import net.minecraft.inventory.Slot;
import techreborn.client.SlotFake; import techreborn.client.SlotFake;
import techreborn.client.SlotOutput; import techreborn.client.SlotOutput;
import techreborn.tiles.TileQuantumTank; import techreborn.tiles.TileQuantumTank;
public class ContainerQuantumTank extends Container { public class ContainerQuantumTank extends TechRebornContainer {
public TileQuantumTank tileQuantumTank; public TileQuantumTank tileQuantumTank;
public EntityPlayer player; public EntityPlayer player;

View file

@ -1,13 +1,12 @@
package techreborn.client.container; package techreborn.client.container;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot; import net.minecraft.inventory.Slot;
import techreborn.client.SlotFake; import techreborn.client.SlotFake;
import techreborn.client.SlotOutput; import techreborn.client.SlotOutput;
import techreborn.tiles.TileThermalGenerator; import techreborn.tiles.TileThermalGenerator;
public class ContainerThermalGenerator extends Container { public class ContainerThermalGenerator extends TechRebornContainer {
public TileThermalGenerator tileThermalGenerator; public TileThermalGenerator tileThermalGenerator;
public EntityPlayer player; public EntityPlayer player;

View file

@ -0,0 +1,94 @@
package techreborn.client.container;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import techreborn.client.SlotFake;
import techreborn.util.FluidUtils;
public abstract class TechRebornContainer extends Container {
public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) {
ItemStack originalStack = null;
Slot slot = (Slot) inventorySlots.get(slotIndex);
int numSlots = inventorySlots.size();
if (slot != null && slot.getHasStack()) {
ItemStack stackInSlot = slot.getStack();
originalStack = stackInSlot.copy();
if (slotIndex >= numSlots - 9 * 4 && tryShiftItem(stackInSlot, numSlots)) {
// NOOP
} else if (slotIndex >= numSlots - 9 * 4 && slotIndex < numSlots - 9) {
if (!shiftItemStack(stackInSlot, numSlots - 9, numSlots))
return null;
} else if (slotIndex >= numSlots - 9 && slotIndex < numSlots) {
if (!shiftItemStack(stackInSlot, numSlots - 9 * 4, numSlots - 9))
return null;
} else if (!shiftItemStack(stackInSlot, numSlots - 9 * 4, numSlots))
return null;
slot.onSlotChange(stackInSlot, originalStack);
if (stackInSlot.stackSize <= 0)
slot.putStack(null);
else
slot.onSlotChanged();
if (stackInSlot.stackSize == originalStack.stackSize)
return null;
slot.onPickupFromSlot(player, stackInSlot);
}
return originalStack;
}
protected boolean shiftItemStack(ItemStack stackToShift, int start, int end) {
boolean changed = false;
if (stackToShift.isStackable())
for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < end; slotIndex++) {
Slot slot = (Slot) inventorySlots.get(slotIndex);
ItemStack stackInSlot = slot.getStack();
if (stackInSlot != null && FluidUtils.isItemEqual(stackInSlot, stackToShift, true, true)) {
int resultingStackSize = stackInSlot.stackSize + stackToShift.stackSize;
int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit());
if (resultingStackSize <= max) {
stackToShift.stackSize = 0;
stackInSlot.stackSize = resultingStackSize;
slot.onSlotChanged();
changed = true;
} else if (stackInSlot.stackSize < max) {
stackToShift.stackSize -= max - stackInSlot.stackSize;
stackInSlot.stackSize = max;
slot.onSlotChanged();
changed = true;
}
}
}
if (stackToShift.stackSize > 0)
for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < end; slotIndex++) {
Slot slot = (Slot) inventorySlots.get(slotIndex);
ItemStack stackInSlot = slot.getStack();
if (stackInSlot == null) {
int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit());
stackInSlot = stackToShift.copy();
stackInSlot.stackSize = Math.min(stackToShift.stackSize, max);
stackToShift.stackSize -= stackInSlot.stackSize;
slot.putStack(stackInSlot);
slot.onSlotChanged();
changed = true;
}
}
return changed;
}
private boolean tryShiftItem(ItemStack stackToShift, int numSlots) {
for (int machineIndex = 0; machineIndex < numSlots - 9 * 4; machineIndex++) {
Slot slot = (Slot) inventorySlots.get(machineIndex);
if (slot instanceof SlotFake) {
continue;
}
if (!slot.isItemValid(stackToShift))
continue;
if (shiftItemStack(stackToShift, machineIndex, machineIndex + 1))
return true;
}
return false;
}
}

View file

@ -6,9 +6,7 @@ import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import techreborn.client.container.ContainerQuantumChest; import techreborn.client.container.ContainerQuantumChest;
import techreborn.client.container.ContainerQuantumTank;
import techreborn.tiles.TileQuantumChest; import techreborn.tiles.TileQuantumChest;
import techreborn.tiles.TileQuantumTank;
public class GuiQuantumChest extends GuiContainer { public class GuiQuantumChest extends GuiContainer {

View file

@ -1,10 +1,10 @@
package techreborn.tiles; package techreborn.tiles;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager; import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet; import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity; import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
@ -73,6 +73,37 @@ public class TileQuantumChest extends TileEntity implements IInventory {
readFromNBT(packet.func_148857_g()); readFromNBT(packet.func_148857_g());
} }
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);
inventory.readFromNBT(tagCompound);
storedItem = null;
if (tagCompound.hasKey("storedStack"))
{
storedItem = ItemStack.
loadItemStackFromNBT((NBTTagCompound)tagCompound.getTag("storedStack"));
}
if(storedItem != null){
storedItem.stackSize = tagCompound.getInteger("storedQuantity");
}
}
@Override
public void writeToNBT(NBTTagCompound tagCompound) {
super.writeToNBT(tagCompound);
inventory.writeToNBT(tagCompound);
if (storedItem != null)
{
tagCompound.setTag("storedStack", storedItem.writeToNBT(new NBTTagCompound()));
tagCompound.setInteger("storedQuantity", storedItem.stackSize);
}
else
tagCompound.setInteger("storedQuantity", 0);
}
@Override @Override
public int getSizeInventory() { public int getSizeInventory() {
return inventory.getSizeInventory(); return inventory.getSizeInventory();

View file

@ -19,7 +19,7 @@ import techreborn.util.Tank;
public class TileQuantumTank extends TileEntity implements IFluidHandler, IInventory { public class TileQuantumTank extends TileEntity implements IFluidHandler, IInventory {
public Tank tank = new Tank("TileQuantumTank", 2000000000, this); public Tank tank = new Tank("TileQuantumTank", Integer.MAX_VALUE, this);
public Inventory inventory = new Inventory(3, "TileQuantumTank", 64); public Inventory inventory = new Inventory(3, "TileQuantumTank", 64);
@Override @Override