diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/event/LivingEquipmentChangeEvent.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/event/LivingEquipmentChangeEvent.java new file mode 100644 index 00000000..4a9f9ea7 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/event/LivingEquipmentChangeEvent.java @@ -0,0 +1,68 @@ +package com.gtnewhorizon.gtnhlib.client.event; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.item.ItemStack; +import net.minecraftforge.event.entity.living.LivingEvent; + +public class LivingEquipmentChangeEvent extends LivingEvent { + + /* + * Backported from github.com/MinecraftForge/MinecraftForge/pull/3411 + */ + private final int slot; + private final ItemStack from; + private final ItemStack to; + + /** + * {@link LivingEquipmentChangeEvent} is fired when the Equipment of an Entity changes.
+ * This event is fired whenever changes in Equipment are detected in {@link EntityLivingBase#onUpdate()}.
+ * This also includes entities joining the World, as well as being cloned.
+ * This event is fired on server-side only.
+ *
+ * {@link #slot} contains the index of the affected inventory slot. + * + * {@link #from} contains the {@link ItemStack} that was equipped previously.
+ * {@link #to} contains the {@link ItemStack} that is equipped now.
+ *
+ * This event is not {@link cpw.mods.fml.common.eventhandler.Cancelable}.
+ *
+ * This event does not have a result. {@link HasResult}
+ *
+ * This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}. + **/ + + public LivingEquipmentChangeEvent(EntityLivingBase entity, int slot, ItemStack from, ItemStack to) { + super(entity); + this.slot = slot; + this.from = from; + this.to = to; + } + + /** + * @return index of the affected inventory slot. + * + */ + public int getSlot() { + return this.slot; + } + + public ItemStack getFrom() { + return this.from; + } + + public ItemStack getTo() { + return this.to; + } +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java index 869c0f8f..f4703376 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java @@ -24,6 +24,8 @@ public enum Mixins { .setPhase(Phase.EARLY).addMixinClasses("fml.EventBusAccessor", "fml.EnumHolderAccessor")), TOOLTIP_RENDER(new Builder("TooltipRenderer").addMixinClasses("MixinGuiScreen").addTargetedMod(TargetedMod.VANILLA) .setApplyIf(() -> true).setPhase(Phase.EARLY).setSide(Side.CLIENT)), + EQUIPMENT_CHANGE_EVENT(new Builder("Add equipment change Forge events").addTargetedMod(TargetedMod.VANILLA) + .setSide(Side.BOTH).setPhase(Phase.EARLY).addMixinClasses("MixinEntityLivingBase").setApplyIf(() -> true)), DEBUG_TEXTURES(new Builder("Dump textures sizes").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT) .setPhase(Phase.EARLY) .setApplyIf(() -> Boolean.parseBoolean(System.getProperty("gtnhlib.debugtextures", "false"))) diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinEntityLivingBase.java b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinEntityLivingBase.java new file mode 100644 index 00000000..41f88b75 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/mixins/early/MixinEntityLivingBase.java @@ -0,0 +1,27 @@ +package com.gtnewhorizon.gtnhlib.mixins.early; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.item.ItemStack; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.gtnewhorizon.gtnhlib.client.event.LivingEquipmentChangeEvent; +import com.llamalad7.mixinextras.sugar.Local; + +@Mixin(EntityLivingBase.class) +public class MixinEntityLivingBase { + + @Inject( + method = "onUpdate", + at = @At( + value = "INVOKE_ASSIGN", + target = "Lnet/minecraft/world/WorldServer;getEntityTracker()Lnet/minecraft/entity/EntityTracker;")) + private void addEquipmentChangeEventBus(CallbackInfo ci, @Local(ordinal = 1) int slot, + @Local(ordinal = 0) ItemStack prevItem, @Local(ordinal = 1) ItemStack newItem) { + net.minecraftforge.common.MinecraftForge.EVENT_BUS + .post(new LivingEquipmentChangeEvent((EntityLivingBase) (Object) this, slot, prevItem, newItem)); + } +}