Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.theonlytails.ruby.tileentity;
- 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.LockableLootTileEntity;
- import net.minecraft.tileentity.TileEntityType;
- import net.minecraft.util.*;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.TranslationTextComponent;
- import net.minecraftforge.common.capabilities.Capability;
- import net.minecraftforge.common.util.LazyOptional;
- import net.minecraftforge.items.CapabilityItemHandler;
- import net.minecraftforge.items.IItemHandlerModifiable;
- import net.minecraftforge.items.wrapper.InvWrapper;
- import org.jetbrains.annotations.NotNull;
- import org.jetbrains.annotations.Nullable;
- public class RubyBarrelTileEntity extends LockableLootTileEntity {
- private final IItemHandlerModifiable items = createHandler();
- protected int numPlayersUsing;
- private NonNullList<ItemStack> chestContents = NonNullList.withSize(45, ItemStack.EMPTY);
- private LazyOptional<IItemHandlerModifiable> itemHandler = LazyOptional.of(() -> items);
- public RubyBarrelTileEntity(TileEntityType<?> typeIn) {
- super(typeIn);
- }
- public RubyBarrelTileEntity() {
- this(TileEntitiesRegistry.RUBY_BARREL.get());
- }
- @NotNull
- public NonNullList<ItemStack> getItems() {
- return this.chestContents;
- }
- protected void setItems(@NotNull NonNullList<ItemStack> itemsIn) {
- chestContents = itemsIn;
- }
- @Override
- public @NotNull ITextComponent getDefaultName() {
- return new TranslationTextComponent("container.ruby_barrel");
- }
- @Override
- public @NotNull Container createMenu(final int id, @NotNull final PlayerInventory playerInv) {
- return ChestContainer.createGeneric9X5(id, playerInv);
- }
- public int getSizeInventory() {
- return 45;
- }
- @Override
- public void read(@NotNull BlockState state, @NotNull CompoundNBT nbt) {
- super.read(state, nbt);
- this.chestContents = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
- if (!this.checkLootAndRead(nbt)) {
- ItemStackHelper.loadAllItems(nbt, this.chestContents);
- }
- }
- private void playSound(SoundEvent sound) {
- double dx = (double) this.pos.getX() + 0.5D;
- double dy = (double) this.pos.getY() + 0.5D;
- double dz = (double) this.pos.getZ() + 0.5D;
- if (this.world != null) {
- this.world.playSound(null,
- dx, dy, dz, sound,
- SoundCategory.BLOCKS, 0.5f,
- this.world.rand.nextFloat() * 0.1f + 0.9f);
- }
- }
- @Override
- public boolean receiveClientEvent(int id, int type) {
- if (id == 1) {
- this.numPlayersUsing = type;
- return true;
- } else {
- return super.receiveClientEvent(id, type);
- }
- }
- public void openInventory(@NotNull PlayerEntity player) {
- if (!player.isSpectator()) {
- if (this.numPlayersUsing < 0) {
- this.numPlayersUsing = 0;
- }
- ++this.numPlayersUsing;
- BlockState blockstate = this.getBlockState();
- boolean flag = blockstate.get(BarrelBlock.PROPERTY_OPEN);
- if (!flag) {
- this.playSound(SoundEvents.BLOCK_BARREL_OPEN);
- this.setOpenProperty(blockstate, true);
- }
- this.scheduleTick();
- }
- }
- private void scheduleTick() {
- assert this.world != null;
- this.world.getPendingBlockTicks().scheduleTick(this.getPos(), this.getBlockState().getBlock(), 5);
- }
- @Override
- public void updateContainingBlockInfo() {
- super.updateContainingBlockInfo();
- if (this.itemHandler != null) {
- this.itemHandler.invalidate();
- this.itemHandler = null;
- }
- }
- @Override
- public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
- if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
- return itemHandler.cast();
- }
- return super.getCapability(cap, side);
- }
- private IItemHandlerModifiable createHandler() {
- return new InvWrapper(this);
- }
- @Override
- public void remove() {
- super.remove();
- if (itemHandler != null) {
- this.itemHandler.invalidate();
- }
- }
- private void setOpenProperty(BlockState state, boolean open) {
- assert this.world != null;
- this.world.setBlockState(this.getPos(), state.with(BarrelBlock.PROPERTY_OPEN, open), 3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment