Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.theonlytails.ruby.tileentity;
- import com.theonlytails.ruby.blocks.RubyBarrelBlock;
- import com.theonlytails.ruby.init.TileEntityTypesRegistry;
- import net.minecraft.block.BlockState;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.entity.player.PlayerInventory;
- import net.minecraft.inventory.container.ChestContainer;
- import net.minecraft.inventory.container.Container;
- import net.minecraft.inventory.container.INamedContainerProvider;
- import net.minecraft.nbt.CompoundNBT;
- import net.minecraft.network.NetworkManager;
- import net.minecraft.network.play.server.SUpdateTileEntityPacket;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.SoundCategory;
- import net.minecraft.util.SoundEvent;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.TranslationTextComponent;
- import net.minecraftforge.common.extensions.IForgeTileEntity;
- import net.minecraftforge.common.util.LazyOptional;
- import net.minecraftforge.items.IItemHandler;
- import net.minecraftforge.items.IItemHandlerModifiable;
- import net.minecraftforge.items.ItemStackHandler;
- import javax.annotation.Nullable;
- public class TileRubyBarrel extends TileEntity implements IForgeTileEntity, INamedContainerProvider {
- public int size = 45;
- public final IItemHandlerModifiable itemHandler = new ItemStackHandler(size) {
- @Override
- protected void onContentsChanged(int slot) {
- super.onContentsChanged(slot);
- markDirty();
- }
- };
- public final LazyOptional<IItemHandler> optional = LazyOptional.of(() -> itemHandler);
- public int players = 0;
- public TileRubyBarrel() {
- super(TileEntityTypesRegistry.RUBY_BARREL.get());
- }
- @Override
- public ITextComponent getDisplayName() {
- return new TranslationTextComponent(this.getBlockState().getBlock().getTranslationKey());
- }
- @Nullable
- @Override
- public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity player) {
- return ChestContainer.createGeneric9X5(id, playerInventory);
- }
- public void changeState(BlockState blockState, boolean value) {
- if (blockState.getBlock() instanceof RubyBarrelBlock) {
- if (world != null) {
- this.world.setBlockState(this.pos, blockState.with(RubyBarrelBlock.PROPERTY_OPEN, value));
- }
- }
- }
- public void playSound(SoundEvent soundEvent) {
- if (this.getBlockState().getBlock() instanceof RubyBarrelBlock) {
- double x = this.pos.getX() + 0.5D;
- double y = this.pos.getY() + 0.5D;
- double z = this.pos.getZ() + 0.5D;
- if (this.world != null) {
- this.world.playSound(null, x, y, z, soundEvent,
- SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
- }
- }
- }
- @Override
- public CompoundNBT write(CompoundNBT compound) {
- return super.write(compound);
- }
- @Override
- public void read(BlockState state, CompoundNBT nbt) {
- super.read(state, nbt);
- }
- @Override
- public CompoundNBT getUpdateTag() {
- return super.write(new CompoundNBT());
- }
- @Nullable
- @Override
- public SUpdateTileEntityPacket getUpdatePacket() {
- CompoundNBT nbt = new CompoundNBT();
- this.write(nbt);
- return new SUpdateTileEntityPacket(this.pos, 0, nbt);
- }
- @Override
- public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
- this.read(this.getBlockState(), pkt.getNbtCompound());
- }
- @Override
- public void remove() {
- super.remove();
- this.optional.invalidate();
- }
- }
Add Comment
Please, Sign In to add comment