Skip to content

Commit

Permalink
more event refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash (Bagel) committed Jul 11, 2024
1 parent 372a1aa commit fbebc7e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
public interface LivingHurtEvent {
Event<LivingHurtEvent> EVENT = EventFactory.createArrayBacked(LivingHurtEvent.class, callbacks -> (source, damaged, amount) -> {
for (LivingHurtEvent callback : callbacks) {
float newAmount = callback.onHurt(source, damaged, amount);
float newAmount = callback.onLivingHurt(source, damaged, amount);
if (newAmount != amount) return newAmount;
}
return amount;
});

float onHurt(DamageSource source, LivingEntity damaged, float amount);
float onLivingHurt(DamageSource source, LivingEntity damaged, float amount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public class AttributesGui implements Renderable, GuiEventListener, NarratableEn
protected static float scrollOffset = 0;
// Ditto.
protected static boolean hideUnchanged = false;
protected static boolean swappedFromTrinkets = false;

protected final InventoryScreen parent;
protected final Player player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CombatRulesMixin {
*/
@Inject(method ="getDamageAfterMagicAbsorb", at = @At("HEAD"), cancellable = true)
private static void zenith_attributes$getDamageAfterMagicAbsorb(float damage, float protPoints, CallbackInfoReturnable<Float> cir) {
AttributesLib.LOGGER.warn("Overriding getDamageAfterMagicAbsorb!");
cir.setReturnValue(damage * ALCombatRules.getProtDamageReduction(protPoints));
}

Expand All @@ -27,6 +28,7 @@ public class CombatRulesMixin {
@Inject(method ="getDamageAfterAbsorb", at = @At("HEAD"), cancellable = true)
private static void zenith_attributes$getDamageAfterAbsorb(float damage, float totalArmor, float toughnessAttribute, CallbackInfoReturnable<Float> cir) {
AttributesLib.LOGGER.trace("Invocation of CombatRules#getDamageAfterAbsorb is bypassing armor pen.");
AttributesLib.LOGGER.warn("Overriding getDamageAfterAbsorb!");
cir.setReturnValue(damage * ALCombatRules.getArmorDamageReduction(damage, totalArmor));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.shadowsoffire.attributeslib.mixin;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import de.dafuqs.additionalentityattributes.AdditionalEntityAttributes;
import dev.shadowsoffire.attributeslib.api.ALCombatRules;
import dev.shadowsoffire.attributeslib.api.ALObjects;
Expand All @@ -22,6 +21,7 @@
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
Expand Down Expand Up @@ -80,31 +80,31 @@ public LivingEntityMixin(EntityType<?> pEntityType, Level pLevel) {
return inst == null ? -1 : inst.getAmplifier();
}

@ModifyExpressionValue(at = @At(value = "INVOKE", target = "Lnet/minecraft/world/damagesource/CombatRules;getDamageAfterAbsorb(FFF)F"), method = "getDamageAfterArmorAbsorb", require = 1)
public float zenith_attributes$applyArmorPen(float amount, DamageSource src) {
return ALCombatRules.getDamageAfterArmor((LivingEntity) (Object) this, src, amount,((LivingEntity)(Object) this).getArmorValue(), (float) ((LivingEntity)(Object) this).getAttributeValue(Attributes.ARMOR_TOUGHNESS));
@Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/world/damagesource/CombatRules;getDamageAfterAbsorb(FFF)F"), method = "getDamageAfterArmorAbsorb", require = 1)
public float zenith_attributes$applyArmorPenplyArmorPen(float amount, float armor, float toughness, DamageSource src, float amt2) {
return ALCombatRules.getDamageAfterArmor(ths(), src, amount,(ths()).getArmorValue(), (float) (ths()).getAttributeValue(Attributes.ARMOR_TOUGHNESS));
}

@ModifyExpressionValue(at = @At(value = "INVOKE", target = "Lnet/minecraft/world/damagesource/CombatRules;getDamageAfterMagicAbsorb(FF)F"), method = "getDamageAfterMagicAbsorb", require = 1)
public float zenith_attributes$applyProtPen(float amount, DamageSource src) {
return ALCombatRules.getDamageAfterProtection((LivingEntity) (Object) this, src, amount, EnchantmentHelper.getDamageProtection(((LivingEntity)(Object) this).getArmorSlots(), src));
@Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/world/damagesource/CombatRules;getDamageAfterMagicAbsorb(FF)F"), method = "getDamageAfterMagicAbsorb", require = 1)
public float zenith_attributes$applyProtPen(float amount, float protPoints, DamageSource src, float amt2) {
return ALCombatRules.getDamageAfterProtection(ths(), src, amount, EnchantmentHelper.getDamageProtection((ths()).getArmorSlots(), src));
}

@ModifyVariable(method = "heal", at = @At(value = "HEAD"), argsOnly = true)
private float zenith_attributes$healEvent(float value){
float amount = LivingHealEvent.EVENT.invoker().onLivingHeal((LivingEntity) (Object) this, value);
private float zenith_attributes$onHealEvent(float value){
float amount = LivingHealEvent.EVENT.invoker().onLivingHeal(ths(), value);
return amount >= 0 ? amount : 0;
}

@Inject(method = "updateUsingItem", at = @At("HEAD"))
private void zenith_attributes$useItemEvent(ItemStack usingItem, CallbackInfo ci) {
private void zenith_attributes$modifyDrawSpeed(ItemStack usingItem, CallbackInfo ci) {
if (!usingItem.isEmpty())
this.useItemRemaining = AttributeEvents.drawSpeed((LivingEntity) (Object) this, usingItem, this.useItemRemaining);
this.useItemRemaining = AttributeEvents.drawSpeed(ths(), usingItem, this.useItemRemaining);
}

@ModifyVariable(method = "hurt", at = @At("HEAD"), argsOnly = true)
private float zenith_attributes$onHurtEvent(float amount, DamageSource source, float amount2) {
return LivingHurtEvent.EVENT.invoker().onHurt(source, (LivingEntity) (Object) this, amount);
return LivingHurtEvent.EVENT.invoker().onLivingHurt(source, ths(), amount);
}

@Inject(method = "createLivingAttributes", at = @At("RETURN"))
Expand All @@ -131,4 +131,8 @@ public LivingEntityMixin(EntityType<?> pEntityType, Level pLevel) {

}

@Unique
private LivingEntity ths() {
return (LivingEntity) (Object) this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public class PlayerMixin {

@ModifyVariable(method = "hurt", at = @At("HEAD"), argsOnly = true)
private float zenith_attributes$onHurt(float amount, DamageSource source, float amount2) {
return LivingHurtEvent.EVENT.invoker().onHurt(source, (LivingEntity) (Object) this, amount);
return LivingHurtEvent.EVENT.invoker().onLivingHurt(source, (LivingEntity) (Object) this, amount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ public String getRefMapperConfig() {
return null;
}

//lightly stolen from spectrum
@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
if (mixinClassName.equals("dev.shadowsoffire.attributeslib.mixin.compat.artifacts.present.ArtifactAttributeModifierMixin") && !FabricLoader.getInstance().isModLoaded("artifacts")) {
return false;
String COMPAT_PACKAGE_ROOT = Plugin.class.getPackageName(); // Shorthand getting the plugin package to ensure not making trouble with other mixins
String COMPAT_PRESENT_KEY = "present";
FabricLoader LOADER = FabricLoader.getInstance();

if (!mixinClassName.startsWith(COMPAT_PACKAGE_ROOT)) {
return true; // We do not meddle with the others' work
}
String[] compatRoot = COMPAT_PACKAGE_ROOT.split("\\.");
String[] mixinPath = mixinClassName.split("\\.");
// The id of the mod the mixin depends on
String compatModId = mixinPath[compatRoot.length];
if (compatModId.equals("other")) {
return true;
}
boolean isREICompat = mixinClassName.equals("dev.shadowsoffire.attributeslib.mixin.compat.roughlyenoughitems.present.DefaultPotionEffectExclusionZonesMixin") || mixinClassName.equals("dev.shadowsoffire.attributeslib.mixin.compat.roughlyenoughitems.present.EffectRenderingInventoryScreenMixinREI");
if (!FabricLoader.getInstance().isModLoaded("roughlyenoughitems") && isREICompat) {
return false;
// Whether the mixin is for when the mod is loaded or not
boolean isPresentMixin = mixinPath[compatRoot.length + 1].equals(COMPAT_PRESENT_KEY);

if (isPresentMixin) {
// We only load the mixin if the mod we want to be present is found
return LOADER.isModLoaded(compatModId);
}
return true;
return !LOADER.isModLoaded(compatModId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dev.shadowsoffire.attributeslib.mixin.compat;
package dev.shadowsoffire.attributeslib.mixin.compat.other;

import io.github.fabricators_of_create.porting_lib.extensions.extensions.BlockStateExtensions;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(BlockState.class)
public class BlockStateExtensionMixin implements BlockStateExtensions {
public class BlockStateExtensionMixin implements BlockStateExtensions { //Implemented here as it isn't in porting lib
}
4 changes: 2 additions & 2 deletions src/main/resources/zenith_attributes.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"PotionUtilsMixin",
"ProjectileMixin",
"accessors.AttributeSupplierBuilderAccessor",
"compat.BlockStateExtensionMixin",
"compat.artifacts.present.ArtifactAttributeModifierMixin"
"compat.artifacts.present.ArtifactAttributeModifierMixin",
"compat.other.BlockStateExtensionMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit fbebc7e

Please sign in to comment.