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.container.RubyBarrelContainer;
- import com.theonlytails.ruby.init.TileEntitiesRegistry;
- import net.minecraft.block.BarrelBlock;
- import net.minecraft.block.Block;
- 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.Container;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.CompoundNBT;
- import net.minecraft.tileentity.LockableLootTileEntity;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.tileentity.TileEntityType;
- import net.minecraft.util.*;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.TranslationTextComponent;
- import net.minecraft.world.IBlockReader;
- 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());
- }
- public static int getPlayersUsing(IBlockReader reader, BlockPos pos) {
- BlockState blockState = reader.getBlockState(pos);
- if (blockState.hasTileEntity()) {
- TileEntity tileEntity = reader.getTileEntity(pos);
- if (tileEntity instanceof RubyBarrelTileEntity) {
- return ((RubyBarrelTileEntity) tileEntity).numPlayersUsing;
- }
- }
- return 0;
- }
- public static void swapContents(RubyBarrelTileEntity te, RubyBarrelTileEntity otherTe) {
- NonNullList<ItemStack> list = te.getItems();
- te.setItems(otherTe.getItems());
- otherTe.setItems(list);
- }
- @Override
- @NotNull
- public NonNullList<ItemStack> getItems() {
- return this.chestContents;
- }
- @Override
- protected void setItems(@NotNull NonNullList<ItemStack> itemsIn) {
- chestContents = itemsIn;
- }
- @Override
- protected @NotNull ITextComponent getDefaultName() {
- return new TranslationTextComponent("container.ruby_barrel");
- }
- @Override
- protected @NotNull Container createMenu(int id, @NotNull PlayerInventory player) {
- return new RubyBarrelContainer(id, player, this);
- }
- @Override
- 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);
- }
- }
- @Override
- 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(RubyBarrelBlock.OPEN_PROPERTY);
- if (!flag) {
- this.playSound(SoundEvents.BLOCK_BARREL_OPEN);
- this.setOpenProperty(blockState, true);
- }
- this.onOpenOrClose();
- }
- }
- protected void onOpenOrClose() {
- Block block = this.getBlockState().getBlock();
- if (world != null) {
- if (block instanceof RubyBarrelBlock) {
- this.world.addBlockEvent(this.pos, block, 1, this.numPlayersUsing);
- this.world.notifyNeighborsOfStateChange(this.pos, block);
- }
- }
- }
- @Override
- public void updateContainingBlockInfo() {
- super.updateContainingBlockInfo();
- if (this.itemHandler != null) {
- this.itemHandler.invalidate();
- this.itemHandler = null;
- }
- }
- @Nullable
- @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