Skip to content

Commit

Permalink
Rework resistance calculations for better configurability
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldexun committed Mar 3, 2023
1 parent c48bfe3 commit f78c7e3
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 14 deletions.
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ dependencies {
minecraft {
mappings channel: "${mappings_channel}", version: "${mappings_version}"

// def atDepFileTree = files(configurations.atDependencies.collect { zipTree(it) }).getAsFileTree()
// accessTransformer = files(
// 'src/main/resources/META-INF/accesstransformer.cfg',
// atDepFileTree.matching { include 'META-INF/accesstransformer.cfg' }
// )
def atDepFileTree = files(configurations.atDependencies.collect { zipTree(it) }).getAsFileTree()
accessTransformer = files(
'src/main/resources/META-INF/accesstransformer.cfg',
atDepFileTree.matching { include 'META-INF/accesstransformer.cfg' }
)

runs {
client {
Expand Down Expand Up @@ -92,7 +92,7 @@ jar {
}

manifest {
// attributes 'FMLAT': 'accesstransformer.cfg'
attributes 'FMLAT': 'accesstransformer.cfg'
attributes 'FMLCorePlugin': "${mod_plugin_class}"
attributes 'FMLCorePluginContainsFMLMod': 'true'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ public class PatchPotionCoreResistance extends PatchManager {
public PatchPotionCoreResistance() {
super("Patch Potion Core Resistance");

add(new Patch(this, "com.tmtravlr.potioncore.PotionCoreAttributes",
ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES) {
@Override
public void patch(ClassNode clazzNode) {
MethodNode m_clinit = this.findMethodWithDesc(clazzNode, "()V", "<clinit>");

AbstractInsnNode target = ASMUtil.findMethodInsn(m_clinit, Opcodes.INVOKEVIRTUAL,
"net/minecraft/entity/ai/attributes/RangedAttribute", "func_111112_a", "setShouldWatch",
"(Z)Lnet/minecraft/entity/ai/attributes/BaseAttribute;", 3);
target = target.getPrevious();

m_clinit.instructions.insertBefore(target, ASMUtil.listOf(new MethodInsnNode(Opcodes.INVOKESTATIC,
"com/charles445/rltweaker/hook/HookPotionCore", "resistance_createAttribute",
"(Lnet/minecraft/entity/ai/attributes/RangedAttribute;)Lnet/minecraft/entity/ai/attributes/RangedAttribute;",
false)));
}
});

add(new Patch(this, "com.tmtravlr.potioncore.PotionCoreEffects",
ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public class ConfigPotionCore
@RLConfig.RLCraftTwoEightTwo("true")
@RLConfig.RLCraftTwoNine("true")
public boolean incurableTipsy = true;
@Config.Comment("Requires patchPotionCoreResistance patch! Enable to make resistance potion and resistance attribute to stack additively (otherwise they are stacked multiplicatively)")
@Config.Name("Additive Resistance Stacking")

@Config.Comment("Requires patchPotionCoreResistance patch!")
@Config.Name("Alternative Resistance Mode")
@RLConfig.ImprovementsOnly("true")
@RLConfig.RLCraftTwoEightTwo("true")
@RLConfig.RLCraftTwoNine("true")
public boolean additiveResistanceStacking = true;
public boolean alternativeResistanceMode = true;

@Config.Comment("Requires patchPotionCoreResistance patch! Resistance of the resistance potion attribute modifier.")
@Config.Name("Resistance Potion Modifier Amount")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public class PatchConfig
public boolean patchAmphithereTameDamage = false;

@Config.RequiresMcRestart
@Config.Comment("Fixes Potion Core Resistance calculations")
@Config.Comment("Requires patchCustomAttributeInstances patch! Fixes Potion Core Resistance calculations")
@RLConfig.ImprovementsOnly("true")
@RLConfig.RLCraftTwoEightTwo("true")
@RLConfig.RLCraftTwoNine("true")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.charles445.rltweaker.entity.attribute;

import net.minecraft.entity.ai.attributes.IAttribute;
import net.minecraft.entity.ai.attributes.RangedAttribute;

public abstract class ExtendedRangedAttribute extends RangedAttribute implements AttributeInstanceFactory {

public ExtendedRangedAttribute(IAttribute parentIn, String unlocalizedNameIn, double defaultValue,
double minimumValueIn, double maximumValueIn) {
super(parentIn, unlocalizedNameIn, defaultValue, minimumValueIn, maximumValueIn);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.charles445.rltweaker.entity.attribute;

import net.minecraft.entity.ai.attributes.AbstractAttributeMap;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.IAttribute;
import net.minecraft.entity.ai.attributes.ModifiableAttributeInstance;

public class InversedAttributeInstance extends ModifiableAttributeInstance {

public InversedAttributeInstance(AbstractAttributeMap attributeMapIn, IAttribute genericAttributeIn) {
super(attributeMapIn, genericAttributeIn);
}

@Override
public double computeValue() {
double d0 = this.getBaseValue();

for (AttributeModifier attributemodifier : this.getAppliedModifiers(0)) {
d0 -= attributemodifier.getAmount();
}

double d1 = d0;

for (AttributeModifier attributemodifier1 : this.getAppliedModifiers(1)) {
d1 -= d0 * attributemodifier1.getAmount();
}

for (AttributeModifier attributemodifier2 : this.getAppliedModifiers(2)) {
d1 *= 1.0D - attributemodifier2.getAmount();
}

return this.genericAttribute.clampValue(d1);
}

}
26 changes: 23 additions & 3 deletions src/main/java/com/charles445/rltweaker/hook/HookPotionCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import java.util.List;

import com.charles445.rltweaker.config.ModConfig;
import com.charles445.rltweaker.entity.attribute.ExtendedRangedAttribute;
import com.charles445.rltweaker.entity.attribute.InversedAttributeInstance;

import meldexun.reflectionutil.ReflectionField;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.ai.attributes.AbstractAttributeMap;
import net.minecraft.entity.ai.attributes.IAttribute;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.ai.attributes.RangedAttribute;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -74,6 +78,22 @@ public static PotionEffect onAddTipsyEffect(PotionEffect potionEffect)
"com.tmtravlr.potioncore.PotionCoreAttributes", "DAMAGE_RESISTANCE", "DAMAGE_RESISTANCE");
private static float originalDamage;

public static RangedAttribute resistance_createAttribute(RangedAttribute originalAttribute) {
if (!ModConfig.server.potioncore.alternativeResistanceMode) {
return originalAttribute;
}
// override resistance attribute when alternativeResistanceMode is enabled
return new ExtendedRangedAttribute(null, originalAttribute.getName(), originalAttribute.getDefaultValue(), 0.0D,
Double.MAX_VALUE) {

@Override
public IAttributeInstance createInstance(AbstractAttributeMap attributeMap, IAttribute attribute) {
return new InversedAttributeInstance(attributeMap, attribute);
}

};
}

public static Potion resistance_registerPotionAttributeModifier(Potion potion, IAttribute attribute,
String uniqueId, double ammount, int operation) {
// override registration of attribute modifier
Expand All @@ -95,10 +115,10 @@ public static float postResistancePotionCalculation(EntityLivingBase entity, flo
return damage;
}
IAttributeInstance attribute = entity.getEntityAttribute(DAMAGE_RESISTANCE.get(null));
if (ModConfig.server.potioncore.additiveResistanceStacking) {
return (float) (originalDamage * (damage / originalDamage - (attribute.getAttributeValue() - 1.0D)));
if (ModConfig.server.potioncore.alternativeResistanceMode) {
return (float) (originalDamage * attribute.getAttributeValue());
} else {
return (float) (damage * (1.0D - (attribute.getAttributeValue() - 1.0D)));
return (float) (originalDamage * (1.0D - (attribute.getAttributeValue() - 1.0D)));
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public net.minecraft.entity.ai.attributes.ModifiableAttributeInstance field_111136_b # genericAttribute
public net.minecraft.entity.ai.attributes.ModifiableAttributeInstance func_111129_g()D # computeValue
public net.minecraft.entity.ai.attributes.ModifiableAttributeInstance func_180375_b(I)Ljava/util/Collection; # getAppliedModifiers

0 comments on commit f78c7e3

Please sign in to comment.