Guest User

Untitled

a guest
Aug 14th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 KB | None | 0 0
  1. package com.theundertaker11.demonicintervention.tile;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import baubles.common.network.PacketHandler;
  6. import net.minecraft.block.state.IBlockState;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.entity.player.EntityPlayerMP;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  11. import net.minecraft.tileentity.TileEntity;
  12. import net.minecraft.util.EnumFacing;
  13. import net.minecraft.util.math.BlockPos;
  14. import net.minecraft.world.World;
  15. import net.minecraftforge.common.capabilities.Capability;
  16. import net.minecraftforge.items.CapabilityItemHandler;
  17. import net.minecraftforge.items.ItemStackHandler;
  18.  
  19. public class ItemStoringTileBase extends TileEntity{
  20.    
  21.     private int slotNumber = 1;
  22.    
  23.     public ItemStoringTileBase(){super();}
  24.    
  25.     public void setSlotNumber(int i)
  26.     {
  27.         this.slotNumber = i;
  28.     }
  29.    
  30.     public int getNumberOfSlots()
  31.     {
  32.         return this.slotNumber;
  33.     }
  34.    
  35.     @Override
  36.     @Nullable
  37.     public SPacketUpdateTileEntity getUpdatePacket() {
  38.         return new SPacketUpdateTileEntity(this.pos, 3, this.getUpdateTag());
  39.     }
  40.     //If you overwrite this make sure to call the super
  41.     @Override
  42.     public NBTTagCompound writeToNBT(NBTTagCompound compound)
  43.     {
  44.         compound.setTag("inputitem", itemStackHandler.serializeNBT());
  45.         return super.writeToNBT(compound);
  46.     }
  47.    
  48.     //If you overwrite this make sure to call the super
  49.     @Override
  50.     public void readFromNBT(NBTTagCompound compound)
  51.     {
  52.         super.readFromNBT(compound);
  53.         if (compound.hasKey("inputitem"))
  54.         {
  55.             itemStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("inputitem"));
  56.         }
  57.     }
  58.    
  59.     protected ItemStackHandler itemStackHandler = new ItemStackHandler(this.slotNumber){
  60.         @Override
  61.         protected void onContentsChanged(int slot){
  62.             markDirty();
  63.         }
  64.     };
  65.    
  66.     @Override
  67.     public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
  68.         if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY){
  69.             return true;
  70.         }
  71.         return super.hasCapability(capability, facing);
  72.     }
  73.  
  74.     @Override
  75.     public <T> T getCapability(Capability<T> capability, EnumFacing facing)
  76.     {
  77.         if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
  78.         {
  79.             return (T) itemStackHandler;
  80.         }
  81.         return super.getCapability(capability, facing);
  82.     }
  83.  
  84.     @Override
  85.     public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState)
  86.     {
  87.         return (oldState.getBlock() != newState.getBlock());
  88.     }
  89.     @Override
  90.     public void onLoad()
  91.     {
  92.         updateEverything();
  93.     }
  94.    
  95.     private void updateEverything() {
  96.         world.markBlockRangeForRenderUpdate(pos, pos);
  97.         world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);
  98.         world.scheduleBlockUpdate(pos,this.getBlockType(),0,0);
  99.         markDirty();
  100.     }
  101.     /*
  102.     public void syncToClients() {
  103.         if (this.world != null && !this.world.isRemote) {
  104.             for (EntityPlayer player : this.world.playerEntities) {
  105.                 syncToClient(player);
  106.             }
  107.         }
  108.     }
  109.  
  110.     public void syncToClient(EntityPlayer player) {
  111.         NBTTagCompound syncTag = new NBTTagCompound();
  112.         this.writeToNBT(syncTag);
  113.  
  114.         if (player instanceof EntityPlayerMP && player.getDistance(pos.getX(), pos.getY(), pos.getZ()) <= this.getMaxSyncDistanceSquared()) {
  115.             PacketHandler.INSTANCE.sendTo(new SPacketSyncTileEntity(syncTag, this.pos), (EntityPlayerMP) player);
  116.         }
  117.     }
  118.     */
  119. }
Advertisement
Add Comment
Please, Sign In to add comment