Finished Tool Table Logic

(When an extra item is in the tool table that doesn't belong to the recipe, it'll just be ignored and not taken away)
This commit is contained in:
Ellpeck 2015-09-22 23:32:19 +02:00
parent 9c5b8e3c5c
commit d02c5f61e9
5 changed files with 97 additions and 18 deletions

View file

@ -11,11 +11,10 @@
package ellpeck.actuallyadditions.inventory;
import ellpeck.actuallyadditions.inventory.slot.SlotOutput;
import ellpeck.actuallyadditions.items.InitItems;
import ellpeck.actuallyadditions.items.metalists.TheMiscItems;
import ellpeck.actuallyadditions.recipe.ToolTableHandler;
import ellpeck.actuallyadditions.tile.TileEntityBase;
import ellpeck.actuallyadditions.tile.TileEntityCoffeeMachine;
import ellpeck.actuallyadditions.tile.TileEntityToolTable;
import ellpeck.actuallyadditions.util.ItemUtil;
import invtweaks.api.container.InventoryContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
@ -31,13 +30,41 @@ public class ContainerToolTable extends Container{
public ContainerToolTable(InventoryPlayer inventory, TileEntityBase tile){
this.table = (TileEntityToolTable)tile;
this.addSlotToContainer(new SlotOutput(this.table, 0, 115, 25));
for(int i = 0; i < 3; i++){
for(int j = 0; j < 2; j++){
this.addSlotToContainer(new Slot(this.table, j+i*2+1, 35+j*18, 7+i*18));
this.addSlotToContainer(new Slot(this.table, j+i*2, 35+j*18, 7+i*18){
@Override
public void onSlotChanged(){
if(this.inventory instanceof TileEntityToolTable){
TileEntityToolTable table = (TileEntityToolTable)this.inventory;
ItemStack stack = ToolTableHandler.getResultFromSlots(table.slots);
table.slots[TileEntityToolTable.SLOT_OUTPUT] = stack == null ? null : stack.copy();
}
super.onSlotChanged();
}
});
}
}
this.addSlotToContainer(new SlotOutput(this.table, TileEntityToolTable.SLOT_OUTPUT, 115, 25){
@Override
public void onPickupFromSlot(EntityPlayer player, ItemStack stack){
if(this.inventory instanceof TileEntityToolTable){
TileEntityToolTable table = (TileEntityToolTable)this.inventory;
ToolTableHandler.Recipe recipe = ToolTableHandler.getRecipeFromSlots(table.slots);
for(int i = 0; i < TileEntityToolTable.INPUT_SLOT_AMOUNT; i++){
if(ItemUtil.contains(recipe.itemsNeeded, table.getStackInSlot(i))){
table.decrStackSize(i, 1);
}
}
ItemStack newOutput = ToolTableHandler.getResultFromSlots(table.slots);
table.slots[TileEntityToolTable.SLOT_OUTPUT] = newOutput == null ? null : newOutput.copy();
}
super.onPickupFromSlot(player, stack);
}
});
for(int i = 0; i < 3; i++){
for(int j = 0; j < 9; j++){
this.addSlotToContainer(new Slot(inventory, j+i*9+9, 8+j*18, 69+i*18));
@ -74,9 +101,8 @@ public class ContainerToolTable extends Container{
//Other Slots in Inventory excluded
else if(slot >= inventoryStart){
//Shift from Inventory
//TODO THIS
if(newStack.getItem() == InitItems.itemMisc && newStack.getItemDamage() == TheMiscItems.CUP.ordinal()){
if(!this.mergeItemStack(newStack, TileEntityCoffeeMachine.SLOT_INPUT, TileEntityCoffeeMachine.SLOT_INPUT+1, false)) return null;
if(ToolTableHandler.isIngredient(newStack)){
if(!this.mergeItemStack(newStack, 0, TileEntityToolTable.INPUT_SLOT_AMOUNT, false)) return null;
}
//

View file

@ -26,6 +26,7 @@ import ellpeck.actuallyadditions.event.TooltipEvent;
import ellpeck.actuallyadditions.tile.*;
import ellpeck.actuallyadditions.update.UpdateCheckerClientNotifier;
import ellpeck.actuallyadditions.util.*;
import net.minecraft.client.Minecraft;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.MinecraftForgeClient;
@ -39,7 +40,7 @@ public class ClientProxy implements IProxy{
public void preInit(FMLPreInitializationEvent event){
ModUtil.LOGGER.info("PreInitializing ClientProxy...");
PersistantVariables.setTheFile(new File(event.getModConfigurationDirectory().getParent(), ModUtil.MOD_ID+"Data.dat"));
PersistantVariables.setTheFile(new File(Minecraft.getMinecraft().mcDataDir, ModUtil.MOD_ID+"Data.dat"));
KeyBinds.init();
}

View file

@ -11,6 +11,7 @@
package ellpeck.actuallyadditions.recipe;
import ellpeck.actuallyadditions.items.InitItems;
import ellpeck.actuallyadditions.util.ItemUtil;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
@ -22,21 +23,47 @@ public class ToolTableHandler{
public static ArrayList<Recipe> recipes = new ArrayList<Recipe>();
public static void init(){
addRecipe(new ItemStack(Items.diamond_pickaxe), new ItemStack(InitItems.itemPhantomConnector), new ItemStack(Blocks.stone_brick_stairs), new ItemStack(Blocks.planks));
//TODO Actual real recipes
addRecipe(new ItemStack(InitItems.itemPhantomConnector), new ItemStack(Items.diamond_pickaxe), new ItemStack(Blocks.stone_brick_stairs), new ItemStack(Blocks.planks));
}
private static void addRecipe(ItemStack tool, ItemStack output, ItemStack... itemsNeeded){
recipes.add(new Recipe(tool, output, itemsNeeded));
private static void addRecipe(ItemStack output, ItemStack... itemsNeeded){
recipes.add(new Recipe(output, itemsNeeded));
}
public static ItemStack getResultFromSlots(ItemStack[] slots){
Recipe recipe = getRecipeFromSlots(slots);
return recipe == null ? null : recipe.output;
}
public static Recipe getRecipeFromSlots(ItemStack[] slots){
for(Recipe recipe : recipes){
if(ItemUtil.containsAll(slots, recipe.itemsNeeded)){
return recipe;
}
}
return null;
}
public static boolean isIngredient(ItemStack stack){
for(Recipe recipe : recipes){
if(stack != null){
for(ItemStack aStack : recipe.itemsNeeded){
if(aStack != null && stack.isItemEqual(aStack)){
return true;
}
}
}
}
return false;
}
public static class Recipe{
public ItemStack tool;
public ItemStack[] itemsNeeded;
public ItemStack output;
public Recipe(ItemStack tool, ItemStack output, ItemStack... itemsNeeded){
this.tool = tool;
public Recipe(ItemStack output, ItemStack... itemsNeeded){
this.output = output;
this.itemsNeeded = itemsNeeded;
}

View file

@ -15,14 +15,15 @@ import net.minecraft.item.ItemStack;
public class TileEntityToolTable extends TileEntityInventoryBase{
public static final int SLOT_OUTPUT = 6;
public static final int INPUT_SLOT_AMOUNT = 6;
public TileEntityToolTable(){
super(7, "toolTable");
}
@Override
public void updateEntity(){
public boolean canUpdate(){
return false;
}
@Override

View file

@ -13,6 +13,7 @@ package ellpeck.actuallyadditions.util;
import cpw.mods.fml.common.registry.GameRegistry;
import ellpeck.actuallyadditions.creative.CreativeTab;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class ItemUtil{
@ -43,4 +44,27 @@ public class ItemUtil{
return ModUtil.MOD_ID_LOWER+"."+((INameableItem)item).getName();
}
/**
* Returns true if stacksOne contains every ItemStack in stackTwo
*/
public static boolean containsAll(ItemStack[] stacksOne, ItemStack[] stacksTwo){
for(ItemStack stackTwo : stacksTwo){
if(!contains(stacksOne, stackTwo)){
return false;
}
}
return true;
}
/**
* Returns true if array contains stack or if both contain null
*/
public static boolean contains(ItemStack[] array, ItemStack stack){
for(ItemStack aStack : array){
if((stack == null && aStack == null) || (stack != null && aStack != null && aStack.isItemEqual(stack))){
return true;
}
}
return false;
}
}