2014-09-24 14:32:58 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
* Copyright (c) 2012 cpw.
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the GNU Public License v3.0
|
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
|
* http://www.gnu.org/licenses/gpl.html
|
2016-05-19 01:47:25 +02:00
|
|
|
* <p>
|
2014-09-24 14:32:58 +02:00
|
|
|
* Contributors:
|
2016-05-19 01:47:25 +02:00
|
|
|
* cpw - initial API and implementation
|
2014-09-24 14:32:58 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
package cpw.mods.ironchest;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Comparator;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
2014-09-24 14:32:58 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2014-09-24 14:32:58 +02:00
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2014-09-25 12:08:22 +02:00
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
2016-03-21 17:44:27 +01:00
|
|
|
import net.minecraft.init.SoundEvents;
|
2014-09-25 12:08:22 +02:00
|
|
|
import net.minecraft.inventory.Container;
|
2014-09-24 14:32:58 +02:00
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2015-05-27 23:17:59 +02:00
|
|
|
import net.minecraft.network.NetworkManager;
|
2016-03-21 17:44:27 +01:00
|
|
|
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
|
2014-09-25 12:08:22 +02:00
|
|
|
import net.minecraft.tileentity.TileEntityLockable;
|
2014-12-02 21:25:03 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2015-11-23 06:34:34 +01:00
|
|
|
import net.minecraft.util.ITickable;
|
2016-03-21 17:44:27 +01:00
|
|
|
import net.minecraft.util.SoundCategory;
|
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
2015-05-27 23:17:59 +02:00
|
|
|
import net.minecraftforge.common.util.Constants;
|
2014-09-24 14:32:58 +02:00
|
|
|
|
2015-11-23 06:34:34 +01:00
|
|
|
public class TileEntityIronChest extends TileEntityLockable implements ITickable, IInventory
|
2014-09-25 12:08:22 +02:00
|
|
|
{
|
2014-09-24 14:32:58 +02:00
|
|
|
private int ticksSinceSync = -1;
|
|
|
|
|
public float prevLidAngle;
|
|
|
|
|
public float lidAngle;
|
2016-06-08 21:59:44 +02:00
|
|
|
private int numPlayersUsing;
|
2014-09-24 14:32:58 +02:00
|
|
|
public ItemStack[] chestContents;
|
|
|
|
|
private ItemStack[] topStacks;
|
2016-05-19 01:47:25 +02:00
|
|
|
private EnumFacing facing;
|
2014-09-24 14:32:58 +02:00
|
|
|
private boolean inventoryTouched;
|
|
|
|
|
private boolean hadStuff;
|
2014-09-25 12:08:22 +02:00
|
|
|
private String customName;
|
2014-09-24 14:32:58 +02:00
|
|
|
|
|
|
|
|
public TileEntityIronChest()
|
|
|
|
|
{
|
|
|
|
|
this(IronChestType.IRON);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected TileEntityIronChest(IronChestType type)
|
|
|
|
|
{
|
|
|
|
|
super();
|
2016-05-19 01:47:25 +02:00
|
|
|
this.chestContents = new ItemStack[type.size];
|
2014-09-24 14:32:58 +02:00
|
|
|
this.topStacks = new ItemStack[8];
|
2016-05-19 01:47:25 +02:00
|
|
|
this.facing = EnumFacing.NORTH;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2015-05-27 23:17:59 +02:00
|
|
|
|
|
|
|
|
public void setContents(ItemStack[] contents)
|
2015-01-29 17:58:38 +01:00
|
|
|
{
|
2016-05-19 01:47:25 +02:00
|
|
|
this.chestContents = new ItemStack[this.getType().size];
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2015-01-29 17:58:38 +01:00
|
|
|
for (int i = 0; i < contents.length; i++)
|
|
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
if (i < this.chestContents.length)
|
2015-01-29 17:58:38 +01:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.chestContents[i] = contents[i];
|
2015-01-29 17:58:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.inventoryTouched = true;
|
2015-01-29 17:58:38 +01:00
|
|
|
}
|
2014-09-24 14:32:58 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getSizeInventory()
|
|
|
|
|
{
|
2016-05-19 01:47:25 +02:00
|
|
|
return this.chestContents.length;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-19 01:47:25 +02:00
|
|
|
public EnumFacing getFacing()
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
return this.facing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IronChestType getType()
|
|
|
|
|
{
|
2016-05-30 19:43:45 +02:00
|
|
|
IronChestType type = IronChestType.WOOD;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-05-30 19:43:45 +02:00
|
|
|
if (this.hasWorldObj())
|
|
|
|
|
{
|
|
|
|
|
IBlockState state = this.worldObj.getBlockState(this.pos);
|
|
|
|
|
if (state.getBlock() == IronChest.ironChestBlock)
|
|
|
|
|
{
|
|
|
|
|
type = state.getValue(BlockIronChest.VARIANT_PROP);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-05-30 19:43:45 +02:00
|
|
|
return type;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-06-08 21:59:44 +02:00
|
|
|
public ItemStack getStackInSlot(int index)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.inventoryTouched = true;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
return this.chestContents[index];
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void markDirty()
|
|
|
|
|
{
|
|
|
|
|
super.markDirty();
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.sortTopStacks();
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void sortTopStacks()
|
|
|
|
|
{
|
2016-05-19 01:47:25 +02:00
|
|
|
if (!this.getType().isTransparent() || (this.worldObj != null && this.worldObj.isRemote))
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
ItemStack[] tempCopy = new ItemStack[this.getSizeInventory()];
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2014-09-24 14:32:58 +02:00
|
|
|
boolean hasStuff = false;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2014-09-24 14:32:58 +02:00
|
|
|
int compressedIdx = 0;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
mainLoop: for (int i = 0; i < this.getSizeInventory(); i++)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
if (this.chestContents[i] != null)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
for (int j = 0; j < compressedIdx; j++)
|
|
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
if (tempCopy[j].isItemEqual(this.chestContents[i]))
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
tempCopy[j].stackSize += this.chestContents[i].stackSize;
|
2014-09-24 14:32:58 +02:00
|
|
|
continue mainLoop;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-21 17:44:27 +01:00
|
|
|
tempCopy[compressedIdx++] = this.chestContents[i].copy();
|
2014-09-24 14:32:58 +02:00
|
|
|
hasStuff = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
if (!hasStuff && this.hadStuff)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.hadStuff = false;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
for (int i = 0; i < this.topStacks.length; i++)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.topStacks[i] = null;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
if (this.worldObj != null)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
IBlockState iblockstate = this.worldObj.getBlockState(this.pos);
|
|
|
|
|
this.worldObj.notifyBlockUpdate(this.pos, iblockstate, iblockstate, 3);
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2014-09-24 14:32:58 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.hadStuff = true;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
Arrays.sort(tempCopy, new Comparator<ItemStack>() {
|
2014-09-24 14:32:58 +02:00
|
|
|
@Override
|
|
|
|
|
public int compare(ItemStack o1, ItemStack o2)
|
|
|
|
|
{
|
|
|
|
|
if (o1 == null)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
2016-03-21 17:44:27 +01:00
|
|
|
}
|
|
|
|
|
else if (o2 == null)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
return -1;
|
2016-03-21 17:44:27 +01:00
|
|
|
}
|
|
|
|
|
else
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
return o2.stackSize - o1.stackSize;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2014-09-24 14:32:58 +02:00
|
|
|
int p = 0;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
for (ItemStack element : tempCopy)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
if (element != null && element.stackSize > 0)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.topStacks[p++] = element;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
if (p == this.topStacks.length)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
for (int i = p; i < this.topStacks.length; i++)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.topStacks[i] = null;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
if (this.worldObj != null)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
IBlockState iblockstate = this.worldObj.getBlockState(this.pos);
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.worldObj.notifyBlockUpdate(this.pos, iblockstate, iblockstate, 3);
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-06-08 21:59:44 +02:00
|
|
|
public ItemStack decrStackSize(int index, int count)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
if (this.chestContents[index] != null)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
if (this.chestContents[index].stackSize <= count)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
ItemStack stack = this.chestContents[index];
|
|
|
|
|
|
|
|
|
|
this.chestContents[index] = null;
|
|
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.markDirty();
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
return stack;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
ItemStack stack = this.chestContents[index].splitStack(count);
|
|
|
|
|
|
|
|
|
|
if (this.chestContents[index].stackSize == 0)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.chestContents[index] = null;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.markDirty();
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
return stack;
|
2016-03-21 17:44:27 +01:00
|
|
|
}
|
|
|
|
|
else
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-06-08 21:59:44 +02:00
|
|
|
public void setInventorySlotContents(int index, @Nullable ItemStack stack)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.chestContents[index] = stack;
|
|
|
|
|
|
|
|
|
|
if (stack != null && stack.stackSize > this.getInventoryStackLimit())
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
stack.stackSize = this.getInventoryStackLimit();
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.markDirty();
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2015-05-27 23:17:59 +02:00
|
|
|
|
2014-09-25 12:08:22 +02:00
|
|
|
@Override
|
2016-01-12 23:27:56 +01:00
|
|
|
public String getName()
|
2014-09-25 12:08:22 +02:00
|
|
|
{
|
2016-05-19 01:47:25 +02:00
|
|
|
return this.hasCustomName() ? this.customName : this.getType().name();
|
2014-09-25 12:08:22 +02:00
|
|
|
}
|
2015-05-27 23:17:59 +02:00
|
|
|
|
2014-09-25 12:08:22 +02:00
|
|
|
@Override
|
2014-10-01 02:53:12 +02:00
|
|
|
public boolean hasCustomName()
|
2014-09-25 12:08:22 +02:00
|
|
|
{
|
|
|
|
|
return this.customName != null && this.customName.length() > 0;
|
|
|
|
|
}
|
2015-05-27 23:17:59 +02:00
|
|
|
|
2014-09-25 12:08:22 +02:00
|
|
|
public void setCustomName(String name)
|
|
|
|
|
{
|
|
|
|
|
this.customName = name;
|
|
|
|
|
}
|
2014-09-24 14:32:58 +02:00
|
|
|
|
|
|
|
|
@Override
|
2016-06-08 21:59:44 +02:00
|
|
|
public void readFromNBT(NBTTagCompound compound)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
super.readFromNBT(compound);
|
|
|
|
|
|
|
|
|
|
NBTTagList tagList = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
|
2015-05-27 23:17:59 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.chestContents = new ItemStack[this.getSizeInventory()];
|
2015-05-27 23:17:59 +02:00
|
|
|
|
2016-06-08 21:59:44 +02:00
|
|
|
if (compound.hasKey("CustomName", Constants.NBT.TAG_STRING))
|
2014-09-25 12:08:22 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.customName = compound.getString("CustomName");
|
2014-09-25 12:08:22 +02:00
|
|
|
}
|
2015-05-27 23:17:59 +02:00
|
|
|
|
2016-06-08 21:59:44 +02:00
|
|
|
for (int i = 0; i < tagList.tagCount(); i++)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
NBTTagCompound tag = tagList.getCompoundTagAt(i);
|
|
|
|
|
|
|
|
|
|
int slot = tag.getByte("Slot") & 0xff;
|
|
|
|
|
|
|
|
|
|
if (slot >= 0 && slot < this.chestContents.length)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.chestContents[slot] = ItemStack.loadItemStackFromNBT(tag);
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
this.facing = EnumFacing.VALUES[compound.getByte("facing")];
|
|
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.sortTopStacks();
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-06-08 21:59:44 +02:00
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
super.writeToNBT(compound);
|
|
|
|
|
|
|
|
|
|
NBTTagList tagList = new NBTTagList();
|
|
|
|
|
|
|
|
|
|
for (int slot = 0; slot < this.chestContents.length; slot++)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
if (this.chestContents[slot] != null)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
|
|
|
|
|
|
|
|
tag.setByte("Slot", (byte) slot);
|
|
|
|
|
|
|
|
|
|
this.chestContents[slot].writeToNBT(tag);
|
|
|
|
|
|
|
|
|
|
tagList.appendTag(tag);
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-08 21:59:44 +02:00
|
|
|
compound.setTag("Items", tagList);
|
|
|
|
|
|
|
|
|
|
compound.setByte("facing", (byte) this.facing.ordinal());
|
2015-05-27 23:17:59 +02:00
|
|
|
|
2014-10-01 02:53:12 +02:00
|
|
|
if (this.hasCustomName())
|
2014-09-25 12:08:22 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
compound.setString("CustomName", this.customName);
|
2014-09-25 12:08:22 +02:00
|
|
|
}
|
2016-05-20 00:17:38 +02:00
|
|
|
|
2016-06-08 21:59:44 +02:00
|
|
|
return compound;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getInventoryStackLimit()
|
|
|
|
|
{
|
|
|
|
|
return 64;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-06-08 21:59:44 +02:00
|
|
|
public boolean isUseableByPlayer(EntityPlayer player)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
if (this.worldObj == null)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
if (this.worldObj.getTileEntity(this.pos) != this)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
return player.getDistanceSq(this.pos.getX() + 0.5D, this.pos.getY() + 0.5D, this.pos.getZ() + 0.5D) <= 64D;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void update()
|
|
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
// Resynchronizes clients with the server state
|
|
|
|
|
|
|
|
|
|
//@formatter:off
|
|
|
|
|
if (this.worldObj != null && !this.worldObj.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + this.pos.getX() + this.pos.getY() + this.pos.getZ()) % 200 == 0)
|
|
|
|
|
//@formatter:on
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.numPlayersUsing = 0;
|
|
|
|
|
|
|
|
|
|
float f = 5.0F;
|
|
|
|
|
|
2016-04-21 20:29:22 +02:00
|
|
|
//@formatter:off
|
2016-06-08 21:59:44 +02:00
|
|
|
for (EntityPlayer player : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, new AxisAlignedBB(this.pos.getX() - f, this.pos.getY() - f, this.pos.getZ() - f, this.pos.getX() + 1 + f, this.pos.getY() + 1 + f, this.pos.getZ() + 1 + f)))
|
2016-04-21 20:29:22 +02:00
|
|
|
//@formatter:on
|
2015-05-27 23:17:59 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
if (player.openContainer instanceof ContainerIronChest)
|
2015-05-27 23:17:59 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
++this.numPlayersUsing;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
if (this.worldObj != null && !this.worldObj.isRemote && this.ticksSinceSync < 0)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.worldObj.addBlockEvent(this.pos, IronChest.ironChestBlock, 3, ((this.numPlayersUsing << 3) & 0xF8) | (this.facing.ordinal() & 0x7));
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
if (!this.worldObj.isRemote && this.inventoryTouched)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.inventoryTouched = false;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.sortTopStacks();
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.ticksSinceSync++;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
this.prevLidAngle = this.lidAngle;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
float angle = 0.1F;
|
|
|
|
|
|
|
|
|
|
if (this.numPlayersUsing > 0 && this.lidAngle == 0.0F)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
double x = this.pos.getX() + 0.5D;
|
|
|
|
|
double y = this.pos.getY() + 0.5D;
|
|
|
|
|
double z = this.pos.getZ() + 0.5D;
|
|
|
|
|
|
2016-04-21 20:29:22 +02:00
|
|
|
//@formatter:off
|
2016-06-08 21:59:44 +02:00
|
|
|
this.worldObj.playSound(null, x, y, z, SoundEvents.BLOCK_CHEST_OPEN, SoundCategory.BLOCKS, 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
|
2016-04-21 20:29:22 +02:00
|
|
|
//@formatter:on
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
float currentAngle = this.lidAngle;
|
|
|
|
|
|
|
|
|
|
if (this.numPlayersUsing > 0)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.lidAngle += angle;
|
2016-03-21 17:44:27 +01:00
|
|
|
}
|
|
|
|
|
else
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.lidAngle -= angle;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
if (this.lidAngle > 1.0F)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.lidAngle = 1.0F;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
float maxAngle = 0.5F;
|
|
|
|
|
|
|
|
|
|
if (this.lidAngle < maxAngle && currentAngle >= maxAngle)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
double x = this.pos.getX() + 0.5D;
|
|
|
|
|
double y = this.pos.getY() + 0.5D;
|
|
|
|
|
double z = this.pos.getZ() + 0.5D;
|
|
|
|
|
|
2016-04-21 20:29:22 +02:00
|
|
|
//@formatter:off
|
2016-06-08 21:59:44 +02:00
|
|
|
this.worldObj.playSound(null, x, y, z, SoundEvents.BLOCK_CHEST_CLOSE, SoundCategory.BLOCKS, 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
|
2016-04-21 20:29:22 +02:00
|
|
|
//@formatter:on
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
if (this.lidAngle < 0.0F)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.lidAngle = 0.0F;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-06-08 21:59:44 +02:00
|
|
|
public boolean receiveClientEvent(int id, int type)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
if (id == 1)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.numPlayersUsing = type;
|
2016-03-21 17:44:27 +01:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
else if (id == 2)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.facing = EnumFacing.VALUES[type];
|
2016-03-21 17:44:27 +01:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
else if (id == 3)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.facing = EnumFacing.VALUES[type & 0x7];
|
|
|
|
|
this.numPlayersUsing = (type & 0xF8) >> 3;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2014-09-24 14:32:58 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void openInventory(EntityPlayer player)
|
|
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
if (this.worldObj == null)
|
2015-05-27 23:17:59 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
this.numPlayersUsing++;
|
|
|
|
|
|
|
|
|
|
this.worldObj.addBlockEvent(this.pos, IronChest.ironChestBlock, 1, this.numPlayersUsing);
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void closeInventory(EntityPlayer player)
|
|
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
if (this.worldObj == null)
|
2015-05-27 23:17:59 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
this.numPlayersUsing--;
|
|
|
|
|
|
|
|
|
|
this.worldObj.addBlockEvent(this.pos, IronChest.ironChestBlock, 1, this.numPlayersUsing);
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
2016-06-08 21:59:44 +02:00
|
|
|
public void setFacing(EnumFacing facing)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.facing = facing;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ItemStack[] getTopItemStacks()
|
|
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
return this.topStacks;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-05-20 00:17:38 +02:00
|
|
|
public SPacketUpdateTileEntity getUpdatePacket()
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
|
|
|
|
compound.setByte("facing", (byte) this.facing.ordinal());
|
2016-03-21 17:44:27 +01:00
|
|
|
ItemStack[] stacks = this.buildItemStackDataList();
|
2015-05-27 23:17:59 +02:00
|
|
|
if (stacks != null)
|
|
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
NBTTagList tagList = new NBTTagList();
|
|
|
|
|
for (int slot = 0; slot < stacks.length; slot++)
|
2015-05-27 23:17:59 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
if (stacks[slot] != null)
|
2015-05-27 23:17:59 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
|
|
|
tag.setByte("Slot", (byte) slot);
|
|
|
|
|
stacks[slot].writeToNBT(tag);
|
|
|
|
|
tagList.appendTag(tag);
|
2015-05-27 23:17:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
compound.setTag("stacks", tagList);
|
2015-05-27 23:17:59 +02:00
|
|
|
}
|
|
|
|
|
|
2016-06-08 21:59:44 +02:00
|
|
|
return new SPacketUpdateTileEntity(this.pos, 0, compound);
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-27 23:17:59 +02:00
|
|
|
@Override
|
2016-03-21 17:44:27 +01:00
|
|
|
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2015-05-27 23:17:59 +02:00
|
|
|
if (pkt.getTileEntityType() == 0)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
NBTTagCompound compound = pkt.getNbtCompound();
|
|
|
|
|
|
|
|
|
|
this.facing = EnumFacing.VALUES[compound.getByte("facing")];
|
|
|
|
|
|
|
|
|
|
NBTTagList tagList = compound.getTagList("stacks", Constants.NBT.TAG_COMPOUND);
|
2015-05-27 23:17:59 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
ItemStack[] stacks = new ItemStack[this.topStacks.length];
|
2015-05-27 23:17:59 +02:00
|
|
|
|
2016-06-08 21:59:44 +02:00
|
|
|
for (int item = 0; item < stacks.length; item++)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
NBTTagCompound itemStack = tagList.getCompoundTagAt(item);
|
|
|
|
|
|
|
|
|
|
int slot = itemStack.getByte("Slot") & 0xff;
|
|
|
|
|
|
|
|
|
|
if (slot >= 0 && slot < stacks.length)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
stacks[slot] = ItemStack.loadItemStackFromNBT(itemStack);
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2015-05-27 23:17:59 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-19 01:47:25 +02:00
|
|
|
if (this.getType().isTransparent() && stacks != null)
|
2015-05-27 23:17:59 +02:00
|
|
|
{
|
|
|
|
|
int pos = 0;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
for (int i = 0; i < this.topStacks.length; i++)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2015-05-27 23:17:59 +02:00
|
|
|
if (stacks[pos] != null)
|
|
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.topStacks[i] = stacks[pos];
|
|
|
|
|
}
|
|
|
|
|
else
|
2015-05-27 23:17:59 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
this.topStacks[i] = null;
|
2015-05-27 23:17:59 +02:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2015-05-27 23:17:59 +02:00
|
|
|
pos++;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ItemStack[] buildItemStackDataList()
|
|
|
|
|
{
|
2016-05-19 01:47:25 +02:00
|
|
|
if (this.getType().isTransparent())
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-03-21 17:44:27 +01:00
|
|
|
ItemStack[] sortList = new ItemStack[this.topStacks.length];
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2014-09-24 14:32:58 +02:00
|
|
|
int pos = 0;
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2016-03-21 17:44:27 +01:00
|
|
|
for (ItemStack is : this.topStacks)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
if (is != null)
|
|
|
|
|
{
|
|
|
|
|
sortList[pos++] = is;
|
2016-03-21 17:44:27 +01:00
|
|
|
}
|
|
|
|
|
else
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
sortList[pos++] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2014-09-24 14:32:58 +02:00
|
|
|
return sortList;
|
|
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
2014-09-24 14:32:58 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-06-08 21:59:44 +02:00
|
|
|
public ItemStack removeStackFromSlot(int index)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
if (this.chestContents[index] != null)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
ItemStack stack = this.chestContents[index];
|
|
|
|
|
|
|
|
|
|
this.chestContents[index] = null;
|
|
|
|
|
|
|
|
|
|
return stack;
|
2016-03-21 17:44:27 +01:00
|
|
|
}
|
|
|
|
|
else
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-06-08 21:59:44 +02:00
|
|
|
public boolean isItemValidForSlot(int index, ItemStack stack)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
return this.getType().acceptsStack(stack);
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-27 23:17:59 +02:00
|
|
|
public void rotateAround()
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-05-19 01:47:25 +02:00
|
|
|
this.setFacing(this.facing.rotateY());
|
|
|
|
|
this.worldObj.addBlockEvent(this.pos, IronChest.ironChestBlock, 2, this.facing.ordinal());
|
2014-12-02 21:25:03 +01:00
|
|
|
}
|
2014-09-24 14:32:58 +02:00
|
|
|
|
2016-06-08 21:59:44 +02:00
|
|
|
public void wasPlaced(EntityLivingBase entityliving, ItemStack stack)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-27 23:17:59 +02:00
|
|
|
public void removeAdornments()
|
|
|
|
|
{
|
|
|
|
|
}
|
2014-09-24 14:32:58 +02:00
|
|
|
|
|
|
|
|
@Override
|
2014-09-25 12:08:22 +02:00
|
|
|
public int getField(int id)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2014-09-25 12:08:22 +02:00
|
|
|
return 0;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-05-27 23:17:59 +02:00
|
|
|
public void setField(int id, int value)
|
|
|
|
|
{
|
|
|
|
|
}
|
2014-09-25 12:08:22 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getFieldCount()
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-12-02 20:34:29 +01:00
|
|
|
public void clear()
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
for (int slot = 0; slot < this.chestContents.length; ++slot)
|
2014-09-25 12:08:22 +02:00
|
|
|
{
|
2016-06-08 21:59:44 +02:00
|
|
|
this.chestContents[slot] = null;
|
2014-09-25 12:08:22 +02:00
|
|
|
}
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-06-08 21:59:44 +02:00
|
|
|
public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2014-09-25 12:08:22 +02:00
|
|
|
return null;
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-09-25 12:08:22 +02:00
|
|
|
public String getGuiID()
|
2014-09-24 14:32:58 +02:00
|
|
|
{
|
2016-05-19 01:47:25 +02:00
|
|
|
return "IronChest:" + this.getType().name();
|
2014-09-24 14:32:58 +02:00
|
|
|
}
|
2015-05-27 23:17:59 +02:00
|
|
|
|
2014-12-02 21:25:03 +01:00
|
|
|
@Override
|
|
|
|
|
public boolean canRenderBreaking()
|
|
|
|
|
{
|
2015-05-27 23:17:59 +02:00
|
|
|
return true;
|
2014-12-02 21:25:03 +01:00
|
|
|
}
|
2016-06-08 21:59:44 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public NBTTagCompound getUpdateTag()
|
|
|
|
|
{
|
|
|
|
|
return writeToNBT(new NBTTagCompound());
|
|
|
|
|
}
|
2015-01-29 00:24:09 +01:00
|
|
|
}
|