Commit 26ef75a1 by Jonathan Thomas

Replacing reflection to use a mixin for accessing the GoalSelector.

parent fa1841d6
Pipeline #11931 passed with stage
in 20 seconds
package com.owlmaddie.goals; package com.owlmaddie.goals;
import com.owlmaddie.mixin.MixinMobEntity;
import net.minecraft.entity.ai.goal.GoalSelector; import net.minecraft.entity.ai.goal.GoalSelector;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import java.lang.reflect.Field;
/** /**
* The {@code GoalUtils} class uses reflection to extend the MobEntity class * The {@code GoalUtils} class uses reflection to extend the MobEntity class
...@@ -11,23 +11,8 @@ import java.lang.reflect.Field; ...@@ -11,23 +11,8 @@ import java.lang.reflect.Field;
*/ */
public class GoalUtils { public class GoalUtils {
private static Field goalSelectorField;
static {
try {
goalSelectorField = MobEntity.class.getDeclaredField("goalSelector");
goalSelectorField.setAccessible(true);
} catch (NoSuchFieldException e) {
// Handle the exception, perhaps the field name has changed
}
}
public static GoalSelector getGoalSelector(MobEntity mobEntity) { public static GoalSelector getGoalSelector(MobEntity mobEntity) {
try { MixinMobEntity mixingEntity = (MixinMobEntity)mobEntity;
return (GoalSelector) goalSelectorField.get(mobEntity); return mixingEntity.getGoalSelector();
} catch (IllegalAccessException e) {
// Handle the exception
return null;
}
} }
} }
package com.owlmaddie.mixin;
import org.spongepowered.asm.mixin.Mixin;
import net.minecraft.entity.mob.MobEntity;
import org.spongepowered.asm.mixin.gen.Accessor;
import net.minecraft.entity.ai.goal.GoalSelector;
/**
* The {@code MixinMobEntity} mixin class exposes the goalSelector field from the MobEntity class.
*/
@Mixin(MobEntity.class)
public interface MixinMobEntity {
@Accessor("goalSelector") public GoalSelector getGoalSelector();
}
\ No newline at end of file
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
"package": "com.owlmaddie.mixin", "package": "com.owlmaddie.mixin",
"compatibilityLevel": "JAVA_17", "compatibilityLevel": "JAVA_17",
"mixins": [ "mixins": [
"ExampleMixin" "ExampleMixin",
"MixinMobEntity"
], ],
"injectors": { "injectors": {
"defaultRequire": 1 "defaultRequire": 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment