From 7320ddb44f63bfd2f24295e081586af7a9a66c9c Mon Sep 17 00:00:00 2001 From: Glease <4586901+Glease@users.noreply.github.com> Date: Fri, 22 Jan 2021 22:24:00 +0800 Subject: [PATCH] Dirty fix for BQ3 crafting quest not triggering --- .../forestry/core/gui/ContainerForestry.java | 266 +++++++++--------- .../factory/gui/ContainerWorktable.java | 48 ++++ 2 files changed, 181 insertions(+), 133 deletions(-) diff --git a/src/main/java/forestry/core/gui/ContainerForestry.java b/src/main/java/forestry/core/gui/ContainerForestry.java index 76a5a79a3..6ae34f34f 100644 --- a/src/main/java/forestry/core/gui/ContainerForestry.java +++ b/src/main/java/forestry/core/gui/ContainerForestry.java @@ -1,133 +1,133 @@ -/******************************************************************************* - * 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.core.gui; - -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Multimaps; - -import java.util.List; -import java.util.Map; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.InventoryPlayer; -import net.minecraft.inventory.Container; -import net.minecraft.inventory.Slot; -import net.minecraft.item.ItemStack; - -import forestry.core.gui.slots.SlotFilteredInventory; -import forestry.core.gui.slots.SlotForestry; -import forestry.core.gui.slots.SlotLocked; -import forestry.core.network.IForestryPacketClient; -import forestry.core.proxy.Proxies; -import forestry.core.utils.SlotUtil; - -import invtweaks.api.container.ContainerSection; -import invtweaks.api.container.ContainerSectionCallback; - -@invtweaks.api.container.ChestContainer(showButtons = false) -public abstract class ContainerForestry extends Container { - protected final void addPlayerInventory(InventoryPlayer playerInventory, int xInv, int yInv) { - // Player inventory - for (int row = 0; row < 3; row++) { - for (int column = 0; column < 9; column++) { - addSlotToContainer(new Slot(playerInventory, column + row * 9 + 9, xInv + column * 18, yInv + row * 18)); - } - } - // Player hotbar - for (int column = 0; column < 9; column++) { - addHotbarSlot(playerInventory, column, xInv + column * 18, yInv + 58); - } - } - - protected void addHotbarSlot(InventoryPlayer playerInventory, int slot, int x, int y) { - addSlotToContainer(new Slot(playerInventory, slot, x, y)); - } - - @Override - public Slot addSlotToContainer(Slot p_75146_1_) { - return super.addSlotToContainer(p_75146_1_); - } - - @Override - public ItemStack slotClick(int slotIndex, int button, int modifier, EntityPlayer player) { - if (!canAccess(player)) { - return null; - } - - if (modifier == 2 && button >= 0 && button < 9) { - // hotkey used to move item from slot to hotbar - int hotbarSlotIndex = 27 + button; - Slot hotbarSlot = getSlot(hotbarSlotIndex); - if (hotbarSlot instanceof SlotLocked) { - return null; - } - } - - Slot slot = (slotIndex < 0) ? null : getSlot(slotIndex); - if (slot instanceof SlotForestry) { - SlotForestry slotForestry = (SlotForestry) slot; - if (slotForestry.isPhantom()) { - return SlotUtil.slotClickPhantom(slotForestry, button, modifier, player); - } - } - - return super.slotClick(slotIndex, button, modifier, player); - } - - @Override - public final ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) { - if (!canAccess(player)) { - return null; - } - - return SlotUtil.transferStackInSlot(inventorySlots, player, slotIndex); - } - - protected abstract boolean canAccess(EntityPlayer player); - - protected final void sendPacketToCrafters(IForestryPacketClient packet) { - for (Object crafter : crafters) { - if (crafter instanceof EntityPlayer) { - Proxies.net.sendToPlayer(packet, (EntityPlayer) crafter); - } - } - } - - @SuppressWarnings("unused") // inventory tweaks - @ContainerSectionCallback - public Map> getContainerSections() { - ArrayListMultimap map = ArrayListMultimap.create(); - - for (Object object : inventorySlots) { - if (!(object instanceof Slot)) { - continue; - } - Slot slot = (Slot) object; - - if (slot.inventory instanceof InventoryPlayer) { - map.put(ContainerSection.INVENTORY, slot); - if (slot.slotNumber < 9) { - map.put(ContainerSection.INVENTORY_HOTBAR, slot); - } else if (slot.slotNumber < 36) { - map.put(ContainerSection.INVENTORY_NOT_HOTBAR, slot); - } else { - map.put(ContainerSection.ARMOR, slot); - } - } else { - if (!(slot instanceof SlotForestry) || slot instanceof SlotFilteredInventory) { - map.put(ContainerSection.CHEST, slot); - } - } - } - - return Multimaps.asMap(map); - } -} +/******************************************************************************* + * 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.core.gui; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimaps; + +import java.util.List; +import java.util.Map; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; + +import forestry.core.gui.slots.SlotFilteredInventory; +import forestry.core.gui.slots.SlotForestry; +import forestry.core.gui.slots.SlotLocked; +import forestry.core.network.IForestryPacketClient; +import forestry.core.proxy.Proxies; +import forestry.core.utils.SlotUtil; + +import invtweaks.api.container.ContainerSection; +import invtweaks.api.container.ContainerSectionCallback; + +@invtweaks.api.container.ChestContainer(showButtons = false) +public abstract class ContainerForestry extends Container { + protected final void addPlayerInventory(InventoryPlayer playerInventory, int xInv, int yInv) { + // Player inventory + for (int row = 0; row < 3; row++) { + for (int column = 0; column < 9; column++) { + addSlotToContainer(new Slot(playerInventory, column + row * 9 + 9, xInv + column * 18, yInv + row * 18)); + } + } + // Player hotbar + for (int column = 0; column < 9; column++) { + addHotbarSlot(playerInventory, column, xInv + column * 18, yInv + 58); + } + } + + protected void addHotbarSlot(InventoryPlayer playerInventory, int slot, int x, int y) { + addSlotToContainer(new Slot(playerInventory, slot, x, y)); + } + + @Override + public Slot addSlotToContainer(Slot p_75146_1_) { + return super.addSlotToContainer(p_75146_1_); + } + + @Override + public ItemStack slotClick(int slotIndex, int button, int modifier, EntityPlayer player) { + if (!canAccess(player)) { + return null; + } + + if (modifier == 2 && button >= 0 && button < 9) { + // hotkey used to move item from slot to hotbar + int hotbarSlotIndex = 27 + button; + Slot hotbarSlot = getSlot(hotbarSlotIndex); + if (hotbarSlot instanceof SlotLocked) { + return null; + } + } + + Slot slot = (slotIndex < 0) ? null : getSlot(slotIndex); + if (slot instanceof SlotForestry) { + SlotForestry slotForestry = (SlotForestry) slot; + if (slotForestry.isPhantom()) { + return SlotUtil.slotClickPhantom(slotForestry, button, modifier, player); + } + } + + return super.slotClick(slotIndex, button, modifier, player); + } + + @Override + public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) { + if (!canAccess(player)) { + return null; + } + + return SlotUtil.transferStackInSlot(inventorySlots, player, slotIndex); + } + + protected abstract boolean canAccess(EntityPlayer player); + + protected final void sendPacketToCrafters(IForestryPacketClient packet) { + for (Object crafter : crafters) { + if (crafter instanceof EntityPlayer) { + Proxies.net.sendToPlayer(packet, (EntityPlayer) crafter); + } + } + } + + @SuppressWarnings("unused") // inventory tweaks + @ContainerSectionCallback + public Map> getContainerSections() { + ArrayListMultimap map = ArrayListMultimap.create(); + + for (Object object : inventorySlots) { + if (!(object instanceof Slot)) { + continue; + } + Slot slot = (Slot) object; + + if (slot.inventory instanceof InventoryPlayer) { + map.put(ContainerSection.INVENTORY, slot); + if (slot.slotNumber < 9) { + map.put(ContainerSection.INVENTORY_HOTBAR, slot); + } else if (slot.slotNumber < 36) { + map.put(ContainerSection.INVENTORY_NOT_HOTBAR, slot); + } else { + map.put(ContainerSection.ARMOR, slot); + } + } else { + if (!(slot instanceof SlotForestry) || slot instanceof SlotFilteredInventory) { + map.put(ContainerSection.CHEST, slot); + } + } + } + + return Multimaps.asMap(map); + } +} diff --git a/src/main/java/forestry/factory/gui/ContainerWorktable.java b/src/main/java/forestry/factory/gui/ContainerWorktable.java index a15e63d1b..cb458c9c5 100755 --- a/src/main/java/forestry/factory/gui/ContainerWorktable.java +++ b/src/main/java/forestry/factory/gui/ContainerWorktable.java @@ -10,6 +10,8 @@ ******************************************************************************/ package forestry.factory.gui; +import cpw.mods.fml.common.gameevent.PlayerEvent; +import forestry.core.utils.Log; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.IInventory; @@ -32,7 +34,39 @@ import forestry.factory.network.packets.PacketWorktableRecipeUpdate; import forestry.factory.recipes.RecipeMemory; import forestry.factory.tiles.TileWorktable; +import java.lang.reflect.Method; + public class ContainerWorktable extends ContainerTile implements IContainerCrafting, IGuiSelectable { + private static final Method craftingEventHandler; + private static final Object craftingEventHandlerInstance; + static { + Object instance; + Method method; + try { + final Class clazz = Class.forName("bq_standard.handlers.EventHandler"); + method = clazz.getMethod("onItemCrafted", PlayerEvent.ItemCraftedEvent.class); + method.setAccessible(true); + instance = clazz.getConstructor().newInstance(); + } catch (ReflectiveOperationException e) { + method = null; + instance = null; + } + if (instance != null) + Log.fine("BetterQuesting 3 Standard Expansion crafting quest compat enabled."); + craftingEventHandlerInstance = instance; + craftingEventHandler = method; + } + + private static void callBQCraftingHandler(PlayerEvent.ItemCraftedEvent event) { + if (craftingEventHandler != null) { + try { + craftingEventHandler.invoke(craftingEventHandlerInstance, event); + } catch (ReflectiveOperationException e) { + Log.logThrowable("Error calling BQ3 crafting event handler", e); + } + } + } + private final InventoryCraftingForestry craftMatrix = new InventoryCraftingForestry(this); private long lastMemoryUpdate; private boolean craftMatrixChanged = false; @@ -119,6 +153,20 @@ public class ContainerWorktable extends ContainerTile implements Proxies.net.sendToServer(new PacketGuiSelectRequest(mouseButton, recipeIndex)); } + @Override + public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) { + final ItemStack transferred = super.transferStackInSlot(player, slotIndex); + if (!player.worldObj.isRemote) { + final Object o = inventorySlots.get(slotIndex); + if (o instanceof SlotCrafter) { + // use the ghost crafting matrix instead of actual crafting matrix + // so BQ3 won't do a lookup again later on + callBQCraftingHandler(new PlayerEvent.ItemCraftedEvent(player, transferred, ((SlotCrafter) o).inventory)); + } + } + return transferred; + } + @Override public void handleSelectionRequest(EntityPlayerMP player, PacketGuiSelectRequest packet) { int primary = packet.getPrimaryIndex();