Guest User

wirelesschestcontainer

a guest
May 4th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.09 KB | None | 0 0
  1. package com.cclloyd.ccmodpack;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.entity.player.InventoryPlayer;
  5. import net.minecraft.inventory.Container;
  6. import net.minecraft.inventory.Slot;
  7. import net.minecraft.item.ItemStack;
  8.  
  9. public class WirelessChestContainer extends Container {
  10.    
  11.     /**
  12.      * The container is used to link the client side gui to the server side inventory and it is where
  13.      * you add the slots to your gui. It can also be used to sync server side data with the client but
  14.      * that will be covered in a later tutorial
  15.      */
  16.    
  17.     // Stores a reference to the tile entity instance for later use
  18.     private WirelessChestEntity wirelessChestEntity;
  19.  
  20.    
  21.     public WirelessChestEntity getEntity() {
  22.         return wirelessChestEntity;
  23.     }
  24.     // must assign a slot number to each of the slots used by the GUI.
  25.     // For this container, we can see both the tile inventory's slots as well as the player inventory slots and the hotbar.
  26.     // Each time we add a Slot to the container, it automatically increases the slotIndex, which means
  27.     //  0 - 8 = hotbar slots (which will map to the InventoryPlayer slot numbers 0 - 8)
  28.     //  9 - 35 = player inventory slots (which map to the InventoryPlayer slot numbers 9 - 35)
  29.     //  36 - 44 = TileInventory slots, which map to our TileEntity slot numbers 0 - 8)
  30.  
  31.     private final int HOTBAR_COUNT = 9;
  32.     private final int PLAYER_ROWS = 3;
  33.     private final int PLAYER_COLUMNS = 9;
  34.     private final int PLAYER_SLOTS = PLAYER_COLUMNS * PLAYER_ROWS;
  35.     private final int VANILLA_SLOTS = HOTBAR_COUNT + PLAYER_SLOTS;
  36.     private final int VANILLA_START = 0;
  37.    
  38.     private final int TE_START = VANILLA_START + VANILLA_SLOTS;
  39.     private final int TE_ROWS = 3;
  40.     private final int TE_COLUMNS = 9;
  41.     private final int TE_SLOTS = TE_ROWS * TE_COLUMNS;
  42.  
  43.     public WirelessChestContainer(InventoryPlayer invPlayer, WirelessChestEntity wirelessChestEntity) {
  44.         this.wirelessChestEntity = wirelessChestEntity;
  45.        
  46.         //  Adds all the slots for items
  47.         final int SLOT_X_SPACING = 18;
  48.         final int SLOT_Y_SPACING = 18;
  49.        
  50.         final int HOTBAR_XPOS = 8;
  51.         final int HOTBAR_YPOS = 164;
  52.         final int PLAYER_XPOS = 8;
  53.         final int PLAYER_YPOS = 106;
  54.         final int TE_XPOS = 8;
  55.         final int TE_YPOS = 48;
  56.        
  57.         // Add the players hotbar to the gui - the [xpos, ypos] location of each item
  58.         for (int x = 0; x < HOTBAR_COUNT; x++) {
  59.             int slotNumber = x;
  60.             addSlotToContainer(new Slot(invPlayer, slotNumber, HOTBAR_XPOS + SLOT_X_SPACING * x, HOTBAR_YPOS));
  61.         }
  62.  
  63.        
  64.         // Add the rest of the players inventory to the gui
  65.         for (int y = 0; y < PLAYER_ROWS; y++) {
  66.             for (int x = 0; x < PLAYER_COLUMNS; x++) {
  67.                 int slotNumber = HOTBAR_COUNT + y * PLAYER_COLUMNS + x;
  68.                 int xpos = PLAYER_XPOS + x * SLOT_X_SPACING;
  69.                 int ypos = PLAYER_YPOS + y * SLOT_Y_SPACING;
  70.                 addSlotToContainer(new Slot(invPlayer, slotNumber,  xpos, ypos));
  71.             }
  72.         }
  73.  
  74.        
  75.         if (TE_SLOTS != wirelessChestEntity.getSizeInventory()) {
  76.             System.err.println("Mismatched slot count in ContainerBasic(" + TE_SLOTS
  77.                                                   + ") and TileInventory (" + wirelessChestEntity.getSizeInventory()+")");
  78.         }
  79.        
  80.        
  81.         // Add the tile inventory container to the gui
  82.         for (int y = 0; y < TE_ROWS; y++) {
  83.             for (int x = 0; x < TE_COLUMNS; x++) {
  84.                 //int slotNumber = HOTBAR_COUNT + y * PLAYER_COLUMNS + x;
  85.                 //int slotNumber = TE_START + (y * TE_COLUMNS) + x;
  86.                 int slotNumber = (y * HOTBAR_COUNT) + x;
  87.                 int xpos = TE_XPOS + (x * SLOT_X_SPACING);
  88.                 int ypos = TE_YPOS + (y * SLOT_Y_SPACING);
  89.                 addSlotToContainer(new Slot(wirelessChestEntity, slotNumber,  xpos, ypos));
  90.             }
  91.         }
  92.        
  93.     }
  94.  
  95.     // Vanilla calls this method every tick to make sure the player is still able to access the inventory, and if not closes the gui
  96.     @Override
  97.     public boolean canInteractWith(EntityPlayer player)
  98.     {
  99.         return wirelessChestEntity.isUseableByPlayer(player);
  100.     }
  101.  
  102.     // This is where you specify what happens when a player shift clicks a slot in the gui
  103.     //  (when you shift click a slot in the TileEntity Inventory, it moves it to the first available position in the hotbar and/or
  104.     //    player inventory.  When you you shift-click a hotbar or player inventory item, it moves it to the first available
  105.     //    position in the TileEntity inventory)
  106.     // At the very least you must override this and return null or the game will crash when the player shift clicks a slot
  107.     // returns null if the source slot is empty, or if none of the the source slot items could be moved
  108.     //   otherwise, returns a copy of the source stack
  109.     @Override
  110.     public ItemStack transferStackInSlot(EntityPlayer player, int sourceSlotIndex)
  111.     {
  112.         Slot sourceSlot = (Slot)inventorySlots.get(sourceSlotIndex);
  113.         if (sourceSlot == null || !sourceSlot.getHasStack()) return null;
  114.         ItemStack sourceStack = sourceSlot.getStack();
  115.         ItemStack copyOfSourceStack = sourceStack.copy();
  116.  
  117.         // Check if the slot clicked is one of the vanilla container slots
  118.         if (sourceSlotIndex >= VANILLA_START && sourceSlotIndex < VANILLA_START + VANILLA_SLOTS) {
  119.             // This is a vanilla container slot so merge the stack into the tile inventory
  120.             if (!mergeItemStack(sourceStack, TE_START, TE_START + TE_SLOTS, false)){
  121.                 return null;
  122.             }
  123.         } else if (sourceSlotIndex >= TE_START && sourceSlotIndex < TE_START + TE_SLOTS) {
  124.             // This is a TE slot so merge the stack into the players inventory
  125.             if (!mergeItemStack(sourceStack, VANILLA_START, VANILLA_START + VANILLA_SLOTS, false)) {
  126.                 return null;
  127.             }
  128.         } else {
  129.             System.err.print("Invalid slotIndex:" + sourceSlotIndex);
  130.             return null;
  131.         }
  132.  
  133.         // If stack size == 0 (the entire stack was moved) set slot contents to null
  134.         if (sourceStack.stackSize == 0) {
  135.             sourceSlot.putStack(null);
  136.         } else {
  137.             sourceSlot.onSlotChanged();
  138.         }
  139.  
  140.         sourceSlot.onPickupFromSlot(player, sourceStack);
  141.         return copyOfSourceStack;
  142.     }
  143.  
  144.     // pass the close container message to the tileEntityInventory (not strictly needed for this example)
  145.     //  see ContainerChest and TileEntityChest
  146.     @Override
  147.     public void onContainerClosed(EntityPlayer playerIn)
  148.     {
  149.         super.onContainerClosed(playerIn);
  150.         this.wirelessChestEntity.closeInventory(playerIn);
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment