Fix auto chisel functionality. Massively clean up and comment TE code.
No files matched your search
@@ -1,9 +1,10 @@
|
||||
package com.cricketcraft.chisel.block.tileentity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
@@ -12,6 +13,7 @@ import net.minecraft.network.Packet;
|
||||
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
import com.cricketcraft.chisel.carving.Carving;
|
||||
import com.cricketcraft.chisel.init.ModItems;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
@@ -19,8 +21,11 @@ import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
public class TileEntityAutoChisel extends TileEntity implements ISidedInventory {
|
||||
|
||||
private final int BASE = 0, TARGET = 1, OUTPUT = 2, AUTOMATION = 3, STACK = 4, SPEED = 5;
|
||||
private boolean isAutomated, isStackMode, isFaster;
|
||||
public enum Upgrade {
|
||||
SPEED, AUTOMATION, STACK
|
||||
}
|
||||
|
||||
private final int BASE = 0, TARGET = 1, OUTPUT = 2, MIN_UPGRADE = 3;
|
||||
private static EntityItem ghostItem;
|
||||
boolean equal = false;
|
||||
private ItemStack[] inventory = new ItemStack[6];
|
||||
@@ -72,81 +77,79 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory
|
||||
@Override
|
||||
public void updateEntity() {
|
||||
|
||||
if (inventory[SPEED] == new ItemStack(ModItems.upgrade, 1, 0)) {
|
||||
isFaster = true;
|
||||
} else {
|
||||
isFaster = false;
|
||||
}
|
||||
int checkPeriod = hasUpgrade(Upgrade.SPEED) ? 10 : 40;
|
||||
|
||||
if (inventory[AUTOMATION] == new ItemStack(ModItems.upgrade, 1, 1)) {
|
||||
isAutomated = true;
|
||||
} else {
|
||||
isAutomated = false;
|
||||
}
|
||||
ItemStack base = inventory[BASE], target = inventory[TARGET], output = inventory[OUTPUT];
|
||||
|
||||
if (inventory[STACK] == new ItemStack(ModItems.upgrade, 1, 2)) {
|
||||
isStackMode = true;
|
||||
} else {
|
||||
isStackMode = false;
|
||||
}
|
||||
if (!worldObj.isRemote && worldObj.getWorldTime() % checkPeriod == 0) {
|
||||
if (base != null && target != null) {
|
||||
if (canBeMadeFrom(base, target)) {
|
||||
// the max possible for this craft
|
||||
int canBeMade = target.getMaxStackSize();
|
||||
|
||||
if (isFaster) {
|
||||
if (!worldObj.isRemote && worldObj.getWorldTime() % 10 == 0) {
|
||||
if (inventory[BASE] != null && inventory[TARGET] != null) {
|
||||
if (inventory[BASE].getItem() instanceof ItemBlock && inventory[TARGET].getItem() instanceof ItemBlock) {
|
||||
if (inventory[BASE].getUnlocalizedName().equalsIgnoreCase(inventory[TARGET].getUnlocalizedName())) {
|
||||
if (isStackMode) {
|
||||
if (inventory[BASE].stackSize == inventory[BASE].getMaxStackSize()) {
|
||||
if (inventory[OUTPUT] == null) {
|
||||
inventory[OUTPUT] = new ItemStack(inventory[TARGET].getItem(), 64, inventory[TARGET].getItemDamage());
|
||||
inventory[BASE] = null;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (inventory[OUTPUT] == null) {
|
||||
inventory[OUTPUT] = new ItemStack(inventory[TARGET].getItem(), 1, inventory[TARGET].getItemDamage());
|
||||
inventory[BASE].stackSize--;
|
||||
} else if (inventory[BASE].stackSize != 0) {
|
||||
inventory[OUTPUT].stackSize++;
|
||||
inventory[BASE].stackSize--;
|
||||
} else {
|
||||
inventory[BASE] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if there are items in the output, they count towards the max we can make
|
||||
if (output != null) {
|
||||
canBeMade -= output.stackSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!worldObj.isRemote && worldObj.getWorldTime() % 40 == 0) {
|
||||
if (inventory[BASE] != null && inventory[TARGET] != null) {
|
||||
if (inventory[BASE].getItem() instanceof ItemBlock && inventory[TARGET].getItem() instanceof ItemBlock) {
|
||||
if (inventory[BASE].getUnlocalizedName().equalsIgnoreCase(inventory[TARGET].getUnlocalizedName())) {
|
||||
if (isStackMode) {
|
||||
if (inventory[BASE].stackSize == inventory[BASE].getMaxStackSize()) {
|
||||
if (inventory[OUTPUT] == null) {
|
||||
inventory[OUTPUT] = new ItemStack(inventory[TARGET].getItem(), 64, inventory[TARGET].getItemDamage());
|
||||
inventory[BASE] = null;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (inventory[OUTPUT] == null) {
|
||||
inventory[OUTPUT] = new ItemStack(inventory[TARGET].getItem(), 1, inventory[TARGET].getItemDamage());
|
||||
inventory[BASE].stackSize--;
|
||||
} else if (inventory[BASE].stackSize != 0) {
|
||||
inventory[OUTPUT].stackSize++;
|
||||
inventory[BASE].stackSize--;
|
||||
} else {
|
||||
inventory[BASE] = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
inventory[OUTPUT] = chiseled;
|
||||
} else {
|
||||
// otherwise just add our result to the existing stack
|
||||
inventory[OUTPUT].stackSize += chiseled.stackSize;
|
||||
}
|
||||
// remove what we made from the stack
|
||||
base.stackSize -= chiseled.stackSize;
|
||||
if (base.stackSize <= 0) {
|
||||
inventory[BASE] = null; // clear out 0 size itemstacks
|
||||
}
|
||||
// we changed something, so we need to tell the chunk to save
|
||||
markDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
markDirty();
|
||||
// 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -232,7 +235,7 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory
|
||||
|
||||
@Override
|
||||
public boolean canInsertItem(int slot, ItemStack itemStack, int side) {
|
||||
if (isAutomated) {
|
||||
if (hasUpgrade(Upgrade.AUTOMATION)) {
|
||||
if (side != 0 || side != 1 && slot == BASE) {
|
||||
return true;
|
||||
}
|
||||
@@ -242,7 +245,7 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory
|
||||
|
||||
@Override
|
||||
public boolean canExtractItem(int slot, ItemStack itemStack, int side) {
|
||||
if (isAutomated) {
|
||||
if (hasUpgrade(Upgrade.AUTOMATION)) {
|
||||
if (side == 0 || side == 1 && slot == OUTPUT) {
|
||||
return true;
|
||||
}
|
||||
@@ -286,4 +289,12 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory
|
||||
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() == ModItems.upgrade && stack.getItemDamage() == upgrade.ordinal();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,9 @@ public class ContainerAutoChisel extends Container {
|
||||
addSlotToContainer(new Slot(tileEntityAutoChisel, 0, 53, 15));
|
||||
addSlotToContainer(new Slot(tileEntityAutoChisel, 1, 78, 51));
|
||||
addSlotToContainer(new Slot(tileEntityAutoChisel, 2, 103, 15));
|
||||
addSlotToContainer(new Slot(tileEntityAutoChisel, 3, 151, 51));
|
||||
addSlotToContainer(new Slot(tileEntityAutoChisel, 3, 151, 11));
|
||||
addSlotToContainer(new Slot(tileEntityAutoChisel, 4, 151, 31));
|
||||
addSlotToContainer(new Slot(tileEntityAutoChisel, 5, 151, 11));
|
||||
addSlotToContainer(new Slot(tileEntityAutoChisel, 5, 151, 51));
|
||||
|
||||
bindPlayerInventory(player);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import net.minecraft.world.World;
|
||||
|
||||
import com.cricketcraft.chisel.Chisel;
|
||||
import com.cricketcraft.chisel.block.tileentity.TileEntityAutoChisel;
|
||||
import com.cricketcraft.chisel.block.tileentity.TileEntityAutoChisel.Upgrade;
|
||||
|
||||
public class ItemUpgrade extends BaseItem {
|
||||
|
||||
@@ -22,6 +23,7 @@ public class ItemUpgrade extends BaseItem {
|
||||
super();
|
||||
this.setUnlocalizedName(unlocalizedName);
|
||||
this.setCreativeTab(CreativeTabs.tabMaterials);
|
||||
setHasSubtypes(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -36,16 +38,15 @@ public class ItemUpgrade extends BaseItem {
|
||||
|
||||
@Override
|
||||
public IIcon getIconFromDamage(int meta) {
|
||||
if (meta > 3)
|
||||
meta = 0;
|
||||
|
||||
return this.icons[meta];
|
||||
// using modulo throughout to prevent AIOB
|
||||
return this.icons[meta % Upgrade.values().length];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerIcons(IIconRegister reg) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
this.icons[i] = reg.registerIcon(Chisel.MOD_ID + ":upgradeItem_" + i);
|
||||
Upgrade[] upgrades = Upgrade.values();
|
||||
for (int i = 0; i < upgrades.length; i++) {
|
||||
this.icons[i] = reg.registerIcon(Chisel.MOD_ID + ":upgrade_" + upgrades[i].name().toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,15 +60,7 @@ public class ItemUpgrade extends BaseItem {
|
||||
|
||||
@Override
|
||||
public String getUnlocalizedName(ItemStack stack) {
|
||||
switch (stack.getItemDamage()) {
|
||||
case 0:
|
||||
return this.getUnlocalizedName() + "_speed";
|
||||
case 1:
|
||||
return this.getUnlocalizedName() + "_automation";
|
||||
case 2:
|
||||
return this.getUnlocalizedName() + "_stack";
|
||||
default:
|
||||
return this.getUnlocalizedName();
|
||||
}
|
||||
Upgrade[] upgrades = Upgrade.values();
|
||||
return getUnlocalizedName() + "_" + upgrades[stack.getItemDamage() % upgrades.length].name().toLowerCase();
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 9.8 KiB After Width: | Height: | Size: 1.0 MiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |