2014-12-15 00:11:48 +00:00
|
|
|
package com.cricketcraft.chisel.block.tileentity;
|
2014-10-27 16:26:14 -04:00
|
|
|
|
2014-11-24 18:48:10 -05:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
2014-10-27 16:26:14 -04:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.inventory.ISidedInventory;
|
2014-11-02 16:44:01 -05:00
|
|
|
import net.minecraft.item.ItemBlock;
|
2014-10-27 16:26:14 -04:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
import net.minecraft.network.NetworkManager;
|
|
|
|
|
import net.minecraft.network.Packet;
|
|
|
|
|
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
|
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-12-14 20:05:39 +00:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2014-10-27 16:26:14 -04:00
|
|
|
|
2014-11-26 20:03:54 -05:00
|
|
|
public class TileEntityAutoChisel extends TileEntity implements ISidedInventory {
|
2014-10-27 16:26:14 -04:00
|
|
|
|
2014-11-24 18:48:10 -05:00
|
|
|
private static EntityItem ghostItem;
|
2014-10-29 20:12:32 -04:00
|
|
|
boolean equal = false;
|
2014-11-26 20:03:54 -05:00
|
|
|
private ItemStack[] inventory = new ItemStack[3];
|
|
|
|
|
private String name = "autoChisel";
|
2014-10-29 20:12:32 -04:00
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public boolean canUpdate() {
|
2014-10-29 20:12:32 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-10-27 16:26:14 -04:00
|
|
|
|
2014-11-26 20:03:54 -05:00
|
|
|
@Override
|
|
|
|
|
public ItemStack decrStackSize(int slot, int size) {
|
|
|
|
|
if (inventory[slot] != null) {
|
|
|
|
|
ItemStack is;
|
|
|
|
|
if (inventory[slot].stackSize <= size) {
|
|
|
|
|
is = inventory[slot];
|
|
|
|
|
inventory[slot] = null;
|
|
|
|
|
return is;
|
|
|
|
|
} else {
|
|
|
|
|
is = inventory[slot].splitStack(size);
|
|
|
|
|
if (inventory[slot].stackSize == 0)
|
|
|
|
|
inventory[slot] = null;
|
|
|
|
|
return is;
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void readFromNBT(NBTTagCompound nbt) {
|
|
|
|
|
super.readFromNBT(nbt);
|
|
|
|
|
NBTTagList tags = nbt.getTagList("Items", 10);
|
|
|
|
|
inventory = new ItemStack[getSizeInventory()];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < tags.tagCount(); i++) {
|
|
|
|
|
NBTTagCompound data = tags.getCompoundTagAt(i);
|
|
|
|
|
int j = data.getByte("Slot") & 255;
|
|
|
|
|
|
|
|
|
|
if (j >= 0 && j < inventory.length) {
|
|
|
|
|
inventory[j] = ItemStack.loadItemStackFromNBT(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nbt.hasKey("CustomName", 8)) {
|
2014-10-27 16:26:14 -04:00
|
|
|
this.name = nbt.getString("CustomName");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-29 18:10:28 -04:00
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public void updateEntity() {
|
|
|
|
|
if (!worldObj.isRemote && worldObj.getWorldTime() % 20 == 0) {
|
2014-11-09 10:33:31 -05:00
|
|
|
if (inventory[0] != null && inventory[1] != null) {
|
2014-11-26 20:03:54 -05:00
|
|
|
if (inventory[0].getItem() instanceof ItemBlock && inventory[1].getItem() instanceof ItemBlock) {
|
|
|
|
|
if (inventory[0].getItem() == inventory[1].getItem()) {
|
2014-11-02 16:44:01 -05:00
|
|
|
equal = true;
|
2014-11-26 20:03:54 -05:00
|
|
|
} else {
|
|
|
|
|
equal = false;
|
|
|
|
|
}
|
2014-11-02 16:44:01 -05:00
|
|
|
if (equal) {
|
2014-11-26 20:03:54 -05:00
|
|
|
if (inventory[0].stackSize < 0) {
|
|
|
|
|
inventory[0] = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inventory[1].stackSize < 0) {
|
|
|
|
|
inventory[1] = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inventory[1].stackSize < 0) {
|
|
|
|
|
inventory[1] = null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-09 10:33:31 -05:00
|
|
|
if (inventory[2] == null) {
|
|
|
|
|
setInventorySlotContents(2, new ItemStack(inventory[1].getItem(), 1, inventory[1].getItemDamage()));
|
|
|
|
|
decrStackSize(0, 1);
|
2014-10-31 19:47:22 -04:00
|
|
|
} else {
|
2014-11-26 20:03:54 -05:00
|
|
|
if (inventory[0].stackSize != 0 && inventory[2].stackSize <= getInventoryStackLimit()) {
|
2014-11-02 16:44:01 -05:00
|
|
|
decrStackSize(0, 1);
|
2014-11-09 10:33:31 -05:00
|
|
|
inventory[2].stackSize++;
|
2014-11-02 16:44:01 -05:00
|
|
|
} else {
|
|
|
|
|
inventory[0] = null;
|
|
|
|
|
}
|
2014-10-31 19:47:22 -04:00
|
|
|
}
|
2014-10-29 18:10:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
markDirty();
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-26 20:03:54 -05:00
|
|
|
@Override
|
|
|
|
|
public void writeToNBT(NBTTagCompound nbt) {
|
|
|
|
|
super.writeToNBT(nbt);
|
|
|
|
|
NBTTagList tags = new NBTTagList();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < inventory.length; i++) {
|
|
|
|
|
if (inventory[i] != null) {
|
|
|
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
|
|
|
data.setByte("Slot", (byte) i);
|
|
|
|
|
inventory[i].writeToNBT(data);
|
|
|
|
|
tags.appendTag(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nbt.setTag("Items", tags);
|
|
|
|
|
|
|
|
|
|
if (this.hasCustomInventoryName()) {
|
2014-10-27 16:26:14 -04:00
|
|
|
nbt.setString("CustomName", this.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public int getSizeInventory() {
|
2014-10-27 16:26:14 -04:00
|
|
|
return inventory.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public ItemStack getStackInSlot(int slot) {
|
2014-10-27 16:26:14 -04:00
|
|
|
return inventory[slot];
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-26 20:03:54 -05:00
|
|
|
@Override
|
|
|
|
|
public ItemStack getStackInSlotOnClosing(int slot) {
|
|
|
|
|
if (inventory[slot] != null) {
|
|
|
|
|
ItemStack is = inventory[slot];
|
|
|
|
|
inventory[slot] = null;
|
|
|
|
|
return is;
|
|
|
|
|
} else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-10-27 16:26:14 -04:00
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public void setInventorySlotContents(int slot, ItemStack stack) {
|
2014-10-27 16:26:14 -04:00
|
|
|
inventory[slot] = stack;
|
|
|
|
|
|
2014-11-26 20:03:54 -05:00
|
|
|
if (stack != null && stack.stackSize > getInventoryStackLimit()) {
|
2014-10-27 16:26:14 -04:00
|
|
|
stack.stackSize = getInventoryStackLimit();
|
|
|
|
|
}
|
|
|
|
|
markDirty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public String getInventoryName() {
|
2014-10-27 16:26:14 -04:00
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public final boolean isUseableByPlayer(EntityPlayer player) {
|
2014-10-27 16:26:14 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public int getInventoryStackLimit() {
|
2014-10-27 16:26:14 -04:00
|
|
|
return 64;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public boolean hasCustomInventoryName() {
|
2014-10-27 16:26:14 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public int[] getAccessibleSlotsFromSide(int side) {
|
|
|
|
|
return new int[3];
|
2014-10-27 16:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public boolean isItemValidForSlot(int slot, ItemStack itemStack) {
|
|
|
|
|
return true;
|
2014-10-27 16:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public boolean canInsertItem(int slot, ItemStack itemStack, int side) {
|
|
|
|
|
return false;
|
2014-10-27 16:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public boolean canExtractItem(int slot, ItemStack itemStack, int side) {
|
|
|
|
|
return true;
|
2014-10-27 16:26:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public void openInventory() {
|
|
|
|
|
}
|
2014-10-27 16:26:14 -04:00
|
|
|
|
|
|
|
|
@Override
|
2014-11-26 20:03:54 -05:00
|
|
|
public void closeInventory() {
|
2014-10-27 16:26:14 -04:00
|
|
|
NBTTagList tags = new NBTTagList();
|
|
|
|
|
System.out.print(tags);
|
|
|
|
|
}
|
2014-11-24 18:48:10 -05:00
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
2014-11-26 20:03:54 -05:00
|
|
|
public EntityItem getItemForRendering(int slot) {
|
|
|
|
|
if (ghostItem == null) {
|
2014-11-24 18:48:10 -05:00
|
|
|
ghostItem = new EntityItem(worldObj);
|
|
|
|
|
ghostItem.hoverStart = 0.0F;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-26 20:03:54 -05:00
|
|
|
if (inventory[slot] == null) {
|
2014-11-24 18:48:10 -05:00
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
ghostItem.setEntityItemStack(new ItemStack(inventory[slot].getItem(), 1, inventory[slot].getItemDamage()));
|
|
|
|
|
return ghostItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-26 20:03:54 -05:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Packet getDescriptionPacket() {
|
|
|
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
|
|
|
writeToNBT(tag);
|
|
|
|
|
return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
|
|
|
|
|
readFromNBT(packet.func_148857_g());
|
|
|
|
|
}
|
2014-10-27 16:26:14 -04:00
|
|
|
}
|