Skip to content

Commit

Permalink
Finalize Config & Mixins, (application to world still needed)
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Jun 18, 2024
1 parent bbd3f1b commit cc0dd0f
Show file tree
Hide file tree
Showing 22 changed files with 704 additions and 412 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ tasks {

compileKotlin {
compilerOptions.freeCompilerArgs.set(listOf("-Xjvm-default=all-compatibility"))
compilerOptions.jvmTarget.set(JvmTarget.JVM_21)
compilerOptions.jvmTarget.set(JvmTarget.JVM_17)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.ModifyReceiver;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.sugar.Local;
import org.spongepowered.asm.mixin.Final;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.bibireden.data_attributes.api.event.EntityAttributeModifiedEvents;
import com.bibireden.data_attributes.mutable.MutableAttributeContainer;
Expand Down Expand Up @@ -64,13 +65,13 @@ private void updateTrackedStatus(EntityAttributeInstance instance) {}
@ModifyReceiver(method = "getAttributesToSend", at = @At(value = "INVOKE", target = "Ljava/util/Map;values()Ljava/util/Collection;"))
private Map<?, ?> data_attributes$getAttributesToSend(Map<?, ?> instance) { return this.data_custom; }

@ModifyReturnValue(method = "getCustomInstance(Lnet/minecraft/entity/attribute/EntityAttribute;)Lnet/minecraft/entity/attribute/EntityAttributeInstance;", at = @At("RETURN"))
private EntityAttributeInstance data_attributes$getCustomInstance(EntityAttributeInstance instance) {
Identifier identifier = Registries.ATTRIBUTE.getId(instance.getAttribute());
@Inject(method = "getCustomInstance(Lnet/minecraft/entity/attribute/EntityAttribute;)Lnet/minecraft/entity/attribute/EntityAttributeInstance;", at = @At("HEAD"), cancellable = true)
private void data_getCustomInstance(EntityAttribute attribute2, CallbackInfoReturnable<EntityAttributeInstance> ci) {
Identifier identifier = Registries.ATTRIBUTE.getId(attribute2);

if (identifier != null) {
EntityAttributeInstance entityAttributeInstance = this.data_custom
.computeIfAbsent(identifier, id -> this.fallback.createOverride(this::updateTrackedStatus, instance.getAttribute()));
.computeIfAbsent(identifier, id -> this.fallback.createOverride(this::updateTrackedStatus, attribute2));

if (entityAttributeInstance != null) {
MutableAttributeInstance mutable = (MutableAttributeInstance) entityAttributeInstance;
Expand All @@ -81,10 +82,10 @@ private void updateTrackedStatus(EntityAttributeInstance instance) {}
}
}

return entityAttributeInstance;
ci.setReturnValue(entityAttributeInstance);
} else {
ci.setReturnValue((EntityAttributeInstance) null);
}

return null;
}

@ModifyExpressionValue(
Expand All @@ -95,34 +96,34 @@ private void updateTrackedStatus(EntityAttributeInstance instance) {}
return this.data_custom.get(Registries.ATTRIBUTE.getId(attribute)) != null || original;
}

@ModifyExpressionValue(method = "hasModifierForAttribute(Lnet/minecraft/entity/attribute/EntityAttribute;Ljava/util/UUID;)Z", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_hasModifierForAttribute(Object attribute, @Local(argsOnly = true) EntityAttribute param) {
Identifier identifier = Registries.ATTRIBUTE.getId(param);
@Redirect(method = "hasModifierForAttribute(Lnet/minecraft/entity/attribute/EntityAttribute;Ljava/util/UUID;)Z", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_hasModifierForAttribute(Map<?, ?> instances, Object attribute) {
Identifier identifier = Registries.ATTRIBUTE.getId((EntityAttribute) attribute);
return this.data_custom.get(identifier);
}

@ModifyExpressionValue(method = "getValue", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_attributes$getValue(Object attribute, @Local(argsOnly = true) EntityAttribute param) {
Identifier identifier = Registries.ATTRIBUTE.getId(param);
@Redirect(method = "getValue", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_getValue(Map<?, ?> instances, Object attribute) {
Identifier identifier = Registries.ATTRIBUTE.getId((EntityAttribute) attribute);
return this.data_custom.get(identifier);
}

@ModifyExpressionValue(method = "getBaseValue", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_attributes$getBaseValue(Object attribute, @Local(argsOnly = true) EntityAttribute param) {
Identifier identifier = Registries.ATTRIBUTE.getId(param);
@Redirect(method = "getBaseValue", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_getBaseValue(Map<?, ?> instances, Object attribute) {
Identifier identifier = Registries.ATTRIBUTE.getId((EntityAttribute) attribute);
return this.data_custom.get(identifier);
}

@ModifyExpressionValue(method = "getModifierValue(Lnet/minecraft/entity/attribute/EntityAttribute;Ljava/util/UUID;)D", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_getModifierValue(Object attribute, @Local(argsOnly = true) EntityAttribute param) {
Identifier identifier = Registries.ATTRIBUTE.getId(param);
@Redirect(method = "getModifierValue(Lnet/minecraft/entity/attribute/EntityAttribute;Ljava/util/UUID;)D", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_getModifierValue(Map<?, ?> instances, Object attribute) {
Identifier identifier = Registries.ATTRIBUTE.getId((EntityAttribute) attribute);
return this.data_custom.get(identifier);
}

// Injection to remove custom modifiers
@Inject(method = "removeModifiers", at = @At("HEAD"), cancellable = true)
private void data_removeModifiers(Multimap<EntityAttribute, EntityAttributeModifier> attributeModifiers,
CallbackInfo ci) {
CallbackInfo ci) {
attributeModifiers.asMap().forEach((attribute, collection) -> {
Identifier identifier = Registries.ATTRIBUTE.getId(attribute);
EntityAttributeInstance entityAttributeInstance = this.data_custom.get(identifier);
Expand Down Expand Up @@ -156,8 +157,8 @@ private void data_setFrom(AttributeContainer other, CallbackInfo ci) {
}

// Redirecting to use custom attributes for serialization
@ModifyExpressionValue(method = "toNbt", at = @At(value = "INVOKE", target = "Ljava/util/Map;values()Ljava/util/Collection;"))
private Collection<?> data_toNbt(Collection<?> original) {
@Redirect(method = "toNbt", at = @At(value = "INVOKE", target = "Ljava/util/Map;values()Ljava/util/Collection;"))
private Collection<?> data_toNbt(Map<?, ?> instances) {
return this.data_custom.values();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import java.util.HashMap;
import java.util.Map;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.sugar.Local;
import org.spongepowered.asm.mixin.Final;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.bibireden.data_attributes.mutable.MutableDefaultAttributeContainer;

Expand All @@ -35,7 +34,7 @@ abstract class DefaultAttributeContainerMixin implements MutableDefaultAttribute
// Injecting into the constructor to initialize the custom map
@Inject(method = "<init>", at = @At("TAIL"))
private void data_init(Map<EntityAttribute, EntityAttributeInstance> instances, CallbackInfo ci) {
this.data_instances = new HashMap<>();
this.data_instances = new HashMap<Identifier, EntityAttributeInstance>();

// Populating the custom map with identifiers and instances
instances.forEach((attribute, instance) -> {
Expand All @@ -48,31 +47,32 @@ private void data_init(Map<EntityAttribute, EntityAttributeInstance> instances,
}

// Redirecting the 'require' method to use the custom map
@ModifyExpressionValue(method = "require", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_require(Object original, @Local(argsOnly = true) EntityAttribute attribute) {
Identifier identifier = Registries.ATTRIBUTE.getId(attribute);
@Redirect(method = "require", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_require(Map<?, ?> instances, Object attribute) {
EntityAttribute entityAttribute = (EntityAttribute) attribute;
Identifier identifier = Registries.ATTRIBUTE.getId(entityAttribute);
return this.data_instances.getOrDefault(identifier, this.instances.get(attribute));
}

// Redirecting the 'createOverride' method to use the custom map
@ModifyExpressionValue(method = "createOverride", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_createOverride(Object original, @Local(argsOnly = true) EntityAttribute attribute) {
Identifier identifier = Registries.ATTRIBUTE.getId(attribute);
@Redirect(method = "createOverride", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_createOverride(Map<?, ?> instances, Object attribute) {
Identifier identifier = Registries.ATTRIBUTE.getId((EntityAttribute) attribute);
return this.data_instances.getOrDefault(identifier, this.instances.get(attribute));
}

// Injecting into the 'has' method to check for the existence of attributes in
// the custom map
@ModifyReturnValue(method = "has", at = @At("RETURN"))
private boolean data_has(boolean original, @Local(argsOnly = true) EntityAttribute type) {
@Inject(method = "has", at = @At("HEAD"), cancellable = true)
private void data_has(EntityAttribute type, CallbackInfoReturnable<Boolean> ci) {
Identifier identifier = Registries.ATTRIBUTE.getId(type);
return this.data_instances.containsKey(identifier) || this.instances.containsKey(type);
ci.setReturnValue(this.data_instances.containsKey(identifier) || this.instances.containsKey(type));
}

// Redirecting the 'hasModifier' method to use the custom map
@ModifyExpressionValue(method = "hasModifier", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_hasModifier(Object original, @Local(argsOnly = true) EntityAttribute type) {
Identifier identifier = Registries.ATTRIBUTE.getId(type);
@Redirect(method = "hasModifier", at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object data_hasModifier(Map<?, ?> instances, Object type) {
Identifier identifier = Registries.ATTRIBUTE.getId((EntityAttribute) type);
return this.data_instances.getOrDefault(identifier, this.instances.get(type));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import java.util.function.Consumer;

import com.bibireden.data_attributes.data.AttributeFunction;
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.bibireden.data_attributes.utils.DiminishingMathKt;
import org.spongepowered.asm.mixin.Final;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.bibireden.data_attributes.api.attribute.StackingBehavior;
import com.bibireden.data_attributes.api.attribute.IEntityAttribute;
Expand All @@ -32,6 +33,7 @@
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.util.Identifier;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;

@Mixin(EntityAttributeInstance.class)
abstract class EntityAttributeInstanceMixin implements MutableAttributeInstance, IEntityAttributeInstance {
Expand Down Expand Up @@ -66,25 +68,27 @@ private Collection<EntityAttributeModifier> getModifiersByOperation(EntityAttrib
protected void onUpdate() {
}

@Shadow private double value;

@Inject(method = "<init>", at = @At("TAIL"))
private void data_init(EntityAttribute type, Consumer<EntityAttributeInstance> updateCallback, CallbackInfo ci) {
this.data_identifier = Registries.ATTRIBUTE.getId(type);
}

@ModifyReturnValue(method = "getAttribute", at = @At("RETURN"))
private EntityAttribute data_getAttribute(EntityAttribute original) {
@Inject(method = "getAttribute", at = @At("HEAD"), cancellable = true)
private void data_getAttribute(CallbackInfoReturnable<EntityAttribute> ci) {
EntityAttribute attribute = Registries.ATTRIBUTE.get(this.data_identifier);

if (attribute != null) {
return attribute;
ci.setReturnValue(attribute);
} else {
ci.setReturnValue(this.type);
}

return this.type;
}

@SuppressWarnings("all")
@ModifyReturnValue(method = "computeValue", at = @At("RETURN"))
private double data_computeValue(double original) {
@Inject(method = "computeValue", at = @At("HEAD"), cancellable = true)
private void data_computeValue(CallbackInfoReturnable<Double> ci) {
// DiminishingMathKt.computeStacking((EntityAttributeInstance) (Object) this, this.type, this.data_containerCallback);

MutableEntityAttribute attribute = (MutableEntityAttribute) ((EntityAttributeInstance) (Object) this).getAttribute();
Expand Down Expand Up @@ -114,11 +118,11 @@ private double data_computeValue(double original) {

if (this.data_containerCallback != null) {
Map<IEntityAttribute, AttributeFunction> parents = ((MutableEntityAttribute) attribute)
.data_attributes$parentsMutable();
.data_attributes$parentsMutable();

for (IEntityAttribute parent : parents.keySet()) {
EntityAttributeInstance instance = this.data_containerCallback
.getCustomInstance((EntityAttribute) parent);
.getCustomInstance((EntityAttribute) parent);

if (instance == null)
continue;
Expand Down Expand Up @@ -147,22 +151,22 @@ private double data_computeValue(double original) {
double e = d;

for (EntityAttributeModifier modifier : this
.getModifiersByOperation(EntityAttributeModifier.Operation.MULTIPLY_BASE)) {
.getModifiersByOperation(EntityAttributeModifier.Operation.MULTIPLY_BASE)) {
e += d * modifier.getValue();
}

for (EntityAttributeModifier modifier : this
.getModifiersByOperation(EntityAttributeModifier.Operation.MULTIPLY_TOTAL)) {
.getModifiersByOperation(EntityAttributeModifier.Operation.MULTIPLY_TOTAL)) {
e *= 1.0D + modifier.getValue();
}

if (this.data_containerCallback != null) {
Map<IEntityAttribute, AttributeFunction> parents = ((MutableEntityAttribute) attribute)
.data_attributes$parentsMutable();
.data_attributes$parentsMutable();

for (IEntityAttribute parent : parents.keySet()) {
EntityAttributeInstance instance = this.data_containerCallback
.getCustomInstance((EntityAttribute) parent);
.getCustomInstance((EntityAttribute) parent);

if (instance == null)
continue;
Expand All @@ -180,14 +184,14 @@ private double data_computeValue(double original) {
value += attribute.data_attributes$min();
}

return value;
ci.setReturnValue(value);
}

@Inject(method = "addModifier", at = @At("HEAD"), cancellable = true)
private void data_addModifier(EntityAttributeModifier modifier, CallbackInfo ci) {
EntityAttributeInstance instance = (EntityAttributeInstance) (Object) this;
UUID key = modifier.getId();
EntityAttributeModifier entityAttributeModifier = this.idToModifiers.get(key);
EntityAttributeModifier entityAttributeModifier = (EntityAttributeModifier) this.idToModifiers.get(key);

if (entityAttributeModifier != null) {
throw new IllegalArgumentException("Modifier is already applied on this attribute!");
Expand All @@ -214,10 +218,10 @@ private void data_removeModifier(EntityAttributeModifier modifier, CallbackInfo
ci.cancel();
}

@ModifyExpressionValue(method = "toNbt", at = @At(value = "INVOKE", target = "Lnet/minecraft/registry/Registry;getId(Ljava/lang/Object;)Lnet/minecraft/util/Identifier;"))
private Identifier data_toNbt(Identifier id) {
@Redirect(method = "toNbt", at = @At(value = "INVOKE", target = "Lnet/minecraft/registry/Registry;getId(Ljava/lang/Object;)Lnet/minecraft/util/Identifier;"))
private Identifier data_toNbt(Registry<?> registry, Object type) {
if (this.data_identifier == null)
return Registries.ATTRIBUTE.getId(this.type);
return Registries.ATTRIBUTE.getId((EntityAttribute) type);
return this.data_identifier;
}

Expand All @@ -228,7 +232,7 @@ public Identifier getId() {

@Override
public void actionModifier(final VoidConsumer consumerIn, final EntityAttributeInstance instanceIn,
final EntityAttributeModifier modifierIn, final boolean isWasAdded) {
final EntityAttributeModifier modifierIn, final boolean isWasAdded) {
EntityAttribute entityAttribute = ((EntityAttributeInstance) (Object) this).getAttribute();
MutableEntityAttribute parent = (MutableEntityAttribute) entityAttribute;

Expand All @@ -252,7 +256,7 @@ public void actionModifier(final VoidConsumer consumerIn, final EntityAttributeI
LivingEntity livingEntity = ((MutableAttributeContainer) this.data_containerCallback).data_attributes$getLivingEntity();

EntityAttributeModifiedEvents.MODIFIED.invoker().onModified(entityAttribute, livingEntity, modifierIn, value,
isWasAdded);
isWasAdded);

for (IEntityAttribute child : parent.data_attributes$childrenMutable().keySet()) {
EntityAttribute attribute = (EntityAttribute) child;
Expand Down Expand Up @@ -283,7 +287,9 @@ public void updateModifier(final UUID uuid, final double value) {
if (modifier == null)
return;

this.actionModifier(() -> ((MutableAttributeModifier) modifier).updateValue(value), instance, modifier, false);
this.actionModifier(() -> {
((MutableAttributeModifier) modifier).updateValue(value);
}, instance, modifier, false);
}

@Override
Expand Down
Loading

0 comments on commit cc0dd0f

Please sign in to comment.