Reworked Imprinter to have an inventory

This commit is contained in:
Henry Mao 2013-01-12 12:41:01 +08:00
parent f58b88d30e
commit 8a561c5921
9 changed files with 288 additions and 220 deletions

View file

@ -5,7 +5,6 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.client.MinecraftForgeClient;
import universalelectricity.core.vector.Vector3;
import assemblyline.client.gui.GuiEncoder;
import assemblyline.client.gui.GuiImprinter;
import assemblyline.client.render.BlockRenderingHandler;
@ -24,6 +23,7 @@ import assemblyline.common.machine.armbot.TileEntityArmbot;
import assemblyline.common.machine.belt.TileEntityConveyorBelt;
import assemblyline.common.machine.detector.TileEntityDetector;
import assemblyline.common.machine.encoder.TileEntityEncoder;
import assemblyline.common.machine.imprinter.TileEntityImprinter;
import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.client.registry.RenderingRegistry;
@ -57,8 +57,8 @@ public class ClientProxy extends CommonProxy
switch (ID)
{
case GUI_STAMPER:
return new GuiImprinter(player.inventory, world, new Vector3(x, y, z));
case GUI_IMPRINTER:
return new GuiImprinter(player.inventory, (TileEntityImprinter) tileEntity);
case GUI_ENCODER:
if (tileEntity != null && tileEntity instanceof TileEntityEncoder)
return new GuiEncoder(player.inventory, (TileEntityEncoder) tileEntity);

View file

@ -2,23 +2,22 @@ package assemblyline.client.gui;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.world.World;
import org.lwjgl.opengl.GL11;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.TranslationHelper;
import assemblyline.common.AssemblyLine;
import assemblyline.common.machine.imprinter.ContainerImprinter;
import assemblyline.common.machine.imprinter.TileEntityImprinter;
public class GuiImprinter extends GuiContainer
{
private int containerWidth;
private int containerHeight;
public GuiImprinter(InventoryPlayer par1InventoryPlayer, World worldObj, Vector3 position)
public GuiImprinter(InventoryPlayer par1InventoryPlayer, TileEntityImprinter tileEntity)
{
super(new ContainerImprinter(par1InventoryPlayer, worldObj, position));
super(new ContainerImprinter(par1InventoryPlayer, tileEntity));
}
/**

View file

@ -3,7 +3,6 @@ package assemblyline.common;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.multiblock.TileEntityMulti;
import assemblyline.common.block.TileEntityCrate;
import assemblyline.common.machine.TileEntityManipulator;
@ -14,14 +13,14 @@ import assemblyline.common.machine.detector.TileEntityDetector;
import assemblyline.common.machine.encoder.ContainerEncoder;
import assemblyline.common.machine.encoder.TileEntityEncoder;
import assemblyline.common.machine.imprinter.ContainerImprinter;
import assemblyline.common.machine.imprinter.TileEntityImprinter;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.registry.GameRegistry;
public class CommonProxy implements IGuiHandler
{
public static final int GUI_STAMPER = 1;
public static final int GUI_ARCHITECHT_TABLE = 2;
public static final int GUI_ENCODER = 3;
public static final int GUI_IMPRINTER = 1;
public static final int GUI_ENCODER = 2;
public void preInit()
{
@ -37,6 +36,7 @@ public class CommonProxy implements IGuiHandler
GameRegistry.registerTileEntity(TileEntityDetector.class, "ALDetector");
GameRegistry.registerTileEntity(TileEntityEncoder.class, "ALEncoder");
GameRegistry.registerTileEntity(TileEntityArmbot.class, "ALArmbot");
GameRegistry.registerTileEntity(TileEntityImprinter.class, "ALImprinter");
GameRegistry.registerTileEntity(TileEntityMulti.class, "ALMulti");
}
@ -47,8 +47,8 @@ public class CommonProxy implements IGuiHandler
switch (ID)
{
case GUI_STAMPER:
return new ContainerImprinter(player.inventory, world, new Vector3(x, y, z));
case GUI_IMPRINTER:
return new ContainerImprinter(player.inventory, (TileEntityImprinter) tileEntity);
case GUI_ENCODER:
{
if (tileEntity != null && tileEntity instanceof TileEntityEncoder)

View file

@ -5,6 +5,8 @@ import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import assemblyline.common.AssemblyLine;
import assemblyline.common.machine.imprinter.SlotCustom;
public class ContainerEncoder extends Container
{
@ -20,7 +22,7 @@ public class ContainerEncoder extends Container
this.tileEntity = encoder;
// Disk
this.addSlotToContainer(new Slot(encoder, 0, 80, 24 + Y_OFFSET));
this.addSlotToContainer(new SlotCustom(encoder, 0, 80, 24 + Y_OFFSET, new ItemStack(AssemblyLine.itemDisk)));
int var3;

View file

@ -2,6 +2,7 @@ package assemblyline.common.machine.imprinter;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import universalelectricity.prefab.BlockMachine;
import assemblyline.common.AssemblyLine;
@ -44,11 +45,16 @@ public class BlockImprinter extends BlockMachine
{
if (!world.isRemote)
{
entityPlayer.openGui(AssemblyLine.instance, CommonProxy.GUI_STAMPER, world, x, y, z);
entityPlayer.openGui(AssemblyLine.instance, CommonProxy.GUI_IMPRINTER, world, x, y, z);
}
return true;
}
@Override
public TileEntity createNewTileEntity(World var1)
{
return new TileEntityImprinter();
}
}

View file

@ -14,13 +14,14 @@ import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import assemblyline.common.AssemblyLine;
import assemblyline.common.Pair;
import cpw.mods.fml.relauncher.ReflectionHelper;
public class ContainerImprinter extends Container implements ISlotWatcher
{
private InventoryPlayer inventoryPlayer;
private TileEntityImprinter tileEntity;
public TileEntityImprinter tileEntity;
public ContainerImprinter(InventoryPlayer inventoryPlayer, TileEntityImprinter tileEntity)
{
@ -28,13 +29,13 @@ public class ContainerImprinter extends Container implements ISlotWatcher
this.inventoryPlayer = inventoryPlayer;
// Paper Input
this.addSlotToContainer(new SlotImprint(this.tileEntity, 0, 42, 24));
this.addSlotToContainer(new SlotCustom(this.tileEntity, 0, 42, 24, new ItemStack(AssemblyLine.itemImprint)));
// Item Stamp
this.addSlotToContainer(new Slot(this.tileEntity, 1, 78, 24));
// Output Filter
this.addSlotToContainer(new SlotImprintResult(this.tileEntity, 2, 136, 24));
// Crafting Slot
this.addSlotToContainer(new SlotImprint(this.tileEntity, 3, 78, 53));
this.addSlotToContainer(new SlotCustom(this.tileEntity, 3, 78, 53, new ItemStack(AssemblyLine.itemImprint)));
// Crafting Output
this.addSlotToContainer(new SlotCraftingResult(this, this.tileEntity, 4, 136, 53));
@ -109,204 +110,9 @@ public class ContainerImprinter extends Container implements ISlotWatcher
return copyStack;
}
/**
* Does this player's inventory contain the required resources to craft this item?
*
* @return Required Items
*/
public Pair<ItemStack, ItemStack[]> getIdealRecipe(ItemStack outputItem)
{
for (Object object : CraftingManager.getInstance().getRecipeList())
{
if (object instanceof IRecipe)
{
if (((IRecipe) object).getRecipeOutput() != null)
{
if (outputItem.isItemEqual(((IRecipe) object).getRecipeOutput()))
{
if (object instanceof ShapedRecipes)
{
if (this.hasResource(((ShapedRecipes) object).recipeItems) != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), ((ShapedRecipes) object).recipeItems); }
}
else if (object instanceof ShapelessRecipes)
{
if (this.hasResource(((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1])) != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), (ItemStack[]) ((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1])); }
}
else if (object instanceof ShapedOreRecipe)
{
ShapedOreRecipe oreRecipe = (ShapedOreRecipe) object;
Object[] oreRecipeInput = (Object[]) ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, oreRecipe, "input");
ArrayList<ItemStack> hasResources = this.hasResource(oreRecipeInput);
if (hasResources != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), hasResources.toArray(new ItemStack[1])); }
}
else if (object instanceof ShapelessOreRecipe)
{
ShapelessOreRecipe oreRecipe = (ShapelessOreRecipe) object;
ArrayList oreRecipeInput = (ArrayList) ReflectionHelper.getPrivateValue(ShapelessOreRecipe.class, oreRecipe, "input");
List<ItemStack> hasResources = this.hasResource(oreRecipeInput.toArray());
if (hasResources != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), hasResources.toArray(new ItemStack[1])); }
}
}
}
}
}
return null;
}
/**
* Returns if players has the following resource required.
*
* @param recipeItems - The items to be checked for the recipes.
*/
private ArrayList<ItemStack> hasResource(Object[] recipeItems)
{
/**
* The actual amount of resource required. Each ItemStack will only have stacksize of 1.
*/
ArrayList<ItemStack> actualResources = new ArrayList<ItemStack>();
int itemMatch = 0;
for (Object obj : recipeItems)
{
if (obj instanceof ItemStack)
{
ItemStack recipeItem = (ItemStack) obj;
actualResources.add(recipeItem.copy());
if (recipeItem != null)
{
for (int i = 0; i < this.inventoryPlayer.getSizeInventory(); i++)
{
ItemStack checkStack = this.inventoryPlayer.getStackInSlot(i);
if (checkStack != null)
{
if (SlotCraftingResult.isItemEqual(recipeItem, checkStack))
{
// TODO Do NBT CHecking
itemMatch++;
break;
}
}
}
}
}
else if (obj instanceof ArrayList)
{
ArrayList ingredientsList = (ArrayList) obj;
Object[] ingredientsArray = ingredientsList.toArray();
optionsLoop:
for (int x = 0; x < ingredientsArray.length; x++)
{
if (ingredientsArray[x] != null && ingredientsArray[x] instanceof ItemStack)
{
ItemStack recipeItem = (ItemStack) ingredientsArray[x];
actualResources.add(recipeItem.copy());
if (recipeItem != null)
{
for (int i = 0; i < this.inventoryPlayer.getSizeInventory(); i++)
{
ItemStack checkStack = this.inventoryPlayer.getStackInSlot(i);
if (checkStack != null)
{
if (SlotCraftingResult.isItemEqual(recipeItem, checkStack))
{
// TODO Do NBT CHecking
itemMatch++;
break optionsLoop;
}
}
}
}
}
}
}
}
return itemMatch >= actualResources.size() ? actualResources : null;
}
@Override
public void slotContentsChanged()
{
/**
* Makes the stamping recipe for filters
*/
boolean didStamp = false;
if (this.tileEntity.getStackInSlot(0) != null && this.tileEntity.getStackInSlot(1) != null)
{
if (this.tileEntity.getStackInSlot(0).getItem() instanceof ItemImprinter)
{
ItemStack outputStack = this.tileEntity.getStackInSlot(0).copy();
outputStack.stackSize = 1;
ArrayList<ItemStack> filters = ItemImprinter.getFilters(outputStack);
boolean filteringItemExists = false;
for (ItemStack filteredStack : filters)
{
if (filteredStack.isItemEqual(this.tileEntity.getStackInSlot(1)))
{
filters.remove(filteredStack);
filteringItemExists = true;
break;
}
}
if (!filteringItemExists)
{
filters.add(this.tileEntity.getStackInSlot(1));
}
ItemImprinter.setFilters(outputStack, filters);
this.tileEntity.setInventorySlotContents(2, outputStack);
didStamp = true;
}
}
if (!didStamp)
{
this.tileEntity.setInventorySlotContents(2, null);
}
// CRAFTING
boolean didCraft = false;
if (this.tileEntity.getStackInSlot(3) != null)
{
if (this.tileEntity.getStackInSlot(3).getItem() instanceof ItemImprinter)
{
ArrayList<ItemStack> filters = ItemImprinter.getFilters(this.tileEntity.getStackInSlot(3));
if (filters.size() > 0)
{
ItemStack outputStack = filters.get(0);
if (outputStack != null)
{
Pair<ItemStack, ItemStack[]> idealRecipe = this.getIdealRecipe(outputStack);
if (idealRecipe != null)
{
this.tileEntity.setInventorySlotContents(4, idealRecipe.getKey());
didCraft = true;
}
}
}
}
}
if (!didCraft)
{
this.tileEntity.setInventorySlotContents(4, null);
}
this.tileEntity.onInventoryChanged();
}
}

View file

@ -35,7 +35,7 @@ public class SlotCraftingResult extends Slot
if (this.getStack() != null)
{
ItemStack[] requiredItems = this.container.getIdealRecipe(this.getStack()).getValue().clone();
ItemStack[] requiredItems = this.container.tileEntity.getIdealRecipe(this.getStack()).getValue().clone();
if (requiredItems != null)
{
@ -63,7 +63,7 @@ public class SlotCraftingResult extends Slot
{
if (this.getStack() != null)
{
ItemStack[] idealRecipe = this.container.getIdealRecipe(this.getStack()).getValue();
ItemStack[] idealRecipe = this.container.tileEntity.getIdealRecipe(this.getStack()).getValue();
if (idealRecipe != null)
{
ItemStack[] requiredItems = idealRecipe.clone();

View file

@ -5,17 +5,18 @@ import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import assemblyline.common.AssemblyLine;
public class SlotImprint extends Slot
public class SlotCustom extends Slot
{
private ItemStack itemStack;
public SlotImprint(IInventory par1iInventory, int par2, int par3, int par4)
public SlotCustom(IInventory par1iInventory, int par2, int par3, int par4, ItemStack itemStack)
{
super(par1iInventory, par2, par3, par4);
this.itemStack = itemStack;
}
public boolean isItemValid(ItemStack itemStack)
{
return itemStack.itemID == AssemblyLine.itemImprint.itemID;
return itemStack.isItemEqual(this.itemStack);
}
}

View file

@ -9,6 +9,8 @@ import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.ISidedInventory;
import net.minecraftforge.oredict.ShapedOreRecipe;
@ -25,6 +27,8 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
*/
private ItemStack[] containingItems = new ItemStack[5 + 10];
public static final int START_INVENTORY = 5;
@Override
public boolean canUpdate()
{
@ -134,4 +138,254 @@ public class TileEntityImprinter extends TileEntityAdvanced implements ISidedInv
public void closeChest()
{
}
@Override
public void onInventoryChanged()
{
/**
* Makes the stamping recipe for filters
*/
boolean didStamp = false;
if (this.getStackInSlot(0) != null && this.getStackInSlot(1) != null)
{
if (this.getStackInSlot(0).getItem() instanceof ItemImprinter)
{
ItemStack outputStack = this.getStackInSlot(0).copy();
outputStack.stackSize = 1;
ArrayList<ItemStack> filters = ItemImprinter.getFilters(outputStack);
boolean filteringItemExists = false;
for (ItemStack filteredStack : filters)
{
if (filteredStack.isItemEqual(this.getStackInSlot(1)))
{
filters.remove(filteredStack);
filteringItemExists = true;
break;
}
}
if (!filteringItemExists)
{
filters.add(this.getStackInSlot(1));
}
ItemImprinter.setFilters(outputStack, filters);
this.setInventorySlotContents(2, outputStack);
didStamp = true;
}
}
if (!didStamp)
{
this.setInventorySlotContents(2, null);
}
// CRAFTING
boolean didCraft = false;
if (this.getStackInSlot(3) != null)
{
if (this.getStackInSlot(3).getItem() instanceof ItemImprinter)
{
ArrayList<ItemStack> filters = ItemImprinter.getFilters(this.getStackInSlot(3));
if (filters.size() > 0)
{
ItemStack outputStack = filters.get(0);
if (outputStack != null)
{
Pair<ItemStack, ItemStack[]> idealRecipe = this.getIdealRecipe(outputStack);
if (idealRecipe != null)
{
this.setInventorySlotContents(4, idealRecipe.getKey());
didCraft = true;
}
}
}
}
}
if (!didCraft)
{
this.setInventorySlotContents(4, null);
}
}
/**
* Does this player's inventory contain the required resources to craft this item?
*
* @return Required Items
*/
public Pair<ItemStack, ItemStack[]> getIdealRecipe(ItemStack outputItem)
{
for (Object object : CraftingManager.getInstance().getRecipeList())
{
if (object instanceof IRecipe)
{
if (((IRecipe) object).getRecipeOutput() != null)
{
if (outputItem.isItemEqual(((IRecipe) object).getRecipeOutput()))
{
if (object instanceof ShapedRecipes)
{
if (this.hasResource(((ShapedRecipes) object).recipeItems) != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), ((ShapedRecipes) object).recipeItems); }
}
else if (object instanceof ShapelessRecipes)
{
if (this.hasResource(((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1])) != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), (ItemStack[]) ((ShapelessRecipes) object).recipeItems.toArray(new ItemStack[1])); }
}
else if (object instanceof ShapedOreRecipe)
{
ShapedOreRecipe oreRecipe = (ShapedOreRecipe) object;
Object[] oreRecipeInput = (Object[]) ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, oreRecipe, "input");
ArrayList<ItemStack> hasResources = this.hasResource(oreRecipeInput);
if (hasResources != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), hasResources.toArray(new ItemStack[1])); }
}
else if (object instanceof ShapelessOreRecipe)
{
ShapelessOreRecipe oreRecipe = (ShapelessOreRecipe) object;
ArrayList oreRecipeInput = (ArrayList) ReflectionHelper.getPrivateValue(ShapelessOreRecipe.class, oreRecipe, "input");
List<ItemStack> hasResources = this.hasResource(oreRecipeInput.toArray());
if (hasResources != null) { return new Pair<ItemStack, ItemStack[]>(((IRecipe) object).getRecipeOutput().copy(), hasResources.toArray(new ItemStack[1])); }
}
}
}
}
}
return null;
}
/**
* Returns if players has the following resource required.
*
* @param recipeItems - The items to be checked for the recipes.
*/
private ArrayList<ItemStack> hasResource(Object[] recipeItems)
{
/**
* The actual amount of resource required. Each ItemStack will only have stacksize of 1.
*/
ArrayList<ItemStack> actualResources = new ArrayList<ItemStack>();
int itemMatch = 0;
for (Object obj : recipeItems)
{
if (obj instanceof ItemStack)
{
ItemStack recipeItem = (ItemStack) obj;
actualResources.add(recipeItem.copy());
if (recipeItem != null)
{
for (int i = START_INVENTORY; i < this.getSizeInventory(); i++)
{
ItemStack checkStack = this.getStackInSlot(i);
if (checkStack != null)
{
if (SlotCraftingResult.isItemEqual(recipeItem, checkStack))
{
// TODO Do NBT CHecking
itemMatch++;
break;
}
}
}
}
}
else if (obj instanceof ArrayList)
{
ArrayList ingredientsList = (ArrayList) obj;
Object[] ingredientsArray = ingredientsList.toArray();
optionsLoop:
for (int x = 0; x < ingredientsArray.length; x++)
{
if (ingredientsArray[x] != null && ingredientsArray[x] instanceof ItemStack)
{
ItemStack recipeItem = (ItemStack) ingredientsArray[x];
actualResources.add(recipeItem.copy());
if (recipeItem != null)
{
for (int i = START_INVENTORY; i < this.getSizeInventory(); i++)
{
ItemStack checkStack = this.getStackInSlot(i);
if (checkStack != null)
{
if (SlotCraftingResult.isItemEqual(recipeItem, checkStack))
{
// TODO Do NBT CHecking
itemMatch++;
break optionsLoop;
}
}
}
}
}
}
}
}
return itemMatch >= actualResources.size() ? actualResources : null;
}
/**
* NBT Data
*/
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
NBTTagList var2 = nbt.getTagList("Items");
this.containingItems = new ItemStack[this.getSizeInventory()];
for (int var3 = 0; var3 < var2.tagCount(); ++var3)
{
NBTTagCompound var4 = (NBTTagCompound) var2.tagAt(var3);
byte var5 = var4.getByte("Slot");
if (var5 >= 0 && var5 < this.containingItems.length)
{
this.containingItems[var5] = ItemStack.loadItemStackFromNBT(var4);
}
}
}
/**
* Writes a tile entity to NBT.
*/
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
NBTTagList var2 = new NBTTagList();
for (int var3 = 0; var3 < this.containingItems.length; ++var3)
{
if (this.containingItems[var3] != null)
{
NBTTagCompound var4 = new NBTTagCompound();
var4.setByte("Slot", (byte) var3);
this.containingItems[var3].writeToNBT(var4);
var2.appendTag(var4);
}
}
nbt.setTag("Items", var2);
}
}