Added Crafter.

This commit is contained in:
CrazyPants committed 2014-05-29 17:17:54 +08:00
1 parent 1855feabec
commit 4df347298f
17 files changed
+594 -8

No files matched your search

@@ -143,6 +143,7 @@ public final class Config {
public static boolean useCombustionGenModel = false;
public static int crafterMjPerCraft = 250;
public static void load(FMLPreInitializationEvent event) {
configDirectory = new File(event.getModConfigurationDirectory(), "enderio");
@@ -424,6 +425,9 @@ public final class Config {
useCombustionGenModel = config.get("Settings", "magnetRange", useCombustionGenModel,
"Set to true to enabled WIP combustion generator model.").getBoolean(useCombustionGenModel);
crafterMjPerCraft= config.get("Settings", "crafterMjPerCraft", crafterMjPerCraft,
"MJ used be recipe crafted").getInt(crafterMjPerCraft);
}
private Config() {
@@ -48,6 +48,7 @@ import crazypants.enderio.machine.MachineRecipes;
import crazypants.enderio.machine.PacketRedstoneMode;
import crazypants.enderio.machine.alloy.AlloyRecipeManager;
import crazypants.enderio.machine.alloy.BlockAlloySmelter;
import crazypants.enderio.machine.crafter.BlockCrafter;
import crazypants.enderio.machine.crusher.BlockCrusher;
import crazypants.enderio.machine.crusher.CrusherRecipeManager;
import crazypants.enderio.machine.farm.BlockFarmStation;
@@ -150,6 +151,7 @@ public class EnderIO {
public static BlockVat blockVat;
public static BlockFarmStation blockFarmStation;
public static BlockTank blockTank;
public static BlockCrafter blockCrafter;
public static BlockElectricLight blockElectricLight;
public static BlockLightNode blockLightNode;
@@ -223,6 +225,7 @@ public class EnderIO {
blockPaintedDoubleSlab = new BlockPaintedSlab(true);
blockPaintedSlab.init();
blockPaintedDoubleSlab.init();
blockCrafter = BlockCrafter.create();
blockHyperCube = BlockHyperCube.create();
blockElectricLight = BlockElectricLight.create();
@@ -36,6 +36,7 @@ public class GuiHandler implements IGuiHandler {
public static final int GUI_ID_COMBUSTION_GEN = 21;
public static final int GUI_ID_FARM_STATATION = 22;
public static final int GUI_ID_TANK = 86;
public static final int GUI_ID_CRAFTER = 87;
protected final Map<Integer, IGuiHandler> guiHandlers = new HashMap<Integer, IGuiHandler>();
@@ -42,6 +42,7 @@ public enum ModObject {
blockVat,
blockFarmStation,
blockTank,
blockCrafter,
blockElectricLight,
blockLightNode,
@@ -1,5 +1,7 @@
package crazypants.enderio.machine;
import java.awt.Point;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
@@ -16,7 +18,7 @@ public abstract class AbstractMachineContainer extends Container {
addMachineSlots(playerInv);
if(te.getSlotDefinition().getNumUpgradeSlots() == 1) {
addSlotToContainer(new Slot(te, te.getSlotDefinition().getMinUpgradeSlot(), 12, 60) {
addSlotToContainer(new Slot(te, te.getSlotDefinition().getMinUpgradeSlot(), getUpgradeOffset().x, getUpgradeOffset().y) {
@Override
public int getSlotStackLimit() {
@@ -30,15 +32,17 @@ public abstract class AbstractMachineContainer extends Container {
});
}
int x = getPlayerInventoryOffset().x;
int y = getPlayerInventoryOffset().y;
// add players inventory
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, x + j * 18, y + i * 18));
}
}
for (int i = 0; i < 9; ++i) {
addSlotToContainer(new Slot(playerInv, i, 8 + i * 18, 142));
addSlotToContainer(new Slot(playerInv, i, x + i * 18, y + 58));
}
}
@@ -47,6 +51,14 @@ public abstract class AbstractMachineContainer extends Container {
return tileEntity.isUseableByPlayer(entityplayer);
}
protected Point getPlayerInventoryOffset() {
return new Point(8,84);
}
protected Point getUpgradeOffset() {
return new Point(12,60);
}
protected abstract void addMachineSlots(InventoryPlayer playerInv);
@Override
@@ -709,7 +709,7 @@ public abstract class AbstractMachineEntity extends TileEntityEio implements ISi
if(isSideDisabled(side)) {
return false;
}
return slotDefinition.isInputSlot(slot);
return slotDefinition.isInputSlot(slot) && isMachineItemValidForSlot(slot, var2);
}
@Override
@@ -60,7 +60,7 @@ public abstract class GuiMachineBase extends GuiContainerBase {
});
}
int x = xSize - 5 - BUTTON_SIZE;
int x = getXSize() - 5 - BUTTON_SIZE;
int y = 5;
redstoneButton = new RedstoneModeButton(this, -1, x, y, tileEntity, new BlockCoord(tileEntity));
@@ -150,7 +150,7 @@ public abstract class GuiMachineBase extends GuiContainerBase {
if(renderPowerBar()) {
int i1 = tileEntity.getEnergyStoredScaled(getPowerHeight());
// x, y, u, v, width, height
drawTexturedModalRect(k + getPowerX(), l + (getPowerY() + getPowerHeight()) - i1, 176, 31, getPowerWidth(), i1);
drawTexturedModalRect(k + getPowerX(), l + (getPowerY() + getPowerHeight()) - i1, getPowerU(), getPowerV(), getPowerWidth(), i1);
}
for (int i = 0; i < buttonList.size(); ++i) {
@@ -172,6 +172,14 @@ public abstract class GuiMachineBase extends GuiContainerBase {
}
protected int getPowerV() {
return 31;
}
protected int getPowerU() {
return 176;
}
protected boolean renderPowerBar() {
return true;
}
@@ -0,0 +1,55 @@
package crazypants.enderio.machine.crafter;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import crazypants.enderio.EnderIO;
import crazypants.enderio.GuiHandler;
import crazypants.enderio.ModObject;
import crazypants.enderio.machine.AbstractMachineBlock;
import crazypants.enderio.machine.alloy.BlockAlloySmelter;
import crazypants.enderio.machine.alloy.ContainerAlloySmelter;
import crazypants.enderio.machine.alloy.PacketClientState;
import crazypants.enderio.machine.alloy.TileAlloySmelter;
public class BlockCrafter extends AbstractMachineBlock<TileCrafter> {
public static BlockCrafter create() {
BlockCrafter res = new BlockCrafter();
res.init();
return res;
}
protected BlockCrafter() {
super(ModObject.blockCrafter, TileCrafter.class);
}
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
TileEntity te = world.getTileEntity(x, y, z);
if(te instanceof TileCrafter) {
return new ContainerCrafter(player.inventory, (TileCrafter) te);
}
return null;
}
@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
TileEntity te = world.getTileEntity(x, y, z);
if(te instanceof TileCrafter) {
return new GuiCrafter(player.inventory, (TileCrafter) te);
}
return null;
}
@Override
protected int getGuiId() {
return GuiHandler.GUI_ID_CRAFTER;
}
@Override
protected String getMachineFrontIconKey(boolean active) {
return "enderio:crafter";
}
}
@@ -0,0 +1,125 @@
package crazypants.enderio.machine.crafter;
import java.awt.Point;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import crazypants.enderio.machine.AbstractMachineContainer;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.gui.TemplateSlot;
public class ContainerCrafter extends AbstractMachineContainer {
private TileCrafter crafter;
private DummyCraftingGrid craftingGrid;
public ContainerCrafter(InventoryPlayer playerInv, TileCrafter te) {
super(playerInv, te);
addCrafterSlots();
}
private void addCrafterSlots() {
crafter = (TileCrafter) tileEntity;
craftingGrid = crafter.craftingGrid;
int topY = 16;
int leftX = 31;
int index = 0;
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
int x = leftX + col * 18;
int y = topY + row * 18;
addSlotToContainer(new DummySlot(craftingGrid, index, x, y));
index++;
}
}
addSlotToContainer(new DummySlot(craftingGrid, 9, 90, 34));
}
@Override
protected Point getPlayerInventoryOffset() {
return new Point(30, 84);
}
protected Point getUpgradeOffset() {
return new Point(6, 60);
}
@Override
protected void addMachineSlots(InventoryPlayer playerInv) {
int topY = 16;
int leftX = 31;
int index = 0;
leftX = 113;
index = 0;
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
int x = leftX + col * 18;
int y = topY + row * 18;
addSlotToContainer(new InputSlot(tileEntity, index, x, y));
index++;
}
}
addSlotToContainer(new Slot(tileEntity, 9, 172, 34) {
@Override
public boolean isItemValid(ItemStack itemStack) {
return false;
}
});
}
private class InputSlot extends Slot {
public InputSlot(IInventory par1iInventory, int par2, int par3, int par4) {
super(par1iInventory, par2, par3, par4);
}
@Override
public boolean isItemValid(ItemStack itemStack) {
ItemStack refStack = crafter.craftingGrid.getStackInSlot(slotNumber);
if(refStack == null || itemStack == null) {
return false;
}
return refStack.isItemEqual(itemStack);
}
}
private class DummySlot extends TemplateSlot {
public DummySlot(IInventory inventory, int slotIndex, int x, int y) {
super(inventory, slotIndex, x, y);
}
@Override
public boolean isItemValid(ItemStack itemStack) {
return true;
}
@Override
public void putStack(ItemStack par1ItemStack) {
if(par1ItemStack != null) {
par1ItemStack.stackSize = 0;
}
inventory.setInventorySlotContents(slotIndex, par1ItemStack);
onSlotChanged();
}
@Override
public void onSlotChanged() {
super.onSlotChanged();
crafter.updateCraftingOutput();
}
}
}
@@ -0,0 +1,146 @@
package crazypants.enderio.machine.crafter;
import java.util.List;
import java.util.Map.Entry;
import crazypants.enderio.machine.IoMode;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.ForgeDirection;
public class DummyCraftingGrid implements IInventory {
ItemStack[] inv = new ItemStack[10];
public boolean hasValidRecipe() {
return getOutput() != null;
}
public ItemStack getOutput() {
return inv[9];
}
public void copyRequiredInputs(List<ItemStack> required) {
for (int i = 0; i < 9; i++) {
ItemStack is = inv[i];
if(is != null) {
ItemStack copy = is.copy();
copy.stackSize = 1;
required.add(copy);
}
}
}
@Override
public int getSizeInventory() {
return inv.length;
}
@Override
public ItemStack getStackInSlot(int var1) {
if(var1 < 0 || var1 >= inv.length) {
return null;
}
return inv[var1];
}
@Override
public ItemStack decrStackSize(int fromSlot, int amount) {
ItemStack item = inv[fromSlot];
inv[fromSlot] = null;
if(item == null) {
return null;
}
item.stackSize = 0;
return item;
}
@Override
public ItemStack getStackInSlotOnClosing(int var1) {
return null;
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
if(itemstack != null) {
inv[i] = itemstack.copy();
if(i < 9) {
inv[i].stackSize = 0;
}
} else {
inv[i] = null;
}
}
@Override
public String getInventoryName() {
return "CraftingGrid";
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public int getInventoryStackLimit() {
return 0;
}
@Override
public void markDirty() {
}
@Override
public boolean isUseableByPlayer(EntityPlayer var1) {
return true;
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public boolean isItemValidForSlot(int var1, ItemStack var2) {
return var1 < 9;
}
public void readFromNBT(NBTTagCompound nbtRoot) {
NBTTagList itemList = (NBTTagList) nbtRoot.getTag("Items");
if(itemList == null) {
for (int i = 0; i < inv.length; i++) {
inv[i] = null;
}
return;
}
for (int i = 0; i < itemList.tagCount(); i++) {
NBTTagCompound itemStack = itemList.getCompoundTagAt(i);
byte slot = itemStack.getByte("Slot");
if(slot >= 0 && slot < inv.length) {
inv[slot] = ItemStack.loadItemStackFromNBT(itemStack);
}
}
}
public void writeToNBT(NBTTagCompound nbtRoot) {
NBTTagList itemList = new NBTTagList();
for (int i = 0; i < inv.length; i++) {
if(inv[i] != null) {
NBTTagCompound itemStackNBT = new NBTTagCompound();
itemStackNBT.setByte("Slot", (byte) i);
inv[i].writeToNBT(itemStackNBT);
itemList.appendTag(itemStackNBT);
}
}
nbtRoot.setTag("Items", itemList);
}
}
@@ -0,0 +1,60 @@
package crazypants.enderio.machine.crafter;
import java.awt.Color;
import java.awt.Point;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.entity.player.InventoryPlayer;
import org.lwjgl.opengl.GL11;
import crazypants.enderio.machine.GuiMachineBase;
import crazypants.enderio.machine.generator.StirlingGeneratorContainer;
import crazypants.enderio.machine.generator.TileEntityStirlingGenerator;
import crazypants.enderio.machine.power.PowerDisplayUtil;
import crazypants.render.ColorUtil;
import crazypants.render.RenderUtil;
import crazypants.util.Lang;
public class GuiCrafter extends GuiMachineBase {
private TileCrafter entity;
public GuiCrafter(InventoryPlayer par1InventoryPlayer, TileCrafter te) {
super(te, new ContainerCrafter(par1InventoryPlayer, te));
this.entity = te;
xSize = getXSize();
}
@Override
protected boolean showRecipeButton() {
return false;
}
@Override
public int getXSize() {
return 219;
}
protected int getPowerU() {
return 220;
}
@Override
protected int getPowerX() {
return 9;
}
@Override
protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
RenderUtil.bindTexture("enderio:textures/gui/crafter.png");
int sx = (width - xSize) / 2;
int sy = (height - ySize) / 2;
drawTexturedModalRect(sx, sy, 0, 0, this.xSize, this.ySize);
super.drawGuiContainerBackgroundLayer(par1, par2, par3);
}
}
@@ -0,0 +1,169 @@
package crazypants.enderio.machine.crafter;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.nbt.NBTTagCompound;
import crazypants.enderio.Config;
import crazypants.enderio.ModObject;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.machine.SlotDefinition;
import crazypants.enderio.power.Capacitors;
public class TileCrafter extends AbstractMachineEntity {
DummyCraftingGrid craftingGrid = new DummyCraftingGrid();
public TileCrafter() {
super(new SlotDefinition(9, 1));
}
@Override
public String getMachineName() {
return ModObject.blockCrafter.unlocalisedName;
}
@Override
protected boolean isMachineItemValidForSlot(int slot, ItemStack itemstack) {
if(!slotDefinition.isInputSlot(slot)) {
return false;
}
return craftingGrid.inv[slot] != null && craftingGrid.inv[slot].isItemEqual(itemstack);
}
@Override
public boolean isActive() {
return false;
}
@Override
public float getProgress() {
return 0.5f;
}
@Override
protected boolean processTasks(boolean redstoneCheckPassed) {
if(!redstoneCheckPassed || !craftingGrid.hasValidRecipe() || !canMergeOutput() || !hasPower()) {
return false;
}
if(capacitorType == Capacitors.BASIC_CAPACITOR && worldObj.getTotalWorldTime() % 20 != 0) {
return false;
} else if(capacitorType == Capacitors.ACTIVATED_CAPACITOR && worldObj.getTotalWorldTime() % 10 != 0) {
return false;
} else if(worldObj.getTotalWorldTime() % 2 != 0) {
return false;
}
List<ItemStack> required = new ArrayList<ItemStack>();
craftingGrid.copyRequiredInputs(required);
if(hasRequiredInput(required)) {
craftRecipe();
double used = Math.min(powerHandler.getEnergyStored(), Config.crafterMjPerCraft);
powerHandler.setEnergy(powerHandler.getEnergyStored() - used);
}
return false;
}
private void craftRecipe() {
List<ItemStack> required = new ArrayList<ItemStack>();
craftingGrid.copyRequiredInputs(required);
for(ItemStack req : required) {
for(int i=0;i<9 && req.stackSize > 0; i++) {
ItemStack avail = inventory[i];
if(avail != null && avail.stackSize > 0 && avail.isItemEqual(req)) {
req.stackSize--;
avail = avail.copy();
avail.stackSize--;
if(avail.stackSize <= 0) {
avail = null;
}
setInventorySlotContents(i, avail);
}
}
}
ItemStack output = craftingGrid.getOutput().copy();
if(inventory[9] == null) {
setInventorySlotContents(9, output);
} else {
ItemStack cur = inventory[9].copy();
cur.stackSize += output.stackSize;
setInventorySlotContents(9, cur);
}
}
private boolean canMergeOutput() {
if(inventory[9] == null) {
return true;
}
ItemStack output = craftingGrid.getOutput();
if(!inventory[9].isItemEqual(output)) {
return false;
}
return output.getMaxStackSize() >= (inventory[9].stackSize + output.stackSize);
}
private boolean hasRequiredInput(List<ItemStack> required) {
List<ItemStack> available = new ArrayList<ItemStack>();
for(int i=0;i<9;i++) {
ItemStack is = inventory[i];
if(is != null) {
available.add(is.copy());
}
}
for(ItemStack req : required) {
boolean foundReq = false;
for(ItemStack avail : available) {
if(req.isItemEqual(avail) && avail.stackSize > 0) {
avail.stackSize--;
foundReq = true;
break;
}
}
if(!foundReq) {
return false;
}
}
return true;
}
@Override
public void readCommon(NBTTagCompound nbtRoot) {
super.readCommon(nbtRoot);
NBTTagCompound craftRoot = nbtRoot.getCompoundTag("craftingGrid");
craftingGrid.readFromNBT(craftRoot);
}
@Override
public void writeCommon(NBTTagCompound nbtRoot) {
super.writeCommon(nbtRoot);
NBTTagCompound craftingRoot = new NBTTagCompound();
craftingGrid.writeToNBT(craftingRoot);
nbtRoot.setTag("craftingGrid", craftingRoot);
}
public void updateCraftingOutput() {
InventoryCrafting inv = new InventoryCrafting(new Container() {
@Override
public boolean canInteractWith(EntityPlayer var1) {
return false;
}
}, 3, 3);
for(int i=0;i<9;i++) {
inv.setInventorySlotContents(i, craftingGrid.getStackInSlot(i));
}
ItemStack matches = CraftingManager.getInstance().findMatchingRecipe(inv, worldObj);
craftingGrid.setInventorySlotContents(9, matches);
markDirty();
}
}
@@ -147,7 +147,7 @@ public class TileEntityStirlingGenerator extends AbstractMachineEntity implement
storedEnergy += (ENERGY_PER_TICK * getEnergyMultiplier());
}
burnTime--;
sendBurnTimePacket = worldObj.getWorldTime() % 20 == 1;
sendBurnTimePacket = worldObj.getWorldTime() % 20 == 1 || burnTime == 0;
}
transmitEnergy();
@@ -7,7 +7,7 @@ import net.minecraft.item.ItemStack;
public class TemplateSlot extends Slot {
int slotIndex;
protected int slotIndex;
public TemplateSlot(IInventory inventory, int slotIndex, int x, int y) {
super(inventory, slotIndex, x, y);