diff --git a/src/main/java/techreborn/client/gui/autocrafting/GuiAutoCrafting.java b/src/main/java/techreborn/client/gui/autocrafting/GuiAutoCrafting.java index e2a8a3103..f6660c2f7 100644 --- a/src/main/java/techreborn/client/gui/autocrafting/GuiAutoCrafting.java +++ b/src/main/java/techreborn/client/gui/autocrafting/GuiAutoCrafting.java @@ -24,7 +24,9 @@ import techreborn.packets.PacketSetRecipe; import techreborn.tiles.TileAutoCraftingTable; import java.io.IOException; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import static net.minecraft.item.ItemStack.EMPTY; @@ -75,6 +77,18 @@ public class GuiAutoCrafting extends GuiBase { this.builder.drawMultiEnergyBar(this, 9, 26, (int) this.tileAutoCraftingTable.getEnergy(), (int) this.tileAutoCraftingTable.getMaxPower(), mouseX, mouseY, 0, layer); this.builder.drawProgressBar(this, tileAutoCraftingTable.getProgress(), tileAutoCraftingTable.getMaxProgress(), 120, 44, mouseX, mouseY, TRBuilder.ProgressDirection.RIGHT, layer); + int mX = mouseX - getGuiLeft(); + int mY = mouseY - getGuiTop(); + + if(recipe != null && !tileAutoCraftingTable.customRecipe){ + if (builder.isInRect(91, 66, 23, 23, mX, mY)) { + List list = new ArrayList<>(); + list.add("Click to clear"); + net.minecraftforge.fml.client.config.GuiUtils.drawHoveringText(list, mX, mY, width, height, -1, mc.fontRenderer); + GlStateManager.disableLighting(); + GlStateManager.color(1, 1, 1, 1); + } + } } //Based of vanilla code @@ -174,6 +188,13 @@ public class GuiAutoCrafting extends GuiBase { if (!this.recipeSlector.func_191862_a(mouseX, mouseY, mouseButton)) { super.mouseClicked(mouseX, mouseY, mouseButton); } + int mX = mouseX - getGuiLeft(); + int mY = mouseY - getGuiTop(); + + mc.getTextureManager().bindTexture(TRBuilder.GUI_SHEET); + if (builder.isInRect(91, 66, 23, 23, mX, mY)) { + setRecipe(null, true); + } } @Override @@ -194,9 +215,9 @@ public class GuiAutoCrafting extends GuiBase { super.onGuiClosed(); } - public void setRecipe(IRecipe recipe) { - tileAutoCraftingTable.setCurrentRecipe(recipe.getRegistryName()); - NetworkManager.sendToServer(new PacketSetRecipe(tileAutoCraftingTable, recipe.getRegistryName())); + public void setRecipe(IRecipe recipe, boolean custom) { + tileAutoCraftingTable.setCurrentRecipe(recipe, custom); + NetworkManager.sendToServer(new PacketSetRecipe(tileAutoCraftingTable, recipe, custom)); } } diff --git a/src/main/java/techreborn/client/gui/autocrafting/GuiAutoCraftingRecipeSlector.java b/src/main/java/techreborn/client/gui/autocrafting/GuiAutoCraftingRecipeSlector.java index 7180b92a3..6e74b88f1 100644 --- a/src/main/java/techreborn/client/gui/autocrafting/GuiAutoCraftingRecipeSlector.java +++ b/src/main/java/techreborn/client/gui/autocrafting/GuiAutoCraftingRecipeSlector.java @@ -32,7 +32,7 @@ public class GuiAutoCraftingRecipeSlector extends GuiRecipeBook { @Override public void setContainerRecipe(IRecipe recipe, RecipeList recipes) { - guiAutoCrafting.setRecipe(recipe); + guiAutoCrafting.setRecipe(recipe, false); } public void setGuiAutoCrafting(GuiAutoCrafting guiAutoCrafting) { diff --git a/src/main/java/techreborn/packets/PacketSetRecipe.java b/src/main/java/techreborn/packets/PacketSetRecipe.java index 3cb352327..031ff09da 100644 --- a/src/main/java/techreborn/packets/PacketSetRecipe.java +++ b/src/main/java/techreborn/packets/PacketSetRecipe.java @@ -1,5 +1,6 @@ package techreborn.packets; +import net.minecraft.item.crafting.IRecipe; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; @@ -17,14 +18,21 @@ public class PacketSetRecipe implements INetworkPacket { BlockPos pos; ResourceLocation recipe; + boolean custom; - public PacketSetRecipe(TileAutoCraftingTable tile, ResourceLocation recipe) { + public PacketSetRecipe(TileAutoCraftingTable tile, IRecipe recipe, boolean custom) { this.pos = tile.getPos(); - this.recipe = recipe; + if(recipe == null){ + this.recipe = new ResourceLocation(""); + } else { + this.recipe = recipe.getRegistryName(); + } + if(this.recipe == null){ //TODO fix vanilla recipes this.recipe = new ResourceLocation(""); } + this.custom = custom; } public PacketSetRecipe() { @@ -34,19 +42,21 @@ public class PacketSetRecipe implements INetworkPacket { public void writeData(ExtendedPacketBuffer buffer) throws IOException { buffer.writeBlockPos(pos); buffer.writeResourceLocation(recipe); + buffer.writeBoolean(custom); } @Override public void readData(ExtendedPacketBuffer buffer) throws IOException { pos = buffer.readBlockPos(); recipe = buffer.readResourceLocation(); + custom = buffer.readBoolean(); } @Override public void processData(PacketSetRecipe message, MessageContext context) { - TileEntity tileEntity = context.getServerHandler().player.world.getTileEntity(pos);; + TileEntity tileEntity = context.getServerHandler().player.world.getTileEntity(message.pos);; if(tileEntity instanceof TileAutoCraftingTable){ - ((TileAutoCraftingTable) tileEntity).setCurrentRecipe(recipe); + ((TileAutoCraftingTable) tileEntity).setCurrentRecipe(message.recipe, message.custom); } } } diff --git a/src/main/java/techreborn/tiles/TileAutoCraftingTable.java b/src/main/java/techreborn/tiles/TileAutoCraftingTable.java index 4de2f5735..2c519c81b 100644 --- a/src/main/java/techreborn/tiles/TileAutoCraftingTable.java +++ b/src/main/java/techreborn/tiles/TileAutoCraftingTable.java @@ -1,9 +1,12 @@ package techreborn.tiles; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; +import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.CraftingManager; import net.minecraft.item.crafting.IRecipe; import net.minecraft.item.crafting.Ingredient; import net.minecraft.nbt.NBTTagCompound; @@ -35,13 +38,37 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain public int maxProgress = 120; public int euTick = 10; public Pair cachedRecipe; + public boolean customRecipe = true; + InventoryCrafting inventoryCrafting = null; + IRecipe lastCustomRecipe = null; - public void setCurrentRecipe(ResourceLocation recipe) { + public void setCurrentRecipe(IRecipe recipe, boolean customRecipe) { + if(recipe != null){ + currentRecipe = recipe.getRegistryName(); + } else { + currentRecipe = null; + } + + this.customRecipe = customRecipe; + cachedRecipe = null; + } + + public void setCurrentRecipe(ResourceLocation recipe, boolean customRecipe) { currentRecipe = recipe; + this.customRecipe = customRecipe; + cachedRecipe = null; } @Nullable public IRecipe getIRecipe() { + if(customRecipe){ + InventoryCrafting crafting = getCraftingInventory(); + for(IRecipe testRecipe : CraftingManager.REGISTRY){ + if(testRecipe.matches(crafting, world)){ + return testRecipe; + } + } + } if (currentRecipe == null) { return null; } @@ -57,6 +84,21 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain return cachedRecipe.getRight(); } + public InventoryCrafting getCraftingInventory(){ + if(inventoryCrafting == null){ + inventoryCrafting = new InventoryCrafting(new Container() { + @Override + public boolean canInteractWith(EntityPlayer playerIn) { + return false; + } + }, 3, 3); + } + for (int i = 0; i < 8; i++) { + inventoryCrafting.setInventorySlotContents(i, inventory.getStackInSlot(i)); + } + return inventoryCrafting; + } + @Override public void update() { super.update(); @@ -64,7 +106,7 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain return; } IRecipe recipe = getIRecipe(); - if (recipe != null) { + if (recipe != null || customRecipe) { if (progress >= maxProgress) { if (make(recipe)) { progress = 0; @@ -81,7 +123,10 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain } public boolean canMake(IRecipe recipe) { - if (recipe.canFit(3, 3)) { + if(customRecipe){ + recipe = getIRecipe(); + } + if (recipe != null && recipe.canFit(3, 3)) { boolean missingOutput = false; int[] stacksInSlots = new int[9]; for (int i = 0; i < 9; i++) { @@ -92,7 +137,8 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain boolean foundIngredient = false; for (int i = 0; i < 9; i++) { ItemStack stack = inventory.getStackInSlot(i); - if (stacksInSlots[i] > 0) { + int requiredSize = customRecipe ? 1 : 0; + if (stacksInSlots[i] > requiredSize) { if (ingredient.apply(stack)) { foundIngredient = true; stacksInSlots[i]--; @@ -130,6 +176,12 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain public boolean make(IRecipe recipe) { if (canMake(recipe)) { + if(recipe == null && customRecipe){ + if(lastCustomRecipe == null){ + return false; + }//Should be uptodate as we just set it in canMake + recipe = lastCustomRecipe; + } for (int i = 0; i < recipe.getIngredients().size(); i++) { Ingredient ingredient = recipe.getIngredients().get(i); //Looks for the best slot to take it from @@ -141,15 +193,19 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain ItemStack stack = inventory.getStackInSlot(j); if (ingredient.apply(stack)) { stack.shrink(1); //TODO is this right? or do I need to use it as an actull crafting grid + //TOOD check items to be retuned break; } } } } ItemStack output = inventory.getStackInSlot(9); + //TODO fire forge recipe event + ItemStack ouputStack = recipe.getCraftingResult(getCraftingInventory()); if (output.isEmpty()) { - inventory.setInventorySlotContents(9, recipe.getRecipeOutput().copy()); + inventory.setInventorySlotContents(9, ouputStack.copy()); } else { + //TODO use ouputStack in someway? output.grow(recipe.getRecipeOutput().getCount()); } return true; @@ -243,6 +299,7 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain if (currentRecipe != null) { tag.setString("currentRecipe", currentRecipe.toString()); } + tag.setBoolean("customRecipe", customRecipe); return super.writeToNBT(tag); } @@ -251,6 +308,7 @@ public class TileAutoCraftingTable extends TilePowerAcceptor implements IContain if (tag.hasKey("currentRecipe")) { currentRecipe = new ResourceLocation(tag.getString("currentRecipe")); } + customRecipe = tag.getBoolean("customRecipe"); super.readFromNBT(tag); }