From a1222537910c6b0a84c567afb121dcdae62bb544 Mon Sep 17 00:00:00 2001 From: quat Date: Fri, 3 Aug 2018 00:59:55 -0400 Subject: [PATCH 1/6] Add an inventory wrap hook --- .../api/corporea/InventoryWrapEvent.java | 40 +++++++++++++++++++ .../core/handler/InternalMethodHandler.java | 11 +++-- 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java diff --git a/src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java b/src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java new file mode 100644 index 000000000..4ef3035c9 --- /dev/null +++ b/src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java @@ -0,0 +1,40 @@ +/** + * This class was created by . It's distributed as + * part of the Botania Mod. Get the Source Code in github: + * https://github.com/Vazkii/Botania + * + * Botania is Open Source and distributed under the + * Botania License: http://botaniamod.net/license.php + * + */ +package vazkii.botania.api.corporea; + +import net.minecraftforge.fml.common.eventhandler.Event; + +/** + * InventoryWrapEvent is fired when the corporea system attempts to wrap an inventory for use. + * */ +public class InventoryWrapEvent extends Event { + private IWrappedInventory wrapped = null; + final ICorporeaSpark spark; + + public InventoryWrapEvent(ICorporeaSpark spark) { + this.spark = spark; + } + + public IWrappedInventory getWrappedInventory() { + return wrapped; + } + + public void setWrappedInventory(IWrappedInventory wrapped) { + this.wrapped = wrapped; + } + + public ICorporeaSpark getSpark() { + return spark; + } + + public InvWithLocation getInvWithLocation() { + return spark.getSparkInventory(); + } +} diff --git a/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java b/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java index 079dc96b1..78708d060 100644 --- a/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java +++ b/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java @@ -27,6 +27,7 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.Optional; import net.minecraftforge.items.IItemHandlerModifiable; import vazkii.botania.api.BotaniaAPIClient; @@ -34,6 +35,7 @@ import vazkii.botania.api.corporea.CorporeaHelper; import vazkii.botania.api.corporea.ICorporeaSpark; import vazkii.botania.api.corporea.IWrappedInventory; import vazkii.botania.api.corporea.InvWithLocation; +import vazkii.botania.api.corporea.InventoryWrapEvent; import vazkii.botania.api.internal.DummyMethodHandler; import vazkii.botania.api.internal.IManaNetwork; import vazkii.botania.api.lexicon.LexiconPage; @@ -270,13 +272,14 @@ public class InternalMethodHandler extends DummyMethodHandler { List arrayList = new ArrayList(); for(InvWithLocation inv : inventories) { ICorporeaSpark spark = CorporeaHelper.getSparkForInventory(inv); - IWrappedInventory wrapped = null; - // try integrations - - // last chance - this will always work + + InventoryWrapEvent wrapEvent = new InventoryWrapEvent(spark); + MinecraftForge.EVENT_BUS.post(wrapEvent); + IWrappedInventory wrapped = wrapEvent.getWrappedInventory(); if(wrapped == null) { wrapped = WrappedIInventory.wrap(inv, spark); } + arrayList.add(wrapped); } return arrayList; From bbbfac82f3cf35a396b960e53d0b5915c7c74c1f Mon Sep 17 00:00:00 2001 From: quat Date: Fri, 3 Aug 2018 01:00:44 -0400 Subject: [PATCH 2/6] De-privatize WrappedIInventory for extension --- .../common/integration/corporea/WrappedIInventory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/vazkii/botania/common/integration/corporea/WrappedIInventory.java b/src/main/java/vazkii/botania/common/integration/corporea/WrappedIInventory.java index 5716b7151..8786ce981 100644 --- a/src/main/java/vazkii/botania/common/integration/corporea/WrappedIInventory.java +++ b/src/main/java/vazkii/botania/common/integration/corporea/WrappedIInventory.java @@ -18,11 +18,11 @@ import vazkii.botania.api.corporea.InvWithLocation; import java.util.ArrayList; import java.util.List; -public class WrappedIInventory extends WrappedInventoryBase{ +public class WrappedIInventory extends WrappedInventoryBase { private final InvWithLocation inv; - private WrappedIInventory(InvWithLocation inv, ICorporeaSpark spark) { + protected WrappedIInventory(InvWithLocation inv, ICorporeaSpark spark) { super(spark); this.inv = inv; } From 0eaab9a52cc3967411a353089c5aacb36a692ef2 Mon Sep 17 00:00:00 2001 From: quat Date: Fri, 3 Aug 2018 01:25:06 -0400 Subject: [PATCH 3/6] Add cancellable CorporeaIndexRequestEvent --- .../corporea/CorporeaIndexRequestEvent.java | 33 +++++++++++++++++++ .../tile/corporea/TileCorporeaIndex.java | 15 ++++++--- 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/main/java/vazkii/botania/api/corporea/CorporeaIndexRequestEvent.java diff --git a/src/main/java/vazkii/botania/api/corporea/CorporeaIndexRequestEvent.java b/src/main/java/vazkii/botania/api/corporea/CorporeaIndexRequestEvent.java new file mode 100644 index 000000000..1630333d6 --- /dev/null +++ b/src/main/java/vazkii/botania/api/corporea/CorporeaIndexRequestEvent.java @@ -0,0 +1,33 @@ +/** + * This class was created by . It's distributed as + * part of the Botania Mod. Get the Source Code in github: + * https://github.com/Vazkii/Botania + * + * Botania is Open Source and distributed under the + * Botania License: http://botaniamod.net/license.php + * + */ +package vazkii.botania.api.corporea; + +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraftforge.fml.common.eventhandler.Cancelable; +import net.minecraftforge.fml.common.eventhandler.Event; + +/** + * CorporeaIndexRequestEvent is fired when a player attempts to request an item from a corporea index. + * If the event is cancelled, the request will not be performed. + * */ +@Cancelable +public class CorporeaIndexRequestEvent extends Event { + public final EntityPlayerMP requester; + public String request; + public int requestCount; + public ICorporeaSpark indexSpark; + + public CorporeaIndexRequestEvent(EntityPlayerMP requester, String request, int requestCount, ICorporeaSpark indexSpark) { + this.requester = requester; + this.request = request; + this.requestCount = requestCount; + this.indexSpark = indexSpark; + } +} diff --git a/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java b/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java index a86a0d7ae..7324d5504 100644 --- a/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java +++ b/src/main/java/vazkii/botania/common/block/tile/corporea/TileCorporeaIndex.java @@ -20,6 +20,7 @@ import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.text.Style; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.util.text.TextFormatting; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.ServerChatEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -27,6 +28,7 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import org.apache.commons.lang3.text.WordUtils; import vazkii.botania.api.corporea.CorporeaHelper; +import vazkii.botania.api.corporea.CorporeaIndexRequestEvent; import vazkii.botania.api.corporea.ICorporeaAutoCompleteController; import vazkii.botania.api.corporea.ICorporeaRequestor; import vazkii.botania.api.corporea.ICorporeaSpark; @@ -269,11 +271,14 @@ public class TileCorporeaIndex extends TileCorporeaBase implements ICorporeaRequ if(!stack.isEmpty()) name = stack.getDisplayName().toLowerCase().trim(); } - - index.doCorporeaRequest(name, count, spark); - - event.getPlayer().sendMessage(new TextComponentTranslation("botaniamisc.requestMsg", count, WordUtils.capitalizeFully(name), CorporeaHelper.lastRequestMatches, CorporeaHelper.lastRequestExtractions).setStyle(new Style().setColor(TextFormatting.LIGHT_PURPLE))); - CorporeaRequestTrigger.INSTANCE.trigger(event.getPlayer(), event.getPlayer().getServerWorld(), index.getPos(), CorporeaHelper.lastRequestExtractions); + + CorporeaIndexRequestEvent indexReqEvent = new CorporeaIndexRequestEvent(event.getPlayer(), name, count, spark); + if(!MinecraftForge.EVENT_BUS.post(indexReqEvent)) { + index.doCorporeaRequest(name, count, spark); + + event.getPlayer().sendMessage(new TextComponentTranslation("botaniamisc.requestMsg", count, WordUtils.capitalizeFully(name), CorporeaHelper.lastRequestMatches, CorporeaHelper.lastRequestExtractions).setStyle(new Style().setColor(TextFormatting.LIGHT_PURPLE))); + CorporeaRequestTrigger.INSTANCE.trigger(event.getPlayer(), event.getPlayer().getServerWorld(), index.getPos(), CorporeaHelper.lastRequestExtractions); + } } } From 61e72f3cc3377387521caca3b5434cdd670545e9 Mon Sep 17 00:00:00 2001 From: quat Date: Fri, 3 Aug 2018 01:25:50 -0400 Subject: [PATCH 4/6] Meh just use a field --- .../botania/api/corporea/InventoryWrapEvent.java | 10 +--------- .../common/core/handler/InternalMethodHandler.java | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java b/src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java index 4ef3035c9..dba580786 100644 --- a/src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java +++ b/src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java @@ -15,21 +15,13 @@ import net.minecraftforge.fml.common.eventhandler.Event; * InventoryWrapEvent is fired when the corporea system attempts to wrap an inventory for use. * */ public class InventoryWrapEvent extends Event { - private IWrappedInventory wrapped = null; + public IWrappedInventory wrapped = null; final ICorporeaSpark spark; public InventoryWrapEvent(ICorporeaSpark spark) { this.spark = spark; } - public IWrappedInventory getWrappedInventory() { - return wrapped; - } - - public void setWrappedInventory(IWrappedInventory wrapped) { - this.wrapped = wrapped; - } - public ICorporeaSpark getSpark() { return spark; } diff --git a/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java b/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java index 78708d060..6eb4e56d7 100644 --- a/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java +++ b/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java @@ -275,7 +275,7 @@ public class InternalMethodHandler extends DummyMethodHandler { InventoryWrapEvent wrapEvent = new InventoryWrapEvent(spark); MinecraftForge.EVENT_BUS.post(wrapEvent); - IWrappedInventory wrapped = wrapEvent.getWrappedInventory(); + IWrappedInventory wrapped = wrapEvent.wrapped; if(wrapped == null) { wrapped = WrappedIInventory.wrap(inv, spark); } From fae9bcca7597ac19df790665f0fd81a9b22dd467 Mon Sep 17 00:00:00 2001 From: quat Date: Fri, 3 Aug 2018 12:58:00 -0400 Subject: [PATCH 5/6] Make wrappediinventory a bit more subclass friendly --- .../common/integration/corporea/WrappedIInventory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/vazkii/botania/common/integration/corporea/WrappedIInventory.java b/src/main/java/vazkii/botania/common/integration/corporea/WrappedIInventory.java index 8786ce981..5a1c96469 100644 --- a/src/main/java/vazkii/botania/common/integration/corporea/WrappedIInventory.java +++ b/src/main/java/vazkii/botania/common/integration/corporea/WrappedIInventory.java @@ -20,7 +20,7 @@ import java.util.List; public class WrappedIInventory extends WrappedInventoryBase { - private final InvWithLocation inv; + protected final InvWithLocation inv; protected WrappedIInventory(InvWithLocation inv, ICorporeaSpark spark) { super(spark); @@ -42,7 +42,7 @@ public class WrappedIInventory extends WrappedInventoryBase { return iterateOverSlots(request, true); } - private List iterateOverSlots(CorporeaRequest request, boolean doit) { + protected List iterateOverSlots(CorporeaRequest request, boolean doit) { List stacks = new ArrayList(); for (int i = inv.handler.getSlots() - 1; i >= 0; i--) { From 84967e4e2e34c24b547d5c167259157477ebf035 Mon Sep 17 00:00:00 2001 From: quat Date: Thu, 16 Aug 2018 00:44:38 -0400 Subject: [PATCH 6/6] Revert inventory wrap hook This reverts commit a1222537910c6b0a84c567afb121dcdae62bb544. --- .../api/corporea/InventoryWrapEvent.java | 32 ------------------- .../core/handler/InternalMethodHandler.java | 11 +++---- 2 files changed, 4 insertions(+), 39 deletions(-) delete mode 100644 src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java diff --git a/src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java b/src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java deleted file mode 100644 index dba580786..000000000 --- a/src/main/java/vazkii/botania/api/corporea/InventoryWrapEvent.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * This class was created by . It's distributed as - * part of the Botania Mod. Get the Source Code in github: - * https://github.com/Vazkii/Botania - * - * Botania is Open Source and distributed under the - * Botania License: http://botaniamod.net/license.php - * - */ -package vazkii.botania.api.corporea; - -import net.minecraftforge.fml.common.eventhandler.Event; - -/** - * InventoryWrapEvent is fired when the corporea system attempts to wrap an inventory for use. - * */ -public class InventoryWrapEvent extends Event { - public IWrappedInventory wrapped = null; - final ICorporeaSpark spark; - - public InventoryWrapEvent(ICorporeaSpark spark) { - this.spark = spark; - } - - public ICorporeaSpark getSpark() { - return spark; - } - - public InvWithLocation getInvWithLocation() { - return spark.getSparkInventory(); - } -} diff --git a/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java b/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java index 6eb4e56d7..079dc96b1 100644 --- a/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java +++ b/src/main/java/vazkii/botania/common/core/handler/InternalMethodHandler.java @@ -27,7 +27,6 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.Optional; import net.minecraftforge.items.IItemHandlerModifiable; import vazkii.botania.api.BotaniaAPIClient; @@ -35,7 +34,6 @@ import vazkii.botania.api.corporea.CorporeaHelper; import vazkii.botania.api.corporea.ICorporeaSpark; import vazkii.botania.api.corporea.IWrappedInventory; import vazkii.botania.api.corporea.InvWithLocation; -import vazkii.botania.api.corporea.InventoryWrapEvent; import vazkii.botania.api.internal.DummyMethodHandler; import vazkii.botania.api.internal.IManaNetwork; import vazkii.botania.api.lexicon.LexiconPage; @@ -272,14 +270,13 @@ public class InternalMethodHandler extends DummyMethodHandler { List arrayList = new ArrayList(); for(InvWithLocation inv : inventories) { ICorporeaSpark spark = CorporeaHelper.getSparkForInventory(inv); - - InventoryWrapEvent wrapEvent = new InventoryWrapEvent(spark); - MinecraftForge.EVENT_BUS.post(wrapEvent); - IWrappedInventory wrapped = wrapEvent.wrapped; + IWrappedInventory wrapped = null; + // try integrations + + // last chance - this will always work if(wrapped == null) { wrapped = WrappedIInventory.wrap(inv, spark); } - arrayList.add(wrapped); } return arrayList;