Advertisement
Creepinson

Untitled

May 12th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. package me.creepinson.entities.tileentity;
  2.  
  3. import net.minecraft.block.state.IBlockState;
  4. import net.minecraft.item.ItemStack;
  5. import net.minecraft.nbt.NBTTagCompound;
  6. import net.minecraft.network.NetworkManager;
  7. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  8. import net.minecraft.tileentity.TileEntity;
  9.  
  10. public class TileEntityPedastal_Magic extends TileEntity {
  11.  
  12. private ItemStack stack;
  13.  
  14. public ItemStack getStack() {
  15. return stack;
  16. }
  17.  
  18. public void setStack(ItemStack stack) {
  19. this.stack = stack;
  20. markDirty();
  21. if (world != null) {
  22. IBlockState state = world.getBlockState(getPos());
  23. world.notifyBlockUpdate(getPos(), state, state, 3);
  24. }
  25. }
  26.  
  27. @Override
  28. public NBTTagCompound getUpdateTag() {
  29. // getUpdateTag() is called whenever the chunkdata is sent to the
  30. // client. In contrast getUpdatePacket() is called when the tile entity
  31. // itself wants to sync to the client. In many cases you want to send
  32. // over the same information in getUpdateTag() as in getUpdatePacket().
  33. return writeToNBT(new NBTTagCompound());
  34. }
  35.  
  36. @Override
  37. public SPacketUpdateTileEntity getUpdatePacket() {
  38. // Prepare a packet for syncing our TE to the client. Since we only have to sync the stack
  39. // and that's all we have we just write our entire NBT here. If you have a complex
  40. // tile entity that doesn't need to have all information on the client you can write
  41. // a more optimal NBT here.
  42. NBTTagCompound nbtTag = new NBTTagCompound();
  43. this.writeToNBT(nbtTag);
  44. return new SPacketUpdateTileEntity(getPos(), 1, nbtTag);
  45. }
  46.  
  47. @Override
  48. public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
  49. // Here we get the packet from the server and read it into our client side tile entity
  50. this.readFromNBT(packet.getNbtCompound());
  51. }
  52.  
  53. @Override
  54. public void readFromNBT(NBTTagCompound compound) {
  55. super.readFromNBT(compound);
  56. if (compound.hasKey("item")) {
  57. stack = new ItemStack(compound.getCompoundTag("item"));
  58. } else {
  59. stack = ItemStack.EMPTY;
  60. }
  61. }
  62.  
  63. @Override
  64. public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  65.  
  66. if (stack != ItemStack.EMPTY) {
  67. NBTTagCompound tagCompound = new NBTTagCompound();
  68.  
  69. compound.setTag("item", tagCompound);
  70.  
  71. }
  72. return super.writeToNBT(compound);
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement