Add mixin to register LivingEquipmentChangeEvent (#117)

This commit is contained in:
Mary authored and GitHub committed 2025-03-05 01:29:26 -06:00
1 parent c70e7b9121
commit ef32d751fd
3 files changed
+97

No files matched your search

@@ -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. <br>
* This event is fired whenever changes in Equipment are detected in {@link EntityLivingBase#onUpdate()}. <br>
* This also includes entities joining the World, as well as being cloned. <br>
* This event is fired on server-side only. <br>
* <br>
* {@link #slot} contains the index of the affected inventory slot.
* <ul>
* <li>0: Equipped in main hand
* <li>1: Boots
* <li>2: Leggings
* <li>3: Chestplate
* <li>4: Helmet
* </ul>
* {@link #from} contains the {@link ItemStack} that was equipped previously. <br>
* {@link #to} contains the {@link ItemStack} that is equipped now. <br>
* <br>
* This event is not {@link cpw.mods.fml.common.eventhandler.Cancelable}. <br>
* <br>
* This event does not have a result. {@link HasResult} <br>
* <br>
* 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.
* <ul>
* <li>0: Equipped in main hand
* <li>1: Boots
* <li>2: Leggings
* <li>3: Chestplate
* <li>4: Helmet
* </ul>
*/
public int getSlot() {
return this.slot;
}
public ItemStack getFrom() {
return this.from;
}
public ItemStack getTo() {
return this.to;
}
}
@@ -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")))
@@ -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));
}
}