Files
Chisel/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java
T

417 lines
11 KiB
Java
Raw Normal View History

2014-12-15 00:11:48 +00:00
package com.cricketcraft.chisel.block.tileentity;
2014-10-27 16:26:14 -04:00
import java.util.ArrayList;
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;
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;
import net.minecraft.util.StatCollector;
2014-10-27 16:26:14 -04:00
2015-01-09 01:50:32 -05:00
import com.cricketcraft.chisel.api.IChiselItem;
2015-01-18 14:40:39 -05:00
import com.cricketcraft.chisel.api.carving.CarvingUtils;
import com.cricketcraft.chisel.carving.Carving;
2015-01-12 00:19:05 -05:00
import com.cricketcraft.chisel.client.GeneralChiselClient;
import com.cricketcraft.chisel.init.ChiselItems;
import com.cricketcraft.chisel.network.PacketHandler;
2015-01-11 18:23:07 -05:00
import com.cricketcraft.chisel.network.message.MessageAutoChisel;
import com.cricketcraft.chisel.network.message.MessageSlotUpdate;
2015-01-18 14:40:39 -05:00
import com.cricketcraft.chisel.utils.General;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2014-11-26 20:03:54 -05:00
public class TileEntityAutoChisel extends TileEntity implements ISidedInventory {
2014-10-27 16:26:14 -04:00
public enum Upgrade {
SPEED, AUTOMATION, STACK;
public String getUnlocalizedName() {
return ChiselItems.upgrade.getUnlocalizedName() + "_" + this.name().toLowerCase();
}
2015-01-09 01:50:32 -05:00
public String getLocalizedName() {
return StatCollector.translateToLocal(getUnlocalizedName() + ".name");
}
}
2015-01-09 01:50:32 -05:00
public static final int BASE = 0, TARGET = 1, OUTPUT = 2, CHISEL = 3, MIN_UPGRADE = 4;
2015-01-12 00:19:05 -05:00
private static final int FAST_SPEED = 1, SLOW_SPEED = 4;
private int progress = 0;
2015-01-09 02:09:22 -05:00
private static EntityItem ghostItem;
boolean equal = false;
2015-01-09 01:50:32 -05:00
private ItemStack[] inventory = new ItemStack[7];
private String name = "autoChisel";
2015-01-09 01:50:32 -05:00
2015-01-12 00:19:05 -05:00
// client animation fields
// used for floating target
2015-01-11 18:23:07 -05:00
public float xRot, yRot, zRot;
2015-01-12 00:19:05 -05:00
// used for chisel item rotation
public static final int maxRot = 60;
public static final int rotAmnt = 15;
2015-01-11 18:23:07 -05:00
public float chiselRot;
public boolean chiseling = false;
public int toChisel = 1;
2015-01-12 00:19:05 -05:00
private ItemStack lastBase;
@Override
public boolean canUpdate() {
return true;
}
@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)) {
this.name = nbt.getString("CustomName");
}
}
@Override
public void updateEntity() {
ItemStack base = inventory[BASE], target = inventory[TARGET], output = inventory[OUTPUT];
2015-01-12 00:19:05 -05:00
if (!worldObj.isRemote && hasChisel() && base != null && target != null) {
if (canBeMadeFrom(base, target)) {
if (progress >= (hasUpgrade(Upgrade.SPEED) ? FAST_SPEED : SLOW_SPEED)) {
// reset progress
progress = 0;
// the max possible for this craft
int canBeMade = target.getMaxStackSize();
// if there are items in the output, they count towards the max we can make
if (output != null) {
canBeMade -= output.stackSize;
}
// can't make more than we have
canBeMade = Math.min(base.stackSize, canBeMade);
// if we can't make any, forget it
if (canBeMade <= 0) {
return;
}
// result will always be a copy of the target, of course
ItemStack chiseled = target.copy();
// if we have the stack upgrade, boost the stack size to the max possible, otherwise just one
chiseled.stackSize = hasUpgrade(Upgrade.STACK) ? canBeMade : 1;
// if we can place the result in the output
if (canMerge(chiseled)) {
// if our output is empty, just use the current result
if (output == null) {
setInventorySlotContents(OUTPUT, chiseled);
} else {
// otherwise just add our result to the existing stack
inventory[OUTPUT].stackSize += chiseled.stackSize;
slotChanged(OUTPUT);
}
2015-01-12 00:19:05 -05:00
chiselItem(chiseled.stackSize);
2015-01-12 00:19:05 -05:00
// remove what we made from the stack
base.stackSize -= chiseled.stackSize;
if (base.stackSize <= 0) {
setInventorySlotContents(BASE, null); // clear out 0 size itemstacks
}
}
2015-01-12 00:19:05 -05:00
} else if (worldObj.getTotalWorldTime() % 10 == 0) {
progress++;
}
}
2015-01-12 00:19:05 -05:00
} else if (worldObj.isRemote) {
if (chiseling) {
chiselRot += rotAmnt;
if (chiselRot >= maxRot) {
chiselItem(0);
2015-01-12 00:19:05 -05:00
}
} else {
chiselRot = Math.max(chiselRot - rotAmnt, 0);
}
}
}
// lets make sure the user isn't trying to make something from a block that doesn't have this as a valid target
private boolean canBeMadeFrom(ItemStack from, ItemStack to) {
ArrayList<ItemStack> results = Carving.chisel.getItems(from);
for (ItemStack s : results) {
if (s.getItem() == to.getItem() && s.getItemDamage() == to.getItemDamage()) {
return true;
}
}
return false;
}
private boolean canMerge(ItemStack toMerge) {
// if the output slot is empty we can merge without checking
if (inventory[OUTPUT] == null) {
return true;
}
// need to check NBT as well as item
if (toMerge.isItemEqual(inventory[OUTPUT]) && ItemStack.areItemStackTagsEqual(toMerge, inventory[OUTPUT])) {
// we only care about metadata if the item has subtypes
return !toMerge.getHasSubtypes() || toMerge.getItemDamage() == inventory[OUTPUT].getItemDamage();
}
return false;
}
2015-01-09 01:50:32 -05:00
private boolean hasChisel() {
return inventory[CHISEL] != null && inventory[CHISEL].getItem() instanceof IChiselItem && inventory[CHISEL].stackSize >= 1;
2015-01-09 01:50:32 -05:00
}
2015-01-11 18:23:07 -05:00
/** Calls IChiselItem#onChisel() and sends the chisel packet for sound/animation */
private void chiselItem(int chiseled) {
2015-01-12 00:19:05 -05:00
if (!worldObj.isRemote) {
2015-01-18 14:40:39 -05:00
((IChiselItem) inventory[CHISEL].getItem()).onChisel(worldObj, this, CHISEL, inventory[CHISEL], General.getVariation(inventory[TARGET]));
PacketHandler.INSTANCE.sendToDimension(new MessageAutoChisel(this, chiseled, true), worldObj.provider.dimensionId);
2015-01-12 00:19:05 -05:00
} else {
GeneralChiselClient.spawnAutoChiselFX(this, lastBase != null ? lastBase : inventory[BASE]);
chiseling = false;
if (lastBase != null) {
lastBase.stackSize -= toChisel;
2015-01-12 00:19:05 -05:00
if (lastBase.stackSize <= 0) {
lastBase = null;
}
}
}
2015-01-11 18:23:07 -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()) {
nbt.setString("CustomName", this.name);
}
}
@Override
public int getSizeInventory() {
return inventory.length;
}
@Override
public ItemStack getStackInSlot(int slot) {
return inventory[slot];
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
if (inventory[slot] != null) {
ItemStack is = inventory[slot];
inventory[slot] = null;
return is;
} else
return null;
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
inventory[slot] = stack;
if (stack != null && stack.stackSize > getInventoryStackLimit()) {
stack.stackSize = getInventoryStackLimit();
}
2015-01-12 00:19:05 -05:00
if (worldObj.isRemote && slot == BASE && stack != null) {
lastBase = stack.copy();
}
if (!worldObj.isRemote) {
slotChanged(slot);
}
}
private void slotChanged(int slot) {
PacketHandler.INSTANCE.sendToDimension(new MessageSlotUpdate(this, slot, inventory[slot]), worldObj.provider.dimensionId);
markDirty();
}
@Override
public String getInventoryName() {
return name;
}
@Override
public final boolean isUseableByPlayer(EntityPlayer player) {
return true;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public int[] getAccessibleSlotsFromSide(int side) {
return new int[] { BASE, TARGET, OUTPUT, CHISEL };
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack itemStack) {
2015-01-09 01:50:32 -05:00
if (itemStack == null) {
return false;
}
2015-01-09 02:09:22 -05:00
switch (slot) {
case BASE:
2015-01-09 01:50:32 -05:00
case TARGET:
return !Carving.chisel.getItems(itemStack).isEmpty();
case OUTPUT:
return false;
2015-01-09 02:09:22 -05:00
case CHISEL:
2015-01-09 01:50:32 -05:00
return itemStack.getItem() instanceof IChiselItem;
default:
return itemStack.getItem() == ChiselItems.upgrade && Upgrade.values()[slot - MIN_UPGRADE].ordinal() == itemStack.getItemDamage();
}
}
@Override
public boolean canInsertItem(int slot, ItemStack itemStack, int side) {
return hasUpgrade(Upgrade.AUTOMATION) && (slot == BASE || slot == TARGET || slot == CHISEL);
}
@Override
public boolean canExtractItem(int slot, ItemStack itemStack, int side) {
2015-01-09 02:09:22 -05:00
return hasUpgrade(Upgrade.AUTOMATION) && slot == OUTPUT;
}
@Override
public void openInventory() {
;
}
@Override
public void closeInventory() {
;
}
@SideOnly(Side.CLIENT)
public EntityItem getItemForRendering(int slot) {
if (ghostItem == null) {
ghostItem = new EntityItem(worldObj);
ghostItem.hoverStart = 0.0F;
}
2015-01-12 00:19:05 -05:00
if (slot == BASE && lastBase != null) {
ghostItem.setEntityItemStack(lastBase.copy());
return ghostItem;
} else if (inventory[slot] == null) {
return null;
} else {
ghostItem.setEntityItemStack(new ItemStack(inventory[slot].getItem(), 1, inventory[slot].getItemDamage()));
return ghostItem;
}
}
@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());
}
public boolean hasUpgrade(Upgrade upgrade) {
ItemStack stack = inventory[MIN_UPGRADE + upgrade.ordinal()];
if (stack != null) {
return stack.getItem() == ChiselItems.upgrade && stack.getItemDamage() == upgrade.ordinal();
}
return false;
}
2015-01-09 01:50:32 -05:00
public String getSlotTooltipUnloc(int slotNumber) {
String base = "autochisel.slot.%s.tooltip";
String name = null;
if (slotNumber < MIN_UPGRADE) {
if (slotNumber == TARGET) {
name = "target";
} else if (slotNumber == CHISEL) {
name = "chisel";
}
return name == null ? null : String.format(base, name);
} else {
return Upgrade.values()[slotNumber - MIN_UPGRADE].getUnlocalizedName() + ".name";
}
}
2015-01-11 18:23:07 -05:00
public void doChiselAnim(ItemStack lastChiseled, int chiseled, boolean playSound) {
2015-01-12 00:19:05 -05:00
this.lastBase = lastChiseled == null ? null : lastChiseled.copy();
this.toChisel = chiseled;
2015-01-12 00:19:05 -05:00
if (playSound) {
this.chiseling = true;
}
}
public ItemStack getLastBase() {
return lastBase == null ? null : lastBase.copy();
2015-01-11 18:23:07 -05:00
}
2014-10-27 16:26:14 -04:00
}