From 4b546af6421ddea940ff30228860e0562637abc7 Mon Sep 17 00:00:00 2001 From: WinDanesz <31292708+WinDanesz@users.noreply.github.com> Date: Mon, 7 Feb 2022 21:50:26 +0100 Subject: [PATCH] Added a new ImbuementActivateEvent to allow addons hooking into the imbuement process --- .../event/ImbuementActivateEvent.java | 56 +++++++++++++++++++ .../tileentity/TileEntityImbuementAltar.java | 10 ++++ 2 files changed, 66 insertions(+) create mode 100644 src/main/java/electroblob/wizardry/event/ImbuementActivateEvent.java diff --git a/src/main/java/electroblob/wizardry/event/ImbuementActivateEvent.java b/src/main/java/electroblob/wizardry/event/ImbuementActivateEvent.java new file mode 100644 index 00000000..438aa7c9 --- /dev/null +++ b/src/main/java/electroblob/wizardry/event/ImbuementActivateEvent.java @@ -0,0 +1,56 @@ +package electroblob.wizardry.event; + +import electroblob.wizardry.constants.Element; +import electroblob.wizardry.tileentity.TileEntityImbuementAltar; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.eventhandler.Cancelable; +import net.minecraftforge.fml.common.eventhandler.Event; + +/** + * ImbuementActivateEvent is fired when {@link TileEntityImbuementAltar#getImbuementResult(net.minecraft.item.ItemStack, electroblob.wizardry.constants.Element[], + * boolean, net.minecraft.world.World, net.minecraft.entity.player.EntityPlayer)} is called to allow adding in imbuement "recipes" dynamically, based on the + * contents of the Imbuement altar's receptacles and the item placed on the altar. + * + * Note that this event is only fired on the server side.
+ *
+ * To alter the result of the imbuement process, change the {@link electroblob.wizardry.event.ImbuementActivateEvent#result} result of the process.
+ *
+ * This event is {@link Cancelable}. If this event is canceled, no further processing takes place.
+ *
+ * This event does not have a result. {@link Event.HasResult}
+ *
+ * This event is fired on the {@link MinecraftForge#EVENT_BUS}. + * + * @author WinDanesz + * @since Wizardry 4.3.5 + */ +@Cancelable +public class ImbuementActivateEvent extends Event { + + /** The item stack being imbued */ + ItemStack input; + + /** The elements of the four receptacles */ + Element[] receptacleElements; + + /** A reference to the current world object (may be null if {@code fullLootGen} is false) */ + World world; + + /** The player that last interacted with the imbuement altar, or null if there isn't one (or if this is being queried for other reasons, e.g. JEI) */ + EntityPlayer lastUser; + + /** The resulting item stack of the imbuement process */ + ItemStack result; + + public ImbuementActivateEvent(ItemStack input, Element[] receptacleElements, World world, EntityPlayer lastUser, ItemStack result) { + super(); + this.input = input; + this.receptacleElements = receptacleElements; + this.world = world; + this.lastUser = lastUser; + this.result = result; + } +} \ No newline at end of file diff --git a/src/main/java/electroblob/wizardry/tileentity/TileEntityImbuementAltar.java b/src/main/java/electroblob/wizardry/tileentity/TileEntityImbuementAltar.java index 39b79ceb..5c40c659 100644 --- a/src/main/java/electroblob/wizardry/tileentity/TileEntityImbuementAltar.java +++ b/src/main/java/electroblob/wizardry/tileentity/TileEntityImbuementAltar.java @@ -2,6 +2,7 @@ package electroblob.wizardry.tileentity; import electroblob.wizardry.block.BlockReceptacle; import electroblob.wizardry.constants.Element; +import electroblob.wizardry.event.ImbuementActivateEvent; import electroblob.wizardry.item.IManaStoringItem; import electroblob.wizardry.item.ItemWizardArmour; import electroblob.wizardry.registry.*; @@ -24,6 +25,7 @@ import net.minecraft.world.World; import net.minecraft.world.WorldServer; import net.minecraft.world.storage.loot.LootContext; import net.minecraft.world.storage.loot.LootTable; +import net.minecraftforge.common.MinecraftForge; import org.apache.commons.lang3.ArrayUtils; import javax.annotation.Nullable; @@ -233,6 +235,14 @@ public class TileEntityImbuementAltar extends TileEntity implements ITickable { */ public static ItemStack getImbuementResult(ItemStack input, Element[] receptacleElements, boolean fullLootGen, World world, EntityPlayer lastUser){ + ItemStack eventResult = ItemStack.EMPTY; + + if (world != null && MinecraftForge.EVENT_BUS.post(new ImbuementActivateEvent(input, receptacleElements, world, lastUser, eventResult))) { + // return the stack if something changes the result from an empty stack. + //noinspection ConstantConditions + if (eventResult != ItemStack.EMPTY) return eventResult; + } + if(input.getItem() instanceof ItemWizardArmour && ((ItemWizardArmour)input.getItem()).element == null){ if(Arrays.stream(receptacleElements).distinct().count() == 1 && receptacleElements[0] != null){ // All the same element