Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. public class TileEntityMyBlock extends TileEntity {
  2. public static final int SIZE = 3;
  3.  
  4. public boolean previousRedstoneState;
  5.  
  6. private ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE) {
  7. @Override
  8. protected void onContentsChanged(int slot) {
  9. sendUpdates();
  10. }
  11. };
  12.  
  13. @Override
  14. public void readFromNBT(NBTTagCompound compound) {
  15. super.readFromNBT(compound);
  16. if (compound.hasKey("ITEMS")) {
  17. itemStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("ITEMS"));
  18. }
  19. if (compound.hasKey("previousRedstoneState")) {
  20. previousRedstoneState = compound.getBoolean("previousRedstoneState");
  21. }
  22. }
  23.  
  24. @Override
  25. public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  26. super.writeToNBT(compound);
  27. compound.setTag("ITEMS", itemStackHandler.serializeNBT());
  28. compound.setBoolean("previousRedstoneState", previousRedstoneState);
  29. return compound;
  30. }
  31.  
  32. @Nullable
  33. @Override
  34. public SPacketUpdateTileEntity getUpdatePacket() {
  35. return new SPacketUpdateTileEntity(getPos(), getBlockMetadata(), getUpdateTag());
  36. }
  37.  
  38. @Override
  39. public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
  40. handleUpdateTag(pkt.getNbtCompound());
  41. }
  42.  
  43. @Override
  44. public NBTTagCompound getUpdateTag() {
  45. return new NBTTagCompound();
  46. }
  47.  
  48. @Override
  49. public void handleUpdateTag(NBTTagCompound tag) {
  50. this.readFromNBT(tag);
  51. }
  52.  
  53. public boolean canInteractWith(EntityPlayer playerIn) {
  54. return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
  55. }
  56.  
  57. @Override
  58. public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
  59. if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  60. return true;
  61. }
  62. return super.hasCapability(capability, facing);
  63. }
  64.  
  65. @Override
  66. public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  67. if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  68. return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(itemStackHandler);
  69. }
  70. return super.getCapability(capability, facing);
  71. }
  72.  
  73. public ItemStack getStackInSlot(int slot) {
  74. return itemStackHandler.getStackInSlot(slot);
  75. }
  76.  
  77. public boolean isActivated() {
  78. IBlockState state = world.getBlockState(pos);
  79. if (state.getBlock() == ModBlocks.MY_BLOCK) {
  80. return state.getValue(MyBlock.ACTIVATED);
  81. }
  82.  
  83. MyMod.LOGGER.info(pos.getX() + " " + pos.getY() + " " + pos.getZ() + " " + state.getBlock().getUnlocalizedName()); // the debug from before
  84.  
  85. return false;
  86. }
  87.  
  88. private void sendUpdates() {
  89. world.markBlockRangeForRenderUpdate(pos, pos);
  90. world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);
  91. world.scheduleBlockUpdate(pos, this.getBlockType(),0,0);
  92. markDirty();
  93. }
  94.  
  95. @Override
  96. public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
  97. return (oldState.getBlock() != newState.getBlock());
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement