diff options
| author | Alkalus <draknyte1@hotmail.com> | 2017-09-22 14:07:13 +1000 |
|---|---|---|
| committer | Alkalus <draknyte1@hotmail.com> | 2017-09-22 14:07:13 +1000 |
| commit | 14b6ee91c1d784539c130d4ed90a0aa2a87d4902 (patch) | |
| tree | fb22577c02a08ff102884b38c2b9b82114e80e26 /src/Java/gtPlusPlus/core/inventories | |
| parent | bc74aaa35b726899e5830425eb7a8fb5e54501e8 (diff) | |
| download | GT5-Unofficial-14b6ee91c1d784539c130d4ed90a0aa2a87d4902.tar.gz GT5-Unofficial-14b6ee91c1d784539c130d4ed90a0aa2a87d4902.tar.bz2 GT5-Unofficial-14b6ee91c1d784539c130d4ed90a0aa2a87d4902.zip | |
+ More work on the Trade Table.
Diffstat (limited to 'src/Java/gtPlusPlus/core/inventories')
| -rw-r--r-- | src/Java/gtPlusPlus/core/inventories/tradetable/InventoryTradeMain.java | 202 | ||||
| -rw-r--r-- | src/Java/gtPlusPlus/core/inventories/tradetable/InventoryTradeOutput.java | 188 |
2 files changed, 390 insertions, 0 deletions
diff --git a/src/Java/gtPlusPlus/core/inventories/tradetable/InventoryTradeMain.java b/src/Java/gtPlusPlus/core/inventories/tradetable/InventoryTradeMain.java new file mode 100644 index 0000000000..7b125edcb0 --- /dev/null +++ b/src/Java/gtPlusPlus/core/inventories/tradetable/InventoryTradeMain.java @@ -0,0 +1,202 @@ +package gtPlusPlus.core.inventories.tradetable; + +import gtPlusPlus.core.util.Utils; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; + +public class InventoryTradeMain implements IInventory{ + + private final String name = "Inventory Grid"; + + /** Defining your inventory size this way is handy */ + public static final int INV_SIZE = 9; + + /** Inventory's size must be same as number of slots you add to the Container class */ + private ItemStack[] inventory = new ItemStack[INV_SIZE]; + + /** + * @param itemstack - the ItemStack to which this inventory belongs + */ + public InventoryTradeMain() + { + + } + + public void readFromNBT(final NBTTagCompound nbt) + { + final NBTTagList list = nbt.getTagList("Items", 10); + this.inventory = new ItemStack[INV_SIZE]; + for(int i = 0;i<list.tagCount();i++) + { + final NBTTagCompound data = list.getCompoundTagAt(i); + final int slot = data.getInteger("Slot"); + if((slot >= 0) && (slot < INV_SIZE)) + { + this.inventory[slot] = ItemStack.loadItemStackFromNBT(data); + } + } + } + + public void writeToNBT(final NBTTagCompound nbt) + { + final NBTTagList list = new NBTTagList(); + for(int i = 0;i<INV_SIZE;i++) + { + final ItemStack stack = this.inventory[i]; + if(stack != null) + { + final NBTTagCompound data = new NBTTagCompound(); + stack.writeToNBT(data); + data.setInteger("Slot", i); + list.appendTag(data); + } + } + nbt.setTag("Items", list); + } + + @Override + public int getSizeInventory() + { + return this.inventory.length; + } + + public ItemStack[] getInventory(){ + return this.inventory; + } + + @Override + public ItemStack getStackInSlot(final int slot) + { + //Utils.LOG_INFO("getStackInSlot["+slot+"]"); + try { + if (slot>= this.inventory.length){ + //Utils.LOG_INFO("Returning invalid slot item."); + return null; + } + return this.inventory[slot]; + } catch (Throwable t){ + t.printStackTrace(); + return null; + } + } + + @Override + public ItemStack decrStackSize(final int slot, final int amount) + { + ItemStack stack = this.getStackInSlot(slot); + if(stack != null) + { + if(stack.stackSize > amount) + { + stack = stack.splitStack(amount); + // Don't forget this line or your inventory will not be saved! + this.markDirty(); + } + else + { + // this method also calls markDirty, so we don't need to call it again + this.setInventorySlotContents(slot, null); + } + } + return stack; + } + + @Override + public ItemStack getStackInSlotOnClosing(final int slot) + { + final ItemStack stack = this.getStackInSlot(slot); + this.setInventorySlotContents(slot, null); + return stack; + } + + @Override + public void setInventorySlotContents(final int slot, final ItemStack stack) + { + if (slot >= this.inventory.length){ + return; + } + this.inventory[slot] = stack; + + if ((stack != null) && (stack.stackSize > this.getInventoryStackLimit())) + { + stack.stackSize = this.getInventoryStackLimit(); + } + + // Don't forget this line or your inventory will not be saved! + this.markDirty(); + } + + // 1.7.2+ renamed to getInventoryName + @Override + public String getInventoryName() + { + return this.name; + } + + // 1.7.2+ renamed to hasCustomInventoryName + @Override + public boolean hasCustomInventoryName() + { + return this.name.length() > 0; + } + + @Override + public int getInventoryStackLimit() + { + return 64; + } + + /** + * This is the method that will handle saving the inventory contents, as it is called (or should be called!) + * anytime the inventory changes. Perfect. Much better than using onUpdate in an Item, as this will also + * let you change things in your inventory without ever opening a Gui, if you want. + */ + // 1.7.2+ renamed to markDirty + @Override + public void markDirty() + { + for (int i = 0; i < this.getSizeInventory(); ++i) + { + final ItemStack temp = this.getStackInSlot(i); + if (temp != null){ + //Utils.LOG_INFO("Slot "+i+" contains "+temp.getDisplayName()+" x"+temp.stackSize); + } + + if ((temp != null) && (temp.stackSize == 0)) { + this.inventory[i] = null; + } + } + } + + @Override + public boolean isUseableByPlayer(final EntityPlayer entityplayer) + { + return true; + } + + // 1.7.2+ renamed to openInventory(EntityPlayer player) + @Override + public void openInventory() {} + + // 1.7.2+ renamed to closeInventory(EntityPlayer player) + @Override + public void closeInventory() {} + + /** + * This method doesn't seem to do what it claims to do, as + * items can still be left-clicked and placed in the inventory + * even when this returns false + */ + @Override + public boolean isItemValidForSlot(final int slot, final ItemStack itemstack) + { + // Don't want to be able to store the inventory item within itself + // Bad things will happen, like losing your inventory + // Actually, this needs a custom Slot to work + return true; + } + +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/inventories/tradetable/InventoryTradeOutput.java b/src/Java/gtPlusPlus/core/inventories/tradetable/InventoryTradeOutput.java new file mode 100644 index 0000000000..fb4329e11a --- /dev/null +++ b/src/Java/gtPlusPlus/core/inventories/tradetable/InventoryTradeOutput.java @@ -0,0 +1,188 @@ +package gtPlusPlus.core.inventories.tradetable; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; + +public class InventoryTradeOutput implements IInventory{ + + private final String name = "Inventory Output"; + + /** Defining your inventory size this way is handy */ + public static final int INV_SIZE = 2; + + /** Inventory's size must be same as number of slots you add to the Container class */ + private ItemStack[] inventory = new ItemStack[INV_SIZE]; + + /** + * @param itemstack - the ItemStack to which this inventory belongs + */ + public InventoryTradeOutput() + { + + } + + public void readFromNBT(final NBTTagCompound nbt) + { + final NBTTagList list = nbt.getTagList("Items", 10); + this.inventory = new ItemStack[INV_SIZE]; + for(int i = 0;i<list.tagCount();i++) + { + final NBTTagCompound data = list.getCompoundTagAt(i); + final int slot = data.getInteger("Slot"); + if((slot >= 0) && (slot < INV_SIZE)) + { + this.inventory[slot] = ItemStack.loadItemStackFromNBT(data); + } + } + } + + public void writeToNBT(final NBTTagCompound nbt) + { + final NBTTagList list = new NBTTagList(); + for(int i = 0;i<INV_SIZE;i++) + { + final ItemStack stack = this.inventory[i]; + if(stack != null) + { + final NBTTagCompound data = new NBTTagCompound(); + stack.writeToNBT(data); + data.setInteger("Slot", i); + list.appendTag(data); + } + } + nbt.setTag("Items", list); + } + + @Override + public int getSizeInventory() + { + return this.inventory.length; + } + + public ItemStack[] getInventory(){ + return this.inventory; + } + + @Override + public ItemStack getStackInSlot(final int slot) + { + return this.inventory[slot]; + } + + @Override + public ItemStack decrStackSize(final int slot, final int amount) + { + ItemStack stack = this.getStackInSlot(slot); + if(stack != null) + { + if(stack.stackSize > amount) + { + stack = stack.splitStack(amount); + // Don't forget this line or your inventory will not be saved! + this.markDirty(); + } + else + { + // this method also calls markDirty, so we don't need to call it again + this.setInventorySlotContents(slot, null); + } + } + return stack; + } + + @Override + public ItemStack getStackInSlotOnClosing(final int slot) + { + final ItemStack stack = this.getStackInSlot(slot); + this.setInventorySlotContents(slot, null); + return stack; + } + + @Override + public void setInventorySlotContents(final int slot, final ItemStack stack) + { + this.inventory[slot] = stack; + + if ((stack != null) && (stack.stackSize > this.getInventoryStackLimit())) + { + stack.stackSize = this.getInventoryStackLimit(); + } + + // Don't forget this line or your inventory will not be saved! + this.markDirty(); + } + + // 1.7.2+ renamed to getInventoryName + @Override + public String getInventoryName() + { + return this.name; + } + + // 1.7.2+ renamed to hasCustomInventoryName + @Override + public boolean hasCustomInventoryName() + { + return this.name.length() > 0; + } + + @Override + public int getInventoryStackLimit() + { + return 64; + } + + /** + * This is the method that will handle saving the inventory contents, as it is called (or should be called!) + * anytime the inventory changes. Perfect. Much better than using onUpdate in an Item, as this will also + * let you change things in your inventory without ever opening a Gui, if you want. + */ + // 1.7.2+ renamed to markDirty + @Override + public void markDirty() + { + for (int i = 0; i < this.getSizeInventory(); ++i) + { + final ItemStack temp = this.getStackInSlot(i); + if (temp != null){ + //Utils.LOG_INFO("Slot "+i+" contains "+temp.getDisplayName()+" x"+temp.stackSize); + } + + if ((temp != null) && (temp.stackSize == 0)) { + this.inventory[i] = null; + } + } + } + + @Override + public boolean isUseableByPlayer(final EntityPlayer entityplayer) + { + return true; + } + + // 1.7.2+ renamed to openInventory(EntityPlayer player) + @Override + public void openInventory() {} + + // 1.7.2+ renamed to closeInventory(EntityPlayer player) + @Override + public void closeInventory() {} + + /** + * This method doesn't seem to do what it claims to do, as + * items can still be left-clicked and placed in the inventory + * even when this returns false + */ + @Override + public boolean isItemValidForSlot(final int slot, final ItemStack itemstack) + { + // Don't want to be able to store the inventory item within itself + // Bad things will happen, like losing your inventory + // Actually, this needs a custom Slot to work + return true; + } + +}
\ No newline at end of file |
