Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.monsterhunter.tileentity;
- import com.monsterhunter.item.material.RecolorableItem;
- import net.minecraft.block.BlockChest;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.init.SoundEvents;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.ContainerChest;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.InventoryLargeChest;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.tileentity.TileEntityLockableLoot;
- import net.minecraft.util.ITickable;
- import net.minecraft.util.SoundCategory;
- import net.minecraft.util.math.AxisAlignedBB;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.TextComponentString;
- import net.minecraft.util.text.TextComponentTranslation;
- public class MaterialChestTileEntity extends TileEntityLockableLoot implements ITickable, IInventory {
- //private List<ItemStack> inventory;
- private ItemStack[] inventory;
- private final int SIZE = 27;
- private final int MAX_STACK = 64;
- public float lidAngle;
- public float prevLidAngle;
- public int numPlayersUsing;
- private int ticksSinceSync;
- private String customName;
- private static final String DEFAULT_NAME = "container.material_chest_tile_entity";
- public MaterialChestTileEntity() {
- this.inventory = new ItemStack[SIZE];
- }
- public String getCustomName() {
- return this.customName;
- }
- public void setCustomName(String customName) {
- this.customName = customName;
- }
- @Override
- public String getName() {
- return this.hasCustomName() ? this.customName : DEFAULT_NAME;
- }
- @Override
- public boolean hasCustomName() {
- return this.customName != null && !this.customName.equals("");
- }
- @Override
- public ITextComponent getDisplayName() {
- return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
- }
- @Override
- public int getSizeInventory() {
- return SIZE;
- }
- @Override
- public ItemStack getStackInSlot(int index) {
- if (index < 0 || index >= this.getSizeInventory())
- return null;
- return this.inventory[index];
- }
- @Override
- public ItemStack decrStackSize(int index, int count) {
- if (this.getStackInSlot(index) != null) {
- ItemStack itemstack;
- if (this.getStackInSlot(index).stackSize <= count) {
- itemstack = this.getStackInSlot(index);
- this.setInventorySlotContents(index, null);
- this.markDirty();
- return itemstack;
- } else {
- itemstack = this.getStackInSlot(index).splitStack(count);
- if (this.getStackInSlot(index).stackSize <= 0) {
- this.setInventorySlotContents(index, null);
- } else {
- //Just to show that changes happened
- this.setInventorySlotContents(index, this.getStackInSlot(index));
- }
- this.markDirty();
- return itemstack;
- }
- } else {
- return null;
- }
- }
- @Override
- public ItemStack removeStackFromSlot(int index) {
- if(this.getStackInSlot(index) != null) {
- ItemStack temp = this.getStackInSlot(index);
- inventory[index] = null;
- return temp;
- } else {
- return null;
- }
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack) {
- if (index < 0 || index >= this.getSizeInventory())
- return;
- if (stack != null && stack.stackSize > this.getInventoryStackLimit())
- stack.stackSize = this.getInventoryStackLimit();
- if (stack != null && stack.stackSize == 0)
- stack = null;
- this.inventory[index] = stack;
- this.markDirty();
- }
- @Override
- public int getInventoryStackLimit() {
- return MAX_STACK;
- }
- @Override
- public boolean isUseableByPlayer(EntityPlayer player) {
- return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
- }
- public void openInventory(EntityPlayer player)
- {
- if (!player.isSpectator())
- {
- if (this.numPlayersUsing < 0)
- {
- this.numPlayersUsing = 0;
- }
- ++this.numPlayersUsing;
- this.worldObj.addBlockEvent(this.pos, this.getBlockType(), 1, this.numPlayersUsing);
- this.worldObj.notifyNeighborsOfStateChange(this.pos, this.getBlockType());
- this.worldObj.notifyNeighborsOfStateChange(this.pos.down(), this.getBlockType());
- }
- }
- public void closeInventory(EntityPlayer player)
- {
- if (!player.isSpectator() && this.getBlockType() instanceof BlockChest)
- {
- --this.numPlayersUsing;
- this.worldObj.addBlockEvent(this.pos, this.getBlockType(), 1, this.numPlayersUsing);
- this.worldObj.notifyNeighborsOfStateChange(this.pos, this.getBlockType());
- this.worldObj.notifyNeighborsOfStateChange(this.pos.down(), this.getBlockType());
- }
- }
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack) {
- return stack.getItem() instanceof RecolorableItem;
- }
- @Override
- public int getField(int id) {
- return 0;
- }
- @Override
- public void setField(int id, int value) {
- }
- @Override
- public int getFieldCount() {
- return 0;
- }
- @Override
- public void clear() {
- for(int i = 0; i < this.getSizeInventory(); i++) {
- this.setInventorySlotContents(i, null);
- }
- }
- @Override
- public void writeToNBT(NBTTagCompound nbt) {
- super.writeToNBT(nbt);
- NBTTagList list = new NBTTagList();
- for (int i = 0; i < this.getSizeInventory(); ++i) {
- if (this.getStackInSlot(i) != null) {
- NBTTagCompound stackTag = new NBTTagCompound();
- stackTag.setByte("Slot", (byte) i);
- this.getStackInSlot(i).writeToNBT(stackTag);
- list.appendTag(stackTag);
- }
- }
- nbt.setTag("Items", list);
- if (this.hasCustomName()) {
- nbt.setString("CustomName", this.getCustomName());
- }
- }
- @Override
- public void readFromNBT(NBTTagCompound nbt) {
- super.readFromNBT(nbt);
- NBTTagList list = nbt.getTagList("Items", 10);
- for (int i = 0; i < list.tagCount(); ++i) {
- NBTTagCompound stackTag = list.getCompoundTagAt(i);
- int slot = stackTag.getByte("Slot") & 255;
- this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
- }
- if (nbt.hasKey("CustomName", 8)) {
- this.setCustomName(nbt.getString("CustomName"));
- }
- }
- @Override
- public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
- this.fillWithLoot(playerIn);
- return new ContainerChest(playerInventory, this, playerIn);
- }
- @Override
- public String getGuiID() {
- return "minecraft:chest";
- }
- @Override
- public void update() {
- int i = this.pos.getX();
- int j = this.pos.getY();
- int k = this.pos.getZ();
- ++ticksSinceSync;
- if (!this.worldObj.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + i + j + k) % 200 == 0) {
- this.numPlayersUsing = 0;
- float f = 5.0F;
- for (EntityPlayer entityplayer : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, new AxisAlignedBB((double)((float)i - f), (double)((float)j - f), (double)((float)k - f), (double)((float)(i + 1) + f), (double)((float)(j + 1) + f), (double)((float)(k + 1) + f))))
- {
- if (entityplayer.openContainer instanceof ContainerChest)
- {
- IInventory iinventory = ((ContainerChest)entityplayer.openContainer).getLowerChestInventory();
- if (iinventory == this || iinventory instanceof InventoryLargeChest && ((InventoryLargeChest)iinventory).isPartOfLargeChest(this))
- {
- ++this.numPlayersUsing;
- }
- }
- }
- }
- this.prevLidAngle = this.lidAngle;
- float f1 = 0.1F;
- if (this.numPlayersUsing > 0 && this.lidAngle == 0.0F )
- {
- double d1 = (double)i + 0.5D;
- double d2 = (double)k + 0.5D;
- this.worldObj.playSound((EntityPlayer)null, d1, (double)j + 0.5D, d2, SoundEvents.block_chest_open, SoundCategory.BLOCKS, 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
- }
- if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F)
- {
- float f2 = this.lidAngle;
- if (this.numPlayersUsing > 0)
- {
- this.lidAngle += f1;
- }
- else
- {
- this.lidAngle -= f1;
- }
- if (this.lidAngle > 1.0F)
- {
- this.lidAngle = 1.0F;
- }
- float f3 = 0.5F;
- if (this.lidAngle < f3 && f2 >= f3)
- {
- double d3 = (double)i + 0.5D;
- double d0 = (double)k + 0.5D;
- this.worldObj.playSound((EntityPlayer)null, d3, (double)j + 0.5D, d0, SoundEvents.block_chest_close, SoundCategory.BLOCKS, 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
- }
- if (this.lidAngle < 0.0F)
- {
- this.lidAngle = 0.0F;
- }
- }
- }
- public boolean receiveClientEvent(int id, int type)
- {
- if (id == 1)
- {
- this.numPlayersUsing = type;
- return true;
- }
- else
- {
- return super.receiveClientEvent(id, type);
- }
- }
- }
Add Comment
Please, Sign In to add comment