From 4e6157cc82930ac0f084597f26b44f54e277f256 Mon Sep 17 00:00:00 2001 From: AbdElAziz Date: Wed, 14 Jun 2023 17:50:21 +0300 Subject: [PATCH] Implement new changes --- .../canary/common/config/CanaryConfig.java | 1 - .../nearby_tracker/NearbyEntityListener.java | 9 -- .../NearbyEntityListenerProvider.java | 5 - .../nearby_tracker/NearbyEntityTracker.java | 116 ------------------ .../canary/mixin/CanaryMixinPlugin.java | 10 +- .../mixin/ai/goals/AvoidEntityGoalMixin.java | 63 ---------- .../mixin/ai/goals/LookAtPlayerGoalMixin.java | 73 ----------- src/main/resources/canary.mixins.json | 2 - 8 files changed, 9 insertions(+), 270 deletions(-) delete mode 100644 src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityListener.java delete mode 100644 src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityListenerProvider.java delete mode 100644 src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityTracker.java delete mode 100644 src/main/java/com/abdelaziz/canary/mixin/ai/goals/AvoidEntityGoalMixin.java delete mode 100644 src/main/java/com/abdelaziz/canary/mixin/ai/goals/LookAtPlayerGoalMixin.java diff --git a/src/main/java/com/abdelaziz/canary/common/config/CanaryConfig.java b/src/main/java/com/abdelaziz/canary/common/config/CanaryConfig.java index cb1af5f0..df40f70b 100644 --- a/src/main/java/com/abdelaziz/canary/common/config/CanaryConfig.java +++ b/src/main/java/com/abdelaziz/canary/common/config/CanaryConfig.java @@ -30,7 +30,6 @@ private CanaryConfig() { // Defines the default rules which can be configured by the user or other mods. // You must manually add a rule for any new mixins not covered by an existing package rule. this.addMixinRule("ai", true); - this.addMixinRule("ai.goals", true); this.addMixinRule("ai.pathing", true); this.addMixinRule("ai.poi", true); this.addMixinRule("ai.poi.fast_portals", true); diff --git a/src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityListener.java b/src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityListener.java deleted file mode 100644 index 88ab2f4a..00000000 --- a/src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityListener.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.abdelaziz.canary.common.entity.nearby_tracker; - -import net.minecraft.world.entity.Entity; - -public interface NearbyEntityListener { - default Class getEntityClass() { - return Entity.class; - } -} diff --git a/src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityListenerProvider.java b/src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityListenerProvider.java deleted file mode 100644 index 05920c8e..00000000 --- a/src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityListenerProvider.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.abdelaziz.canary.common.entity.nearby_tracker; - -public interface NearbyEntityListenerProvider { - void addListener(NearbyEntityListener listener); -} \ No newline at end of file diff --git a/src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityTracker.java b/src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityTracker.java deleted file mode 100644 index 7a5172a6..00000000 --- a/src/main/java/com/abdelaziz/canary/common/entity/nearby_tracker/NearbyEntityTracker.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.abdelaziz.canary.common.entity.nearby_tracker; - -import com.abdelaziz.canary.common.util.tuples.Range6Int; -import it.unimi.dsi.fastutil.objects.Reference2LongOpenHashMap; -import net.minecraft.core.SectionPos; -import net.minecraft.core.Vec3i; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.ai.targeting.TargetingConditions; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.phys.AABB; - -import java.util.List; - -/** - * Maintains a collection of all entities within the range of this listener. This allows AI goals to quickly - * assess nearby entities which match the provided class. - */ -public class NearbyEntityTracker implements NearbyEntityListener { - private final Class clazz; - private final LivingEntity self; - - private final Reference2LongOpenHashMap nearbyEntities = new Reference2LongOpenHashMap<>(0); - private final Range6Int chunkBoxRadius; - - public NearbyEntityTracker(Class clazz, LivingEntity self, Vec3i boxRadius) { - this.clazz = clazz; - this.self = self; - this.chunkBoxRadius = new Range6Int( - 1 + SectionPos.blockToSectionCoord(boxRadius.getX()), - 1 + SectionPos.blockToSectionCoord(boxRadius.getY()), - 1 + SectionPos.blockToSectionCoord(boxRadius.getZ()), - 1 + SectionPos.blockToSectionCoord(boxRadius.getX()), - 1 + SectionPos.blockToSectionCoord(boxRadius.getY()), - 1 + SectionPos.blockToSectionCoord(boxRadius.getZ()) - ); - } - - @Override - public Class getEntityClass() { - return this.clazz; - } - - /** - * Gets the closest T (extends LivingEntity) to the center of this tracker that also intersects with the given box and meets the - * requirements of the targetPredicate. - * The result may be different from vanilla if there are multiple closest entities. - * - * @param box the box the entities have to intersect - * @param targetPredicate predicate the entity has to meet - * @param x - * @param y - * @param z - * @return the closest Entity that meets all requirements (distance, box intersection, predicate, type T) - */ - public T getClosestEntity(AABB box, TargetingConditions targetPredicate, double x, double y, double z) { - T nearest = null; - double nearestDistance = Double.POSITIVE_INFINITY; - - for (T entity : this.nearbyEntities.keySet()) { - double distance; - if ( - (box == null || box.intersects(entity.getBoundingBox())) && - (distance = entity.distanceToSqr(x, y, z)) <= nearestDistance && - targetPredicate.test(this.self, entity) - ) { - if (distance == nearestDistance) { - nearest = this.getFirst(nearest, entity); - } else { - nearest = entity; - } - nearestDistance = distance; - } - } - - return nearest; - } - - /** - * Gets the Entity that is processed first in vanilla. - * @param entity1 one Entity - * @param entity2 the other Entity - * @return the Entity that is first in vanilla - */ - private T getFirst(T entity1, T entity2) { - if (this.getEntityClass() == Player.class) { - //Get first in player list - List players = this.self.getCommandSenderWorld().players(); - return players.indexOf((Player) entity1) < players.indexOf((Player) entity2) ? entity1 : entity2; - } else { - //Get first sorted by chunk section pos as long, then sorted by first added to the chunk section - //First added to this tracker and first added to the chunk section is equivalent here, because - //this tracker always tracks complete sections and the entities are added in order - long pos1 = SectionPos.asLong(entity1.blockPosition()); - long pos2 = SectionPos.asLong(entity2.blockPosition()); - if (pos1 < pos2) { - return entity1; - } else if (pos2 < pos1) { - return entity2; - } else { - if (this.nearbyEntities.getLong(entity1) < this.nearbyEntities.getLong(entity2)) { - return entity1; - } else { - return entity2; - } - } - } - - } - - @Override - public String toString() { - return super.toString() + " for entity class: " + this.clazz.getName() + ", around entity: " + this.self.toString() + " with NBT: " + this.self.saveWithoutId(new CompoundTag()); - } -} \ No newline at end of file diff --git a/src/main/java/com/abdelaziz/canary/mixin/CanaryMixinPlugin.java b/src/main/java/com/abdelaziz/canary/mixin/CanaryMixinPlugin.java index b95b1767..e1c29dc4 100644 --- a/src/main/java/com/abdelaziz/canary/mixin/CanaryMixinPlugin.java +++ b/src/main/java/com/abdelaziz/canary/mixin/CanaryMixinPlugin.java @@ -59,7 +59,15 @@ public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { } //Fix: if Forge errors is not empty then disable shapes, math.sine_lut and alloc.blockstate optimizations. (Thanks for malte!) - if ((mixinClassName.startsWith(MIXIN_PACKAGE_ROOT + "shapes") && mixinClassName.startsWith(MIXIN_PACKAGE_ROOT + "math.sine_lut") && mixinClassName.startsWith(MIXIN_PACKAGE_ROOT + "alloc.blockstate")) && !LoadingModList.get().getErrors().isEmpty()) { + if (mixinClassName.startsWith(MIXIN_PACKAGE_ROOT + "shapes") && !LoadingModList.get().getErrors().isEmpty()) { + return false; + } + + if (mixinClassName.startsWith(MIXIN_PACKAGE_ROOT + "math.sine_lut") && !LoadingModList.get().getErrors().isEmpty()) { + return false; + } + + if (mixinClassName.startsWith(MIXIN_PACKAGE_ROOT + "alloc.blockstate") && !LoadingModList.get().getErrors().isEmpty()) { return false; } diff --git a/src/main/java/com/abdelaziz/canary/mixin/ai/goals/AvoidEntityGoalMixin.java b/src/main/java/com/abdelaziz/canary/mixin/ai/goals/AvoidEntityGoalMixin.java deleted file mode 100644 index e4a2b62a..00000000 --- a/src/main/java/com/abdelaziz/canary/mixin/ai/goals/AvoidEntityGoalMixin.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.abdelaziz.canary.mixin.ai.goals; - -import com.abdelaziz.canary.common.entity.nearby_tracker.NearbyEntityListenerProvider; -import com.abdelaziz.canary.common.entity.nearby_tracker.NearbyEntityTracker; -import net.minecraft.core.Vec3i; -import net.minecraft.util.Mth; -import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.EntityDimensions; -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.PathfinderMob; -import net.minecraft.world.entity.ai.goal.AvoidEntityGoal; -import net.minecraft.world.entity.ai.targeting.TargetingConditions; -import net.minecraft.world.level.Level; -import net.minecraft.world.phys.AABB; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -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 java.util.List; -import java.util.function.Predicate; - -@Mixin(AvoidEntityGoal.class) -public class AvoidEntityGoalMixin { - @Shadow - @Final - protected PathfinderMob mob; - - @Shadow - @Final - protected float maxDist; - - private NearbyEntityTracker tracker; - - @Inject(method = "(Lnet/minecraft/world/entity/PathfinderMob;Ljava/lang/Class;Ljava/util/function/Predicate;FDDLjava/util/function/Predicate;)V", at = @At("RETURN")) - private void init(PathfinderMob mob, Class fleeFromType, Predicate predicate, float distance, double slowSpeed, double fastSpeed, Predicate predicate2, CallbackInfo ci) { - EntityDimensions dimensions = this.mob.getType().getDimensions(); - double adjustedRange = dimensions.width * 0.5D + this.maxDist + 2D; - int horizontalRange = Mth.ceil(adjustedRange); - this.tracker = new NearbyEntityTracker<>(fleeFromType, mob, new Vec3i(horizontalRange, Mth.ceil(dimensions.height + 3 + 2), horizontalRange)); - - ((NearbyEntityListenerProvider) mob).addListener(this.tracker); - } - - @Redirect( - method = "canUse()Z", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/world/level/Level;getNearestEntity(Ljava/util/List;Lnet/minecraft/world/entity/ai/targeting/TargetingConditions;Lnet/minecraft/world/entity/LivingEntity;DDD)Lnet/minecraft/world/entity/LivingEntity;" - ) - ) - private T redirectGetNearestEntity(Level world, List entityList, TargetingConditions targetPredicate, LivingEntity entity, double x, double y, double z) { - return this.tracker.getClosestEntity(this.mob.getBoundingBox().inflate(this.maxDist, 3.0D, this.maxDist), targetPredicate, x, y, z); - } - - @Redirect(method = "canUse()Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;getEntitiesOfClass(Ljava/lang/Class;Lnet/minecraft/world/phys/AABB;Ljava/util/function/Predicate;)Ljava/util/List;")) - private List redirectGetEntities(Level world, Class entityClass, AABB box, Predicate predicate) { - return null; - } -} diff --git a/src/main/java/com/abdelaziz/canary/mixin/ai/goals/LookAtPlayerGoalMixin.java b/src/main/java/com/abdelaziz/canary/mixin/ai/goals/LookAtPlayerGoalMixin.java deleted file mode 100644 index 336739b0..00000000 --- a/src/main/java/com/abdelaziz/canary/mixin/ai/goals/LookAtPlayerGoalMixin.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.abdelaziz.canary.mixin.ai.goals; - -import com.abdelaziz.canary.common.entity.nearby_tracker.NearbyEntityListenerProvider; -import com.abdelaziz.canary.common.entity.nearby_tracker.NearbyEntityTracker; -import net.minecraft.core.Vec3i; -import net.minecraft.util.Mth; -import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.EntityDimensions; -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.Mob; -import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal; -import net.minecraft.world.entity.ai.targeting.TargetingConditions; -import net.minecraft.world.entity.player.Player; -import net.minecraft.world.level.Level; -import net.minecraft.world.phys.AABB; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -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 java.util.List; -import java.util.function.Predicate; - -@Mixin(LookAtPlayerGoal.class) -public class LookAtPlayerGoalMixin { - @Shadow - @Final - protected Mob mob; - @Shadow - @Final - protected float lookDistance; - private NearbyEntityTracker tracker; - - @Inject(method = "(Lnet/minecraft/world/entity/Mob;Ljava/lang/Class;FFZ)V", at = @At("RETURN")) - private void init(Mob mob, Class targetType, float lookDistance, float chance, boolean b, CallbackInfo ci) { - EntityDimensions dimensions = this.mob.getType().getDimensions(); - double adjustedRange = dimensions.width * 0.5D + this.lookDistance + 2D; - int horizontalRange = Mth.ceil(adjustedRange); - this.tracker = new NearbyEntityTracker<>(targetType, mob, new Vec3i(horizontalRange, Mth.ceil(dimensions.height + 3 + 2), horizontalRange)); - - ((NearbyEntityListenerProvider) mob).addListener(this.tracker); - } - - @Redirect( - method = "canUse()Z", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/world/level/Level;getNearestEntity(Ljava/util/List;Lnet/minecraft/world/entity/ai/targeting/TargetingConditions;Lnet/minecraft/world/entity/LivingEntity;DDD)Lnet/minecraft/world/entity/LivingEntity;" - ) - ) - private LivingEntity redirectGetNearestEntity(Level world, List entityList, TargetingConditions targetPredicate, LivingEntity entity, double x, double y, double z) { - return this.tracker.getClosestEntity(this.mob.getBoundingBox().inflate(this.lookDistance, 3.0D, this.lookDistance), targetPredicate, x, y, z); - } - - @Redirect(method = "canUse()Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;getEntitiesOfClass(Ljava/lang/Class;Lnet/minecraft/world/phys/AABB;Ljava/util/function/Predicate;)Ljava/util/List;")) - private List redirectGetEntities(Level world, Class entityClass, AABB box, Predicate predicate) { - return null; - } - - @Redirect( - method = "canUse()Z", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/world/level/Level;getNearestPlayer(Lnet/minecraft/world/entity/ai/targeting/TargetingConditions;Lnet/minecraft/world/entity/LivingEntity;DDD)Lnet/minecraft/world/entity/player/Player;" - ) - ) - private Player redirectGetClosestPlayer(Level world, TargetingConditions targetPredicate, LivingEntity entity, double x, double y, double z) { - return (Player) this.tracker.getClosestEntity(null, targetPredicate, x, y, z); - } -} diff --git a/src/main/resources/canary.mixins.json b/src/main/resources/canary.mixins.json index 7c974b64..e3aeead1 100644 --- a/src/main/resources/canary.mixins.json +++ b/src/main/resources/canary.mixins.json @@ -9,8 +9,6 @@ "minVersion": "0.8.2", "refmap": "canary.refmap.json", "mixins": [ - "ai.goals.AvoidEntityGoalMixin", - "ai.goals.LookAtPlayerGoalMixin", "ai.pathing.BlockStateBaseMixin", "ai.pathing.FlyNodeEvaluatorMixin", "ai.pathing.PathNavigationRegionMixin",