Advertisement
Creepinson

Untitled

Nov 19th, 2020
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. package me.creepinson.pedestals.tile;
  2.  
  3. import me.creepinson.pedestals.PedestalsRegistryHandler;
  4. import net.minecraft.block.BlockState;
  5. import net.minecraft.item.ItemStack;
  6. import net.minecraft.nbt.CompoundNBT;
  7. import net.minecraft.network.NetworkManager;
  8. import net.minecraft.network.play.server.SUpdateTileEntityPacket;
  9. import net.minecraft.tileentity.ITickableTileEntity;
  10. import net.minecraft.tileentity.TileEntity;
  11. import net.minecraft.util.Direction;
  12. import net.minecraft.util.math.AxisAlignedBB;
  13. import net.minecraftforge.common.capabilities.Capability;
  14. import net.minecraftforge.common.util.LazyOptional;
  15. import net.minecraftforge.items.CapabilityItemHandler;
  16. import net.minecraftforge.items.ItemStackHandler;
  17. import org.jetbrains.annotations.NotNull;
  18.  
  19. import java.util.UUID;
  20.  
  21. public class PedestalTile extends TileEntity implements ITickableTileEntity {
  22.     public double speed = 2;
  23.     private final LazyOptional<ItemStackHandler> lazyHandler;
  24.     private final ItemStackHandler handler = new ItemStackHandler(1);
  25.     private boolean locked;
  26.     public boolean hovering = true;
  27.     protected UUID owner;
  28.     private int age;
  29.  
  30.     public PedestalTile() {
  31.         super(PedestalsRegistryHandler.PEDESTAL_TILE.get());
  32.         this.lazyHandler = LazyOptional.of(() -> handler);
  33.     }
  34.  
  35.     public ItemStack getStack() {
  36.         return handler.getStackInSlot(0);
  37.     }
  38.  
  39.     public void setStack(ItemStack stack) {
  40.         this.handler.setStackInSlot(0, stack);
  41.         sendUpdates();
  42.     }
  43.  
  44.     public void sendUpdates() {
  45.         if (world != null) {
  46.             BlockState state = getBlockState();
  47.             world.updateComparatorOutputLevel(pos, state.getBlock());
  48.             world.markBlockRangeForRenderUpdate(pos, state, state);
  49.             world.notifyBlockUpdate(pos, state, state, 3);
  50.             sendUpdates();
  51.         }
  52.     }
  53.  
  54.     @NotNull
  55.     @Override
  56.     public CompoundNBT getUpdateTag() {
  57.         // getUpdateTag() is called whenever the chunkdata is sent to the
  58.         // client. In contrast getUpdatePacket() is called when the tile entity
  59.         // itself wants to sync to the client. In many cases you want to send
  60.         // over the same information in getUpdateTag() as in getUpdatePacket().
  61.         return write(new CompoundNBT());
  62.     }
  63.  
  64.     @Override
  65.     public SUpdateTileEntityPacket getUpdatePacket() {
  66.         // Prepare a packet for syncing our TE to the client. Since we only have to sync
  67.         // the stack
  68.         // and that's all we have we just write our entire NBT here. If you have a
  69.         // complex
  70.         // tile entity that doesn't need to have all information on the client you can
  71.         // write
  72.         // a more optimal NBT here.
  73.         CompoundNBT nbtTag = new CompoundNBT();
  74.         this.write(nbtTag);
  75.         return new SUpdateTileEntityPacket(getPos(), 0, nbtTag);
  76.     }
  77.  
  78.     @Override
  79.     public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket packet) {
  80.         // Here we get the packet from the server and read it into our client side tile
  81.         // entity
  82.         this.read(getBlockState(), packet.getNbtCompound());
  83.         sendUpdates();
  84.     }
  85.  
  86.     @Override
  87.     public void read(@NotNull BlockState state, @NotNull CompoundNBT compound) {
  88.         super.read(state, compound);
  89.         if (compound.contains("item"))
  90.             setStack(ItemStack.read(compound.getCompound("item")));
  91.         else
  92.             setStack(ItemStack.EMPTY);
  93.         this.locked = compound.getBoolean("locked");
  94.         this.owner = compound.getUniqueId("owner");
  95.     }
  96.  
  97.     @NotNull
  98.     @Override
  99.     public CompoundNBT write(@NotNull CompoundNBT compound) {
  100.         super.write(compound);
  101.         if (!isEmpty()) {
  102.             CompoundNBT tagCompound = new CompoundNBT();
  103.             handler.getStackInSlot(0).write(tagCompound);
  104.             compound.put("item", tagCompound);
  105.         }
  106.  
  107.         compound.putBoolean("locked", this.locked);
  108.         if (this.owner != null)
  109.             compound.putUniqueId("owner", this.owner);
  110.  
  111.         return compound;
  112.     }
  113.  
  114.     public boolean isEmpty() {
  115.         return handler.getStackInSlot(0) == ItemStack.EMPTY;
  116.     }
  117.  
  118.     public boolean isLocked() {
  119.         return this.locked;
  120.     }
  121.  
  122.     public void setLocked(boolean b) {
  123.         this.locked = b;
  124.     }
  125.  
  126.     public void setOwner(UUID uniqueID) {
  127.         this.owner = uniqueID;
  128.     }
  129.  
  130.     public UUID getOwner() {
  131.         return this.owner;
  132.     }
  133.  
  134.     @NotNull
  135.     @Override
  136.     public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, Direction side) {
  137.         return cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? (LazyOptional<T>) lazyHandler
  138.                 : LazyOptional.empty();
  139.     }
  140.  
  141.     @Override
  142.     public AxisAlignedBB getRenderBoundingBox() {
  143.         return INFINITE_EXTENT_AABB;
  144.     }
  145.  
  146.     public int getOutputLevel() {
  147.         if (!this.isEmpty()) return this.getStack().getCount();
  148.         else return 0;
  149.     }
  150.  
  151.     public int getAge() {
  152.         return this.age;
  153.     }
  154.  
  155.     @Override
  156.     public void tick() {
  157.         if (!this.removed) this.age++;
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement