Files
TinkersConstruct/src/main/java/tconstruct/tools/logic/PartBuilderLogic.java
T

172 lines
5.5 KiB
Java
Raw Normal View History

2014-06-04 21:44:48 -08:00
package tconstruct.tools.logic;
2013-12-01 12:19:43 +00:00
import mantle.blocks.abstracts.InventoryLogic;
import net.minecraft.entity.player.InventoryPlayer;
2014-09-09 18:30:15 +10:00
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
2014-02-16 14:23:50 -05:00
import tconstruct.library.crafting.PatternBuilder;
import tconstruct.library.util.IPattern;
2014-09-09 18:30:15 +10:00
import tconstruct.tools.inventory.*;
public class PartBuilderLogic extends InventoryLogic implements ISidedInventory {
boolean craftedTop;
boolean craftedBottom;
public PartBuilderLogic() {
super(10);
craftedTop = false;
craftedBottom = false;
}
@Override
public String getDefaultName() {
return "toolstation.parts";
}
@Override
public Container getGuiContainer(InventoryPlayer inventoryplayer, World world, int x, int y, int z) {
for (int xPos = x - 1; xPos <= x + 1; xPos++) {
for (int zPos = z - 1; zPos <= z + 1; zPos++) {
for (int yPos = y - 1; yPos <= y + 1; yPos++) {
2014-02-15 00:29:24 -05:00
TileEntity tile = world.getTileEntity(xPos, yPos, zPos);
2014-02-17 01:27:15 -05:00
if (tile != null && tile instanceof PatternChestLogic && (x == xPos || z == zPos))
2014-02-15 00:29:24 -05:00
return new PartCrafterChestContainer(inventoryplayer, this, (PatternChestLogic) tile);
}
}
}
return new PartCrafterContainer(inventoryplayer, this);
}
2014-08-31 17:28:24 +02:00
@Override
public boolean canDropInventorySlot(int slot) {
2014-09-09 18:30:15 +10:00
return slot <= 3; // 0-3 = only drop input slots
2014-08-31 17:28:24 +02:00
}
// Called when emptying a slot, not when another item is placed in it
2014-02-17 00:31:43 -05:00
@Override
public ItemStack decrStackSize(int slotID, int quantity) {
ItemStack returnStack = super.decrStackSize(slotID, quantity);
tryBuildPart(slotID);
return returnStack;
}
public void tryBuildPart(int slotID) {
if (slotID == 4 || slotID == 5) {
if (!craftedTop && inventory[0] != null) {
int value = PatternBuilder.instance.getPartValue(inventory[2]);
IPattern item = (IPattern) inventory[0].getItem();
int cost = item != null ? item.getPatternCost(inventory[0]) : 0;
if (value > 0 && cost > 0) {
int decrease = cost / value;
if (cost % value != 0) decrease++;
super.decrStackSize(2, decrease); // Call super to avoid crafting again
}
}
if (inventory[4] != null || inventory[5] != null) craftedTop = true;
else craftedTop = false;
}
if (!craftedTop) buildTopPart();
if (slotID == 6 || slotID == 7) {
if (!craftedBottom && inventory[1] != null) {
int value = PatternBuilder.instance.getPartValue(inventory[3]);
IPattern item = (IPattern) inventory[1].getItem();
int cost = item != null ? item.getPatternCost(inventory[1]) : 0;
if (value > 0 && cost > 0) {
int decrease = cost / value;
if (cost % value != 0) decrease++;
super.decrStackSize(3, decrease); // Call super to avoid crafting again
}
}
if (inventory[6] != null || inventory[7] != null) craftedBottom = true;
else craftedBottom = false;
}
if (!craftedBottom) buildBottomPart();
}
// Called when a slot has something placed into it.
2014-02-17 00:31:43 -05:00
@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) {
super.setInventorySlotContents(slot, itemstack);
if ((slot == 0 || slot == 2) && !craftedTop) buildTopPart();
if ((slot == 1 || slot == 3) && !craftedBottom) buildBottomPart();
}
void buildTopPart() {
ItemStack[] parts = PatternBuilder.instance.getToolPart(inventory[2], inventory[0], inventory[1]);
if (parts != null) {
inventory[4] = parts[0];
inventory[5] = parts[1];
} else {
inventory[4] = inventory[5] = null;
}
}
void buildBottomPart() {
ItemStack[] parts = PatternBuilder.instance.getToolPart(inventory[3], inventory[1], inventory[0]);
if (parts != null) {
inventory[6] = parts[0];
inventory[7] = parts[1];
} else {
inventory[6] = inventory[7] = null;
}
}
/* NBT */
2014-02-17 00:31:43 -05:00
@Override
public void readFromNBT(NBTTagCompound tags) {
2014-02-05 08:28:53 -05:00
super.readFromNBT(tags);
craftedTop = tags.getBoolean("CraftedTop");
craftedBottom = tags.getBoolean("CraftedBottom");
}
2014-02-17 00:31:43 -05:00
@Override
public void writeToNBT(NBTTagCompound tags) {
2014-02-05 08:28:53 -05:00
super.writeToNBT(tags);
tags.setBoolean("CraftedTop", craftedTop);
tags.setBoolean("CraftedBottom", craftedBottom);
}
@Override
public int[] getAccessibleSlotsFromSide(int side) {
return new int[0];
}
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j) {
return false;
}
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j) {
return false;
}
2014-01-18 02:52:49 -05:00
@Override
public String getInventoryName() {
2014-01-18 02:52:49 -05:00
return getDefaultName();
}
@Override
public boolean hasCustomInventoryName() {
2014-01-18 02:52:49 -05:00
return false;
}
2014-02-05 10:22:35 -05:00
2014-02-05 10:28:54 -05:00
@Override
public void openInventory() {}
2014-02-05 10:28:54 -05:00
@Override
public void closeInventory() {}
2014-02-11 21:35:05 -05:00
@Override
public boolean canUpdate() {
return false;
}
}