Guest User

Container class

a guest
Dec 30th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.28 KB | None | 0 0
  1. package com.theundertaker11.GeneticsReborn.blocks.cellanalyser;
  2.  
  3. import com.theundertaker11.GeneticsReborn.items.GRItems;
  4.  
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.inventory.Container;
  7.  
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.entity.player.InventoryPlayer;
  10. import net.minecraft.inventory.Container;
  11. import net.minecraft.inventory.IContainerListener;
  12. import net.minecraft.inventory.IInventory;
  13. import net.minecraft.inventory.Slot;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.util.EnumFacing;
  16. import net.minecraftforge.fml.relauncher.Side;
  17. import net.minecraftforge.fml.relauncher.SideOnly;
  18. import net.minecraftforge.items.CapabilityItemHandler;
  19. import net.minecraftforge.items.IItemHandler;
  20. import net.minecraftforge.items.SlotItemHandler;
  21.  
  22. /**
  23. * User: brandon3055
  24. * Date: 06/01/2015
  25. *
  26. * ContainerSmelting is used to link the client side gui to the server side inventory and it is where
  27. * you add the slots holding items. It is also used to send server side data such as progress bars to the client
  28. * for use in guis
  29. */
  30. public class ContainerCellAnalyser extends Container {
  31.  
  32. // Stores the tile entity instance for later use
  33. private GRTileEntityCellAnalyser tileInventory;
  34.  
  35. // These store cache values, used by the server to only update the client side tile entity when values have changed
  36. private int [] cachedFields;
  37.  
  38. // must assign a slot index to each of the slots used by the GUI.
  39. // For this container, we can see the furnace fuel, input, and output slots as well as the player inventory slots and the hotbar.
  40. // Each time we add a Slot to the container using addSlotToContainer(), it automatically increases the slotIndex, which means
  41. // 0 - 8 = hotbar slots (which will map to the InventoryPlayer slot numbers 0 - 8)
  42. // 9 - 35 = player inventory slots (which map to the InventoryPlayer slot numbers 9 - 35)
  43. // 36 - 39 = fuel slots (tileEntity 0 - 3)
  44. // 40 - 44 = input slots (tileEntity 4 - 8)
  45. // 45 - 49 = output slots (tileEntity 9 - 13)
  46.  
  47. private final int HOTBAR_SLOT_COUNT = 9;
  48. private final int PLAYER_INVENTORY_ROW_COUNT = 3;
  49. private final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
  50. private final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
  51. private final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;
  52.  
  53. //public final int FUEL_SLOTS_COUNT = 4;
  54. public final int INPUT_SLOTS_COUNT = 1;
  55. public final int OUTPUT_SLOTS_COUNT = 1;
  56. public final int TOTAL_SLOTS_COUNT = INPUT_SLOTS_COUNT + OUTPUT_SLOTS_COUNT;
  57.  
  58. // slot index is the unique index for all slots in this container i.e. 0 - 35 for invPlayer then 36 - 49 for tileInventory
  59. private final int VANILLA_FIRST_SLOT_INDEX = 0;
  60. //private final int FIRST_FUEL_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
  61. private final int INPUT_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
  62. private final int OUTPUT_SLOT_INDEX = INPUT_SLOT_INDEX + INPUT_SLOTS_COUNT;
  63.  
  64. // slot number is the slot number within each component; i.e. invPlayer slots 0 - 35, and tileInventory slots 0 - 14
  65. //private final int FIRST_FUEL_SLOT_NUMBER = 0;
  66. private final int INPUT_SLOT_NUMBER = 0;
  67. private final int OUTPUT_SLOT_NUMBER = 0;
  68.  
  69. public ContainerCellAnalyser(InventoryPlayer invPlayer, GRTileEntityCellAnalyser tileInventory){
  70. this.tileInventory = tileInventory;
  71.  
  72. final int SLOT_X_SPACING = 18;
  73. final int SLOT_Y_SPACING = 18;
  74. final int HOTBAR_XPOS = 8;
  75. final int HOTBAR_YPOS = 183;
  76. // Add the players hotbar to the gui - the [xpos, ypos] location of each item
  77. for (int x = 0; x < HOTBAR_SLOT_COUNT; x++) {
  78. int slotNumber = x;
  79. addSlotToContainer(new Slot(invPlayer, slotNumber, HOTBAR_XPOS + SLOT_X_SPACING * x, HOTBAR_YPOS));
  80. }
  81.  
  82. final int PLAYER_INVENTORY_XPOS = 8;
  83. final int PLAYER_INVENTORY_YPOS = 125;
  84. // Add the rest of the players inventory to the gui
  85. for (int y = 0; y < PLAYER_INVENTORY_ROW_COUNT; y++) {
  86. for (int x = 0; x < PLAYER_INVENTORY_COLUMN_COUNT; x++) {
  87. int slotNumber = HOTBAR_SLOT_COUNT + y * PLAYER_INVENTORY_COLUMN_COUNT + x;
  88. int xpos = PLAYER_INVENTORY_XPOS + x * SLOT_X_SPACING;
  89. int ypos = PLAYER_INVENTORY_YPOS + y * SLOT_Y_SPACING;
  90. addSlotToContainer(new Slot(invPlayer, slotNumber, xpos, ypos));
  91. }
  92. }
  93. /*
  94. final int FUEL_SLOTS_XPOS = 53;
  95. final int FUEL_SLOTS_YPOS = 96;
  96. // Add the tile fuel slots
  97. for (int x = 0; x < FUEL_SLOTS_COUNT; x++) {
  98. int slotNumber = x + FIRST_FUEL_SLOT_NUMBER;
  99. addSlotToContainer(new SlotFuel(tileInventory, slotNumber, FUEL_SLOTS_XPOS + SLOT_X_SPACING * x, FUEL_SLOTS_YPOS));
  100. }
  101. */
  102. IItemHandler itemhandlerinput = tileInventory.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
  103. IItemHandler itemhandleroutput = tileInventory.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.DOWN);
  104. final int INPUT_SLOTS_XPOS = 26;
  105. final int INPUT_SLOTS_YPOS = 24;
  106. // Add the tile input slots
  107. for (int y = 0; y < INPUT_SLOTS_COUNT; y++) {
  108. int slotNumber = y + INPUT_SLOT_NUMBER;
  109. addSlotToContainer(new SlotSmeltableInput(itemhandlerinput, slotNumber, INPUT_SLOTS_XPOS, INPUT_SLOTS_YPOS+ SLOT_Y_SPACING * y));
  110. }
  111.  
  112. final int OUTPUT_SLOTS_XPOS = 134;
  113. final int OUTPUT_SLOTS_YPOS = 24;
  114. // Add the tile output slots
  115. for (int y = 0; y < OUTPUT_SLOTS_COUNT; y++) {
  116. int slotNumber = y + OUTPUT_SLOT_NUMBER;
  117. addSlotToContainer(new SlotOutput(itemhandleroutput, slotNumber, OUTPUT_SLOTS_XPOS, OUTPUT_SLOTS_YPOS + SLOT_Y_SPACING * y));
  118. }
  119. }
  120.  
  121. // Checks each tick to make sure the player is still able to access the inventory and if not closes the gui
  122. @Override
  123. public boolean canInteractWith(EntityPlayer player)
  124. {
  125. return tileInventory.isUseableByPlayer(player);
  126. }
  127.  
  128. // This is where you specify what happens when a player shift clicks a slot in the gui
  129. // (when you shift click a slot in the TileEntity Inventory, it moves it to the first available position in the hotbar and/or
  130. // player inventory. When you you shift-click a hotbar or player inventory item, it moves it to the first available
  131. // position in the TileEntity inventory - either input or fuel as appropriate for the item you clicked)
  132. // At the very least you must override this and return null or the game will crash when the player shift clicks a slot
  133. // returns null if the source slot is empty, or if none of the source slot items could be moved.
  134. // otherwise, returns a copy of the source stack
  135. @Override
  136. public ItemStack transferStackInSlot(EntityPlayer player, int sourceSlotIndex)
  137. {
  138. ItemStack itemstack = null;
  139. Slot slot = this.inventorySlots.get(sourceSlotIndex);
  140.  
  141. if (slot != null && slot.getHasStack()) {
  142. ItemStack itemstack1 = slot.getStack();
  143. itemstack = itemstack1.copy();
  144.  
  145. if (sourceSlotIndex < GRTileEntityCellAnalyser.getSIZE()) {
  146. if (!this.mergeItemStack(itemstack1, GRTileEntityCellAnalyser.getSIZE(), this.inventorySlots.size(), true)) {
  147. return null;
  148. }
  149. }else if (!this.mergeItemStack(itemstack1, 0, GRTileEntityCellAnalyser.getSIZE(), false)) {
  150. return null;
  151. }
  152.  
  153. if (itemstack1.stackSize == 0) {
  154. slot.putStack(null);
  155. } else {
  156. slot.onSlotChanged();
  157. }
  158. }
  159. return itemstack;
  160. /*
  161. Slot sourceSlot = (Slot)inventorySlots.get(sourceSlotIndex);
  162. if (sourceSlot == null || !sourceSlot.getHasStack()) return null;
  163. ItemStack sourceStack = sourceSlot.getStack();
  164. ItemStack copyOfSourceStack = sourceStack.copy();
  165.  
  166. // Check if the slot clicked is one of the vanilla container slots
  167. if (sourceSlotIndex >= VANILLA_FIRST_SLOT_INDEX && sourceSlotIndex < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) {
  168. // This is a vanilla container slot so merge the stack into one of the furnace slots
  169. // If the stack is smeltable try to merge merge the stack into the input slots
  170. if (GRTileEntityCellAnalyser.getSmeltingResultForItem(sourceStack) != null){
  171. if (!mergeItemStack(sourceStack, FIRST_INPUT_SLOT_INDEX, FIRST_INPUT_SLOT_INDEX + INPUT_SLOTS_COUNT, false)){
  172. return null;
  173. }
  174. } else if (GRTileEntityCellAnalyser.getItemBurnTime(sourceStack) > 0) {
  175. if (!mergeItemStack(sourceStack, FIRST_FUEL_SLOT_INDEX, FIRST_FUEL_SLOT_INDEX + FUEL_SLOTS_COUNT, true)) {
  176. // Setting the boolean to true places the stack in the bottom slot first
  177. return null;
  178. }
  179. } else {
  180. return null;
  181. }
  182. } else if (sourceSlotIndex >= FIRST_FUEL_SLOT_INDEX && sourceSlotIndex < FIRST_FUEL_SLOT_INDEX + FURNACE_SLOTS_COUNT) {
  183. // This is a furnace slot so merge the stack into the players inventory: try the hotbar first and then the main inventory
  184. // because the main inventory slots are immediately after the hotbar slots, we can just merge with a single call
  185. if (!mergeItemStack(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) {
  186. return null;
  187. }
  188. } else {
  189. System.err.print("Invalid slotIndex:" + sourceSlotIndex);
  190. return null;
  191. }
  192.  
  193. // If stack size == 0 (the entire stack was moved) set slot contents to null
  194. if (sourceStack.stackSize == 0) {
  195. sourceSlot.putStack(null);
  196. } else {
  197. sourceSlot.onSlotChanged();
  198. }
  199.  
  200. sourceSlot.onPickupFromSlot(player, sourceStack);
  201. return copyOfSourceStack;
  202. */
  203. }
  204.  
  205. /* Client Synchronization */
  206.  
  207. // This is where you check if any values have changed and if so send an update to any clients accessing this container
  208. // The container itemstacks are tested in Container.detectAndSendChanges, so we don't need to do that
  209. // We iterate through all of the TileEntity Fields to find any which have changed, and send them.
  210. // You don't have to use fields if you don't wish to; just manually match the ID in sendProgressBarUpdate with the value in
  211. // updateProgressBar()
  212. // The progress bar values are restricted to shorts. If you have a larger value (eg int), it's not a good idea to try and split it
  213. // up into two shorts because the progress bar values are sent independently, and unless you add synchronisation logic at the
  214. // receiving side, your int value will be wrong until the second short arrives. Use a custom packet instead.
  215. @Override
  216. public void detectAndSendChanges() {
  217. super.detectAndSendChanges();
  218.  
  219. boolean allFieldsHaveChanged = false;
  220. boolean fieldHasChanged [] = new boolean[tileInventory.getFieldCount()];
  221. if (cachedFields == null) {
  222. cachedFields = new int[tileInventory.getFieldCount()];
  223. allFieldsHaveChanged = true;
  224. }
  225. for (int i = 0; i < cachedFields.length; ++i) {
  226. if (allFieldsHaveChanged || cachedFields[i] != tileInventory.getField(i)) {
  227. cachedFields[i] = tileInventory.getField(i);
  228. fieldHasChanged[i] = true;
  229. }
  230. }
  231.  
  232. // go through the list of listeners (players using this container) and update them if necessary
  233. for (IContainerListener listener : this.listeners) {
  234. for (int fieldID = 0; fieldID < tileInventory.getFieldCount(); ++fieldID) {
  235. if (fieldHasChanged[fieldID])
  236. {
  237. // Note that although sendProgressBarUpdate takes 2 ints on a server these are truncated to shorts
  238. listener.sendProgressBarUpdate(this, fieldID, cachedFields[fieldID]);
  239. }
  240. }
  241. }
  242. }
  243.  
  244. // Called when a progress bar update is received from the server. The two values (id and data) are the same two
  245. // values given to sendProgressBarUpdate. In this case we are using fields so we just pass them to the tileEntity.
  246. @SideOnly(Side.CLIENT)
  247. @Override
  248. public void updateProgressBar(int id, int data) {
  249. tileInventory.setField(id, data);
  250. }
  251.  
  252. // SlotFuel is a slot for fuel items
  253. /*public class SlotFuel extends Slot {
  254. public SlotFuel(IInventory inventoryIn, int index, int xPosition, int yPosition) {
  255. super(inventoryIn, index, xPosition, yPosition);
  256. }
  257.  
  258. // if this function returns false, the player won't be able to insert the given item into this slot
  259. @Override
  260. public boolean isItemValid(ItemStack stack) {
  261. return GRTileEntityCellAnalyser.isItemValidForFuelSlot(stack);
  262. }
  263. }*/
  264.  
  265. // SlotSmeltableInput is a slot for input items
  266. public class SlotSmeltableInput extends SlotItemHandler {
  267. public SlotSmeltableInput(IItemHandler inventoryIn, int index, int xPosition, int yPosition) {
  268. super(inventoryIn, index, xPosition, yPosition);
  269. }
  270.  
  271. // if this function returns false, the player won't be able to insert the given item into this slot
  272. @Override
  273. public boolean isItemValid(ItemStack stack) {
  274. return (stack.getItem()==GRItems.OrganicMatter);
  275. }
  276. }
  277.  
  278. // SlotOutput is a slot that will not accept any items
  279. public class SlotOutput extends SlotItemHandler {
  280. public SlotOutput(IItemHandler inventoryIn, int index, int xPosition, int yPosition) {
  281. super(inventoryIn, index, xPosition, yPosition);
  282. }
  283.  
  284. // if this function returns false, the player won't be able to insert the given item into this slot
  285. @Override
  286. public boolean isItemValid(ItemStack stack) {
  287. return false;
  288. }
  289. }
  290. }
Add Comment
Please, Sign In to add comment