diff --git a/src/main/java/forestry/core/network/PacketIdClient.java b/src/main/java/forestry/core/network/PacketIdClient.java index 1424d9304..5d4a5c159 100644 --- a/src/main/java/forestry/core/network/PacketIdClient.java +++ b/src/main/java/forestry/core/network/PacketIdClient.java @@ -14,6 +14,7 @@ import javax.annotation.Nonnull; import forestry.core.network.packets.PacketDummyClient; +/** Packets sent to the client from the server */ public enum PacketIdClient implements IPacketId { INVALID, diff --git a/src/main/java/forestry/core/network/PacketIdServer.java b/src/main/java/forestry/core/network/PacketIdServer.java index c4c19be8f..1f39e3f34 100644 --- a/src/main/java/forestry/core/network/PacketIdServer.java +++ b/src/main/java/forestry/core/network/PacketIdServer.java @@ -14,6 +14,7 @@ import javax.annotation.Nonnull; import forestry.core.network.packets.PacketDummyServer; +/** Packets sent to the server from the client */ public enum PacketIdServer implements IPacketId { INVALID, @@ -25,6 +26,9 @@ public enum PacketIdServer implements IPacketId { CHIPSET_CLICK, SOLDERING_IRON_CLICK, + // JEI + WORKTABLE_RECIPE_REQUEST, + // Apiculture BEE_LOGIC_ACTIVE_ENTITY_REQUEST, diff --git a/src/main/java/forestry/core/recipes/RecipeUtil.java b/src/main/java/forestry/core/recipes/RecipeUtil.java index 49d0d30b7..1a286cd9f 100644 --- a/src/main/java/forestry/core/recipes/RecipeUtil.java +++ b/src/main/java/forestry/core/recipes/RecipeUtil.java @@ -10,6 +10,7 @@ ******************************************************************************/ package forestry.core.recipes; +import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -146,6 +147,7 @@ public abstract class RecipeUtil { return crafting; } + @Nonnull public static List findMatchingRecipes(InventoryCrafting inventory, World world) { ItemStack repairRecipe = findRepairRecipe(inventory); if (repairRecipe != null) { diff --git a/src/main/java/forestry/factory/gui/ContainerWorktable.java b/src/main/java/forestry/factory/gui/ContainerWorktable.java index a15e63d1b..1d326d777 100755 --- a/src/main/java/forestry/factory/gui/ContainerWorktable.java +++ b/src/main/java/forestry/factory/gui/ContainerWorktable.java @@ -28,7 +28,9 @@ import forestry.factory.inventory.InventoryCraftingForestry; import forestry.factory.inventory.InventoryGhostCrafting; import forestry.factory.inventory.InventoryWorktable; import forestry.factory.network.packets.PacketWorktableMemoryUpdate; +import forestry.factory.network.packets.PacketWorktableRecipeRequest; import forestry.factory.network.packets.PacketWorktableRecipeUpdate; +import forestry.factory.recipes.MemorizedRecipe; import forestry.factory.recipes.RecipeMemory; import forestry.factory.tiles.TileWorktable; @@ -81,7 +83,7 @@ public class ContainerWorktable extends ContainerTile implements } } - private void updateCraftMatrix() { + public void updateCraftMatrix() { IInventory crafting = tile.getCraftingDisplay(); for (int i = 0; i < crafting.getSizeInventory(); i++) { onCraftMatrixChanged(crafting, i); @@ -155,4 +157,8 @@ public class ContainerWorktable extends ContainerTile implements } } } + + public void sendWorktableRecipeRequest(MemorizedRecipe recipe) { + Proxies.net.sendToServer(new PacketWorktableRecipeRequest(tile, recipe)); + } } diff --git a/src/main/java/forestry/factory/network/PacketRegistryFactory.java b/src/main/java/forestry/factory/network/PacketRegistryFactory.java index ec0e0e46f..1e227d121 100644 --- a/src/main/java/forestry/factory/network/PacketRegistryFactory.java +++ b/src/main/java/forestry/factory/network/PacketRegistryFactory.java @@ -12,6 +12,7 @@ package forestry.factory.network; import forestry.core.network.PacketRegistry; import forestry.factory.network.packets.PacketWorktableMemoryUpdate; +import forestry.factory.network.packets.PacketWorktableRecipeRequest; import forestry.factory.network.packets.PacketWorktableRecipeUpdate; public class PacketRegistryFactory extends PacketRegistry { @@ -20,5 +21,7 @@ public class PacketRegistryFactory extends PacketRegistry { registerClientPacket(new PacketWorktableMemoryUpdate()); registerClientPacket(new PacketWorktableRecipeUpdate()); + + registerServerPacket(new PacketWorktableRecipeRequest()); } } diff --git a/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeRequest.java b/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeRequest.java new file mode 100644 index 000000000..d0bf3153c --- /dev/null +++ b/src/main/java/forestry/factory/network/packets/PacketWorktableRecipeRequest.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.factory.network.packets; + +import java.io.IOException; + +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.tileentity.TileEntity; + +import forestry.core.network.DataInputStreamForestry; +import forestry.core.network.DataOutputStreamForestry; +import forestry.core.network.IForestryPacketServer; +import forestry.core.network.PacketIdServer; +import forestry.core.network.packets.PacketCoordinates; +import forestry.core.proxy.Proxies; +import forestry.factory.gui.ContainerWorktable; +import forestry.factory.recipes.MemorizedRecipe; +import forestry.factory.tiles.TileWorktable; + +/** + * Used to sync the worktable crafting grid from Client to Server. + */ +public class PacketWorktableRecipeRequest extends PacketCoordinates implements IForestryPacketServer { + private MemorizedRecipe recipe; + + public PacketWorktableRecipeRequest() { + } + + public PacketWorktableRecipeRequest(TileWorktable worktable, MemorizedRecipe recipe) { + super(worktable); + this.recipe = recipe; + } + + @Override + protected void writeData(DataOutputStreamForestry data) throws IOException { + super.writeData(data); + data.writeStreamable(recipe); + } + + @Override + public void readData(DataInputStreamForestry data) throws IOException { + super.readData(data); + recipe = data.readStreamable(MemorizedRecipe.class); + } + + @Override + public void onPacketData(DataInputStreamForestry data, EntityPlayerMP player) throws IOException { + TileEntity tile = getTarget(player.worldObj); + if (tile instanceof TileWorktable) { + TileWorktable worktable = (TileWorktable) tile; + worktable.setCurrentRecipe(recipe); + + if (player.openContainer instanceof ContainerWorktable) { + ContainerWorktable containerWorktable = (ContainerWorktable) player.openContainer; + containerWorktable.updateCraftMatrix(); + } + + Proxies.net.sendNetworkPacket(new PacketWorktableRecipeUpdate(worktable), player.worldObj); + } + } + + @Override + public PacketIdServer getPacketId() { + return PacketIdServer.WORKTABLE_RECIPE_REQUEST; + } +} diff --git a/src/main/java/forestry/factory/recipes/jei/FactoryJeiPlugin.java b/src/main/java/forestry/factory/recipes/jei/FactoryJeiPlugin.java index 11ad2f4a9..b3fe6ccd0 100644 --- a/src/main/java/forestry/factory/recipes/jei/FactoryJeiPlugin.java +++ b/src/main/java/forestry/factory/recipes/jei/FactoryJeiPlugin.java @@ -6,8 +6,6 @@ import java.awt.Rectangle; import java.util.List; import forestry.core.gui.GuiForestry; -import forestry.core.recipes.jei.ForestryRecipeCategoryUid; -import forestry.factory.gui.ContainerWorktable; import forestry.factory.recipes.jei.bottler.BottlerRecipeCategory; import forestry.factory.recipes.jei.bottler.BottlerRecipeHandler; import forestry.factory.recipes.jei.bottler.BottlerRecipeMaker; @@ -44,7 +42,6 @@ import mezz.jei.api.IJeiHelpers; import mezz.jei.api.IModRegistry; import mezz.jei.api.JEIPlugin; import mezz.jei.api.gui.IAdvancedGuiHandler; -import mezz.jei.api.recipe.VanillaRecipeCategoryUid; import mezz.jei.api.recipe.transfer.IRecipeTransferRegistry; @JEIPlugin @@ -54,7 +51,8 @@ public class FactoryJeiPlugin extends BlankModPlugin { IJeiHelpers jeiHelpers = registry.getJeiHelpers(); IGuiHelper guiHelper = jeiHelpers.getGuiHelper(); - registry.addRecipeCategories(new BottlerRecipeCategory(guiHelper), + registry.addRecipeCategories( + new BottlerRecipeCategory(guiHelper), new CarpenterRecipeCategory(guiHelper), new CentrifugeRecipeCategory(guiHelper), new FabricatorRecipeCategory(guiHelper), @@ -62,18 +60,21 @@ public class FactoryJeiPlugin extends BlankModPlugin { new MoistenerRecipeCategory(guiHelper), new RainmakerRecipeCategory(guiHelper), new SqueezerRecipeCategory(guiHelper), - new StillRecipeCategory(guiHelper)); + new StillRecipeCategory(guiHelper) + ); - registry.addRecipeHandlers(new BottlerRecipeHandler(), + registry.addRecipeHandlers( + new BottlerRecipeHandler(), new CarpenterRecipeHandler(), new CentrifugeRecipeHandler(), new FabricatorRecipeHandler(), new FermenterRecipeHandler(), new MoistenerRecipeHandler(), new RainmakerRecipeHandler(), - new SqueezerRecipeHandler<>(SqueezerRecipeWrapper.class, ForestryRecipeCategoryUid.SQUEEZER), - new SqueezerRecipeHandler<>(SqueezerContainerRecipeWrapper.class, ForestryRecipeCategoryUid.SQUEEZER), - new StillRecipeHandler()); + new SqueezerRecipeHandler<>(SqueezerRecipeWrapper.class), + new SqueezerRecipeHandler<>(SqueezerContainerRecipeWrapper.class), + new StillRecipeHandler() + ); registry.addRecipes(BottlerRecipeMaker.getBottlerRecipes()); registry.addRecipes(CarpenterRecipeMaker.getCarpenterRecipes()); @@ -87,7 +88,7 @@ public class FactoryJeiPlugin extends BlankModPlugin { registry.addRecipes(StillRecipeMaker.getStillRecipes()); IRecipeTransferRegistry transferRegistry = registry.getRecipeTransferRegistry(); - transferRegistry.addRecipeTransferHandler(ContainerWorktable.class, VanillaRecipeCategoryUid.CRAFTING, 19, 9, 1, 36); + transferRegistry.addRecipeTransferHandler(new WorktableRecipeTransferHandler()); registry.addAdvancedGuiHandlers(new ForestryAdvancedGuiHandler()); } diff --git a/src/main/java/forestry/factory/recipes/jei/WorktableRecipeTransferHandler.java b/src/main/java/forestry/factory/recipes/jei/WorktableRecipeTransferHandler.java new file mode 100644 index 000000000..ddbca540f --- /dev/null +++ b/src/main/java/forestry/factory/recipes/jei/WorktableRecipeTransferHandler.java @@ -0,0 +1,63 @@ +package forestry.factory.recipes.jei; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.item.ItemStack; + +import forestry.factory.gui.ContainerWorktable; +import forestry.factory.inventory.InventoryCraftingForestry; +import forestry.factory.recipes.MemorizedRecipe; + +import mezz.jei.api.gui.IRecipeLayout; +import mezz.jei.api.recipe.VanillaRecipeCategoryUid; +import mezz.jei.api.recipe.transfer.IRecipeTransferError; +import mezz.jei.api.recipe.transfer.IRecipeTransferHandler; +import mezz.jei.gui.ingredients.IGuiIngredient; + +class WorktableRecipeTransferHandler implements IRecipeTransferHandler { + @Override + public Class getContainerClass() { + return ContainerWorktable.class; + } + + @Override + public String getRecipeCategoryUid() { + return VanillaRecipeCategoryUid.CRAFTING; + } + + @Nullable + @Override + public IRecipeTransferError transferRecipe(@Nonnull Container container, @Nonnull IRecipeLayout recipeLayout, @Nonnull EntityPlayer player, boolean maxTransfer, boolean doTransfer) { + Map> guiIngredients = recipeLayout.getItemStacks().getGuiIngredients(); + + ContainerWorktable containerWorktable = (ContainerWorktable) container; + InventoryCraftingForestry inventory = new InventoryCraftingForestry(containerWorktable); + + List recipeOutputs = Collections.emptyList(); + for (Map.Entry> entry : guiIngredients.entrySet()) { + int recipeSlot = entry.getKey(); + List allIngredients = entry.getValue().getAllIngredients(); + if (!allIngredients.isEmpty()) { + if (recipeSlot == 0) { + recipeOutputs = allIngredients; + } else { + ItemStack firstIngredient = allIngredients.get(0); + inventory.setInventorySlotContents(recipeSlot - 1, firstIngredient); + } + } + } + + if (!recipeOutputs.isEmpty() && doTransfer) { + MemorizedRecipe recipe = new MemorizedRecipe(inventory, recipeOutputs); + containerWorktable.sendWorktableRecipeRequest(recipe); + } + + return null; + } +} diff --git a/src/main/java/forestry/factory/recipes/jei/squeezer/SqueezerRecipeHandler.java b/src/main/java/forestry/factory/recipes/jei/squeezer/SqueezerRecipeHandler.java index 01931cabf..6b817770c 100644 --- a/src/main/java/forestry/factory/recipes/jei/squeezer/SqueezerRecipeHandler.java +++ b/src/main/java/forestry/factory/recipes/jei/squeezer/SqueezerRecipeHandler.java @@ -3,6 +3,7 @@ package forestry.factory.recipes.jei.squeezer; import javax.annotation.Nonnull; import forestry.api.recipes.ISqueezerRecipe; +import forestry.core.recipes.jei.ForestryRecipeCategoryUid; import forestry.core.recipes.jei.ForestryRecipeWrapper; import forestry.factory.recipes.ISqueezerContainerRecipe; @@ -12,25 +13,22 @@ import mezz.jei.api.recipe.IRecipeWrapper; public class SqueezerRecipeHandler implements IRecipeHandler { @Nonnull - private final Class clazz; - @Nonnull - private final String UID; + private final Class recipeClass; - public SqueezerRecipeHandler(@Nonnull Class recipeClass, @Nonnull String UID) { - this.clazz = recipeClass; - this.UID = UID; + public SqueezerRecipeHandler(@Nonnull Class recipeClass) { + this.recipeClass = recipeClass; } @Nonnull @Override public Class getRecipeClass() { - return clazz; + return recipeClass; } @Nonnull @Override public String getRecipeCategoryUid() { - return UID; + return ForestryRecipeCategoryUid.SQUEEZER; } @Nonnull diff --git a/src/main/java/forestry/factory/tiles/TileWorktable.java b/src/main/java/forestry/factory/tiles/TileWorktable.java index 0d0c7b6b5..1e2dc62b7 100644 --- a/src/main/java/forestry/factory/tiles/TileWorktable.java +++ b/src/main/java/forestry/factory/tiles/TileWorktable.java @@ -237,7 +237,7 @@ public class TileWorktable extends TileBase implements ICrafterWorktable { public void setCurrentRecipe(InventoryCraftingForestry crafting) { List recipeOutputs = RecipeUtil.findMatchingRecipes(crafting, worldObj); - MemorizedRecipe recipe = recipeOutputs.size() == 0 ? null : new MemorizedRecipe(crafting, recipeOutputs); + MemorizedRecipe recipe = recipeOutputs.isEmpty() ? null : new MemorizedRecipe(crafting, recipeOutputs); if (currentRecipe != null && recipe != null) { if (recipe.hasRecipeOutput(currentRecipe.getRecipeOutput())) {