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.TileEntitiesRegistry;
- import net.minecraft.block.BarrelBlock;
- import net.minecraft.block.BlockState;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.entity.player.PlayerInventory;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.inventory.container.ChestContainer;
- import net.minecraft.inventory.container.Container;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.CompoundNBT;
- import net.minecraft.tileentity.ChestTileEntity;
- import net.minecraft.tileentity.LockableLootTileEntity;
- import net.minecraft.tileentity.TileEntityType;
- import net.minecraft.util.NonNullList;
- import net.minecraft.util.SoundCategory;
- import net.minecraft.util.SoundEvent;
- import net.minecraft.util.SoundEvents;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.TranslationTextComponent;
- import org.jetbrains.annotations.NotNull;
- import static com.theonlytails.ruby.blocks.RubyBarrelBlock.*;
- public class RubyBarrelTileEntity extends LockableLootTileEntity {
- protected int numPlayersUsing;
- private NonNullList<ItemStack> contents = NonNullList.withSize(45, ItemStack.EMPTY);
- public RubyBarrelTileEntity(TileEntityType<?> typeIn) {
- super(typeIn);
- }
- public RubyBarrelTileEntity() {
- this(TileEntitiesRegistry.RUBY_BARREL.get());
- }
- @Override
- public @NotNull CompoundNBT write(@NotNull CompoundNBT nbt) {
- super.write(nbt);
- if (!checkLootAndWrite(nbt)) {
- ItemStackHelper.saveAllItems(nbt, getItems());
- }
- return nbt;
- }
- @Override
- protected @NotNull ITextComponent getDefaultName() {
- return new TranslationTextComponent(this.getBlockState().getBlock().getTranslationKey());
- }
- @Override
- protected @NotNull Container createMenu(int id, @NotNull PlayerInventory player) {
- return ChestContainer.createGeneric9X5(id, player);
- }
- @Override
- @NotNull
- public NonNullList<ItemStack> getItems() {
- return this.contents;
- }
- @Override
- protected void setItems(@NotNull NonNullList<ItemStack> itemsIn) {
- this.contents = itemsIn;
- }
- @Override
- public int getSizeInventory() {
- return 45;
- }
- @Override
- public void openInventory(@NotNull PlayerEntity player) {
- if (!player.isSpectator()) {
- if (numPlayersUsing < 0) {
- numPlayersUsing = 0;
- }
- }
- numPlayersUsing++;
- BlockState blockState = this.getBlockState();
- if (!blockState.get(OPEN)) {
- this.playSound(SoundEvents.BLOCK_BARREL_OPEN);
- this.setOpenProperty(blockState, true);
- }
- this.scheduleTick();
- }
- private void scheduleTick() {
- assert this.world != null;
- this.world.getPendingBlockTicks().scheduleTick(this.pos, this.getBlockState().getBlock(), 5);
- }
- public void barrelTick() {
- int x = pos.getX();
- int y = pos.getY();
- int z = pos.getZ();
- assert world != null;
- numPlayersUsing = ChestTileEntity.calculatePlayersUsing(world, this, x, y, z);
- if (numPlayersUsing > 0) {
- scheduleTick();
- } else {
- BlockState blockstate = getBlockState();
- if (!(blockstate.getBlock() instanceof RubyBarrelBlock)) {
- remove();
- return;
- }
- boolean flag = blockstate.get(BarrelBlock.PROPERTY_OPEN);
- if (flag) {
- this.playSound(SoundEvents.BLOCK_BARREL_CLOSE);
- this.setOpenProperty(blockstate, false);
- }
- }
- }
- @Override
- public void closeInventory(PlayerEntity player) {
- if (!player.isSpectator()) {
- numPlayersUsing--;
- }
- }
- private void setOpenProperty(BlockState blockstate, boolean open) {
- assert world != null;
- world.setBlockState(this.pos, blockstate.with(OPEN, open), 3);
- }
- private void playSound(SoundEvent soundEvent) {
- double x = (double) this.pos.getX() + 0.5D;
- double y = (double) this.pos.getY() + 0.5D;
- double z = (double) this.pos.getZ() + 0.5D;
- assert world != null;
- world.playSound(null, x, y, z, soundEvent, SoundCategory.BLOCKS, 0.5F,
- this.world.rand.nextFloat() * 0.1f + 0.9f);
- }
- @Override
- public @NotNull CompoundNBT getUpdateTag() {
- return write(new CompoundNBT());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment