Guest User

TileEntityICEC

a guest
Jul 31st, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. package com.jam.icc.tileentity;
  2.  
  3. import java.util.List;
  4. import java.util.UUID;
  5.  
  6. import com.jam.icc.block.BlockItemCollectorChest;
  7. import com.jam.icc.block.BlockItemCollectorEnderChest;
  8.  
  9. import net.minecraft.entity.item.EntityItem;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.inventory.IInventory;
  12. import net.minecraft.inventory.Slot;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.nbt.NBTBase;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. import net.minecraft.network.NetworkManager;
  18. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  19. import net.minecraft.tileentity.TileEntity;
  20. import net.minecraft.util.EntitySelectors;
  21. import net.minecraft.util.EnumFacing;
  22. import net.minecraft.util.ITickable;
  23. import net.minecraft.util.math.AxisAlignedBB;
  24. import net.minecraftforge.common.capabilities.Capability;
  25. import net.minecraftforge.items.CapabilityItemHandler;
  26. import net.minecraftforge.items.IItemHandler;
  27. import net.minecraftforge.items.ItemHandlerHelper;
  28. import net.minecraftforge.items.ItemStackHandler;
  29.  
  30. public class TileEntityICEC extends TileEntity implements ITickable{
  31.    
  32.     public static final int SIZE1 = 27;
  33.     int currentTickRate = 20;
  34.     int counter = 0;
  35.    
  36.     public static UUID playerName;
  37.    
  38.    
  39.    
  40.     // This item handler will hold the inventory slots
  41.     //private ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE) {
  42.      //   @Override
  43.       //  protected void onContentsChanged(int slot) {
  44.        //     // We need to tell the tile entity that something has changed so
  45.             // that the chest contents is persisted
  46.        //     TileEntityICEC.this.markDirty();
  47.       // }
  48.     //};
  49.  // This item handler will hold the filter slot
  50.     private ItemStackHandler filterStackHandler = new ItemStackHandler(1) {
  51.         @Override
  52.         protected void onContentsChanged(int slot) {
  53.             // We need to tell the tile entity that something has changed so
  54.             // that the chest c
  55.             //contents is persisted
  56.             TileEntityICEC.this.markDirty();
  57.         }
  58.     };
  59.    
  60.    
  61.    
  62.     private boolean hasBeenCleared;
  63.    
  64.     @Override
  65.     public void readFromNBT(NBTTagCompound compound) {
  66.         super.readFromNBT(compound);
  67.        
  68.         if (compound.hasKey("filterItems")) {
  69.            filterStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("filterItems"));
  70.         }
  71.      
  72.             compound.getUniqueId("playerName");
  73.        
  74.     }
  75.    
  76.    
  77.    
  78.     @Override
  79.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  80.         super.writeToNBT(compound);
  81.        
  82.       compound.setTag("filterItems", filterStackHandler.serializeNBT());
  83.          
  84.      
  85.      
  86.         return compound;
  87.     }
  88.    
  89.     private ItemStackHandler ItemHandler = new ItemStackHandler(27) {
  90.         @Override
  91.         protected void onContentsChanged(int slot) {
  92.            /*
  93.             * if the slots in the container changes, change the slots in the players ender chest inventory.
  94.             */
  95.              EntityPlayer player = world.getPlayerEntityByUUID(getTileData().getUniqueId("playerName"));
  96.                 for(int i = 0; i < 27; i++)
  97.                 {
  98.                     player.getInventoryEnderChest().setInventorySlotContents(i, this.getStackInSlot(i));;
  99.                    
  100.                 }
  101.             }
  102.      
  103.     };
  104.  
  105.     /**
  106.      * Called from the tile entity's block in the onBlockPlacedBy method.
  107.      * @param player
  108.      */
  109.     public void onPlaced(EntityPlayer player)
  110.         {
  111.             playerName = player.getUniqueID();
  112.            
  113.             this.getTileData().setUniqueId("playerName", playerName);
  114.            
  115.            
  116.         }
  117.    
  118.     public void onOpened(EntityPlayer player)
  119.     {
  120.         for(int i = 0; i < 27; i++)
  121.         {
  122.             ItemHandler.setStackInSlot(i, player.getInventoryEnderChest().getStackInSlot(i));
  123.         }
  124.     }
  125.    
  126.    
  127.     /**
  128.      * returns whether or not you are the placer of the tile entity's block.
  129.      * @param player
  130.      * @return
  131.      */
  132.     public boolean isPlacer(EntityPlayer player)
  133.     {
  134.        
  135.        
  136.             if(player.getUniqueID().equals(this.getTileData().getUniqueId("playerName")) )
  137.             {
  138.                 return true;
  139.             }
  140.                 else
  141.             {
  142.                 return false;
  143.             }
  144.        
  145.    
  146.     }
  147.    
  148.    
  149.  
  150.     @Override
  151.     public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
  152.         if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  153.             return true;
  154.         }
  155.        
  156.         return super.hasCapability(capability, facing);
  157.     }
  158.  
  159.    
  160.    
  161.     public ItemStackHandler enderstack()
  162.     {
  163.         return ItemHandler;
  164.        
  165.     }
  166.    
  167.     public ItemStackHandler filterstack()
  168.     {
  169.         return filterStackHandler;
  170.     }
  171.    
  172.  
  173.    
  174. }
Advertisement
Add Comment
Please, Sign In to add comment