Skip to content
Permalink
Browse files

Fix #1142 JEI [+] button integration with worktable

  • Loading branch information
mezz committed Apr 28, 2016
1 parent 04bcdfd commit 6820d31b317d18165e705407c74c5e0b1e676617
@@ -14,6 +14,7 @@

import forestry.core.network.packets.PacketDummyClient;

/** Packets sent to the client from the server */
public enum PacketIdClient implements IPacketId {
INVALID,

@@ -14,6 +14,7 @@

import forestry.core.network.packets.PacketDummyServer;

/** Packets sent to the server from the client */
public enum PacketIdServer implements IPacketId {
INVALID,

@@ -25,6 +26,9 @@
CHIPSET_CLICK,
SOLDERING_IRON_CLICK,

// JEI
WORKTABLE_RECIPE_REQUEST,

// Apiculture
BEE_LOGIC_ACTIVE_ENTITY_REQUEST,

@@ -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 static InventoryCraftingForestry getCraftRecipe(ItemStack[] recipeItems,
return crafting;
}

@Nonnull
public static List<ItemStack> findMatchingRecipes(InventoryCrafting inventory, World world) {
ItemStack repairRecipe = findRepairRecipe(inventory);
if (repairRecipe != null) {
@@ -28,7 +28,9 @@
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 void detectAndSendChanges() {
}
}

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 void handleSelectionRequest(EntityPlayerMP player, PacketGuiSelectRequest
}
}
}

public void sendWorktableRecipeRequest(MemorizedRecipe recipe) {
Proxies.net.sendToServer(new PacketWorktableRecipeRequest(tile, recipe));
}
}
@@ -12,6 +12,7 @@

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 void registerPackets() {

registerClientPacket(new PacketWorktableMemoryUpdate());
registerClientPacket(new PacketWorktableRecipeUpdate());

registerServerPacket(new PacketWorktableRecipeRequest());
}
}
@@ -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;
}
}
@@ -6,8 +6,6 @@
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.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,26 +51,30 @@ public void register(@Nonnull IModRegistry registry) {
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),
new FermenterRecipeCategory(guiHelper),
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 void register(@Nonnull IModRegistry registry) {
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());
}
@@ -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<? extends Container> 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<Integer, ? extends IGuiIngredient<ItemStack>> guiIngredients = recipeLayout.getItemStacks().getGuiIngredients();

ContainerWorktable containerWorktable = (ContainerWorktable) container;
InventoryCraftingForestry inventory = new InventoryCraftingForestry(containerWorktable);

List<ItemStack> recipeOutputs = Collections.emptyList();
for (Map.Entry<Integer, ? extends IGuiIngredient<ItemStack>> entry : guiIngredients.entrySet()) {
int recipeSlot = entry.getKey();
List<ItemStack> 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;
}
}
@@ -3,6 +3,7 @@
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 @@
public class SqueezerRecipeHandler<R extends ForestryRecipeWrapper> implements IRecipeHandler<R> {

@Nonnull
private final Class<R> clazz;
@Nonnull
private final String UID;
private final Class<R> recipeClass;

public SqueezerRecipeHandler(@Nonnull Class<R> recipeClass, @Nonnull String UID) {
this.clazz = recipeClass;
this.UID = UID;
public SqueezerRecipeHandler(@Nonnull Class<R> recipeClass) {
this.recipeClass = recipeClass;
}

@Nonnull
@Override
public Class<R> getRecipeClass() {
return clazz;
return recipeClass;
}

@Nonnull
@Override
public String getRecipeCategoryUid() {
return UID;
return ForestryRecipeCategoryUid.SQUEEZER;
}

@Nonnull
@@ -237,7 +237,7 @@ public void clearCraftMatrix() {

public void setCurrentRecipe(InventoryCraftingForestry crafting) {
List<ItemStack> 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())) {

0 comments on commit 6820d31

Please sign in to comment.
You can’t perform that action at this time.