Advertisement
Atijaf

ContainerTeleporterBlock

Jan 31st, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. public class ContainerTeleporterBlock extends Container{
  2.    
  3.     private final TeleporterTE te;
  4.    
  5.     /** Do not edit these values directly */
  6.     private int currentEnergy;
  7.     private int eupu;
  8.    
  9.    
  10.     public ContainerTeleporterBlock(IInventory playerInv, TeleporterTE te) {
  11.         this.te = te;
  12.        
  13.         //te slot 0, slot id 0
  14.         this.addSlotToContainer(new EnergyCapsuleSlot(te, 0, 36, 2));
  15.        
  16.         //te slot 1, slot id 1
  17.         this.addSlotToContainer(new CordeCacheSlot(te, 1, 72, 2));
  18.        
  19.         //Player Inventory Slots 9-35, Slot IDs 2-29
  20.         for(int y = 0; y < 3; y++){
  21.             for(int x = 0; x < 9; x++){
  22.                 this.addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 48 + y * 18));
  23.             }
  24.         }
  25.                
  26.         // Player Inventory, Slot 0-8, Slot IDs 28-36
  27.         for (int x = 0; x < 9; ++x) {
  28.             this.addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 105));
  29.         }
  30.     }
  31.    
  32.    
  33.    
  34.     @Override
  35.     public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) {
  36.         ItemStack previous = null;
  37.         Slot slot = (Slot) this.inventorySlots.get(fromSlot);
  38.        
  39.         if (slot != null && slot.getHasStack()) {
  40.             ItemStack current = slot.getStack();
  41.             previous = current.copy();
  42.  
  43.             if (fromSlot < 2) {
  44.                 // From TE Inventory to Player Inventory
  45.                 if (!this.mergeItemStack(current, 2, 29, true))
  46.                     return null;
  47.             } else {
  48.                 // From Player Inventory to TE Inventory
  49.                 if (!this.mergeItemStack(current, 0, 2, false))
  50.                     return null;
  51.             }
  52.  
  53.             if (current.stackSize == 0)
  54.                 slot.putStack((ItemStack) null);
  55.             else
  56.                 slot.onSlotChanged();
  57.  
  58.             if (current.stackSize == previous.stackSize)
  59.                 return null;
  60.             slot.onPickupFromSlot(playerIn, current);
  61.         }
  62.         return previous;
  63.     }
  64.    
  65.    
  66.    
  67.     @Override
  68.     protected boolean mergeItemStack(ItemStack stack, int start, int end, boolean backwards)
  69.     {
  70.         boolean flag1 = false;
  71.         int k = (backwards ? end - 1 : start);
  72.         Slot slot;
  73.         ItemStack itemstack1;
  74.  
  75.         if (stack.isStackable())
  76.         {
  77.             while (stack.stackSize > 0 && (!backwards && k < end || backwards && k >= start))
  78.             {
  79.                 slot = (Slot) inventorySlots.get(k);
  80.                 itemstack1 = slot.getStack();
  81.  
  82.                 if (!slot.isItemValid(stack)) {
  83.                     k += (backwards ? -1 : 1);
  84.                     continue;
  85.                 }
  86.  
  87.                 if (itemstack1 != null && itemstack1.getItem() == stack.getItem() &&
  88.                         (!stack.getHasSubtypes() || stack.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(stack, itemstack1))
  89.                 {
  90.                     int l = itemstack1.stackSize + stack.stackSize;
  91.  
  92.                     if (l <= stack.getMaxStackSize() && l <= slot.getSlotStackLimit()) {
  93.                         stack.stackSize = 0;
  94.                         itemstack1.stackSize = l;
  95.                         te.markDirty();;
  96.                         flag1 = true;
  97.                     } else if (itemstack1.stackSize < stack.getMaxStackSize() && l < slot.getSlotStackLimit()) {
  98.                         stack.stackSize -= stack.getMaxStackSize() - itemstack1.stackSize;
  99.                         itemstack1.stackSize = stack.getMaxStackSize();
  100.                         te.markDirty();;
  101.                         flag1 = true;
  102.                     }
  103.                 }
  104.  
  105.                 k += (backwards ? -1 : 1);
  106.             }
  107.         }
  108.         if (stack.stackSize > 0)
  109.         {
  110.             k = (backwards ? end - 1 : start);
  111.             while (!backwards && k < end || backwards && k >= start) {
  112.                 slot = (Slot) inventorySlots.get(k);
  113.                 itemstack1 = slot.getStack();
  114.  
  115.                 if (!slot.isItemValid(stack)) {
  116.                     k += (backwards ? -1 : 1);
  117.                     continue;
  118.                 }
  119.  
  120.                 if (itemstack1 == null) {
  121.                     int l = stack.stackSize;
  122.                     if (l <= slot.getSlotStackLimit()) {
  123.                         slot.putStack(stack.copy());
  124.                         stack.stackSize = 0;
  125.                         te.markDirty();;
  126.                         flag1 = true;
  127.                         break;
  128.                     } else {
  129.                         putStackInSlot(k, new ItemStack(stack.getItem(), slot.getSlotStackLimit(), stack.getItemDamage()));
  130.                         stack.stackSize -= slot.getSlotStackLimit();
  131.                         te.markDirty();;
  132.                         flag1 = true;
  133.                     }
  134.                 }
  135.  
  136.                 k += (backwards ? -1 : 1);
  137.             }
  138.         }
  139.  
  140.         return flag1;
  141.     }
  142.    
  143.    
  144.    
  145.    
  146.     /**
  147.      * ATTENTION!  This class only updates when the player is in the inventory
  148.      */
  149.     @Override
  150.     public void detectAndSendChanges() {
  151.         super.detectAndSendChanges();
  152.        
  153.         for(int i = 0; i < this.crafters.size(); i++){
  154.             ICrafting icrafting = (ICrafting)this.crafters.get(i);
  155.            
  156.             if(this.currentEnergy != this.te.getField(0)){
  157.                 icrafting.sendProgressBarUpdate(this, 0, this.te.getField(0));
  158.             }
  159.             if(this.eupu != te.getField(1)){
  160.                 icrafting.sendProgressBarUpdate(this, 1, this.te.getField(1));
  161.             }
  162.         }
  163.            
  164.     }
  165.    
  166.     @Override
  167.     public void updateProgressBar(int id, int data) {
  168.         this.te.setField(id, data);
  169.     }
  170.  
  171.     @Override
  172.     public boolean canInteractWith(EntityPlayer playerIn) {
  173.         return this.te.isUseableByPlayer(playerIn);
  174.     }
  175.    
  176.     /** Causes the game not to update.  Fix somehow?
  177.      * When this is removed, it works just fine except for the fact that if you remove a corde cache
  178.      * from one block, the one it was pointing to doesn't update...  IDK WHY!
  179.      *
  180.      * QUICK IDEA
  181.      * Redo the initialize thing I had going on in the tile entity, so it only updates once
  182.      * then let this do all the updating (i.e. let this use both server and client)  Only if server finds tile entity though
  183.      *
  184.      */
  185.     @Override
  186.     public ItemStack slotClick(int slotId, int clickedButton, int mode, EntityPlayer player) {
  187.         return super.slotClick(slotId, clickedButton, mode, player);
  188.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement