Skip to content

Commit

Permalink
Improve Heisei Dream (fixes VazkiiMods#4064)
Browse files Browse the repository at this point in the history
Does two things:
- Set the ATTACK_TARGET memory for mobs using the new Brain system (ie it works on Piglins now)
- Override checks in TargetingConditions that prevented Illagers from attacking each other
  • Loading branch information
jpenilla committed Apr 21, 2023
1 parent f6d01ae commit 4a5a2a2
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package vazkii.botania.common;

public interface HurtByTargetGoalAccess {
void botania$setBrainwashed();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package vazkii.botania.common;

public interface TargetingConditionsAccess {
void botania$setBrainwashed();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import net.minecraft.world.entity.ai.goal.GoalSelector;
import net.minecraft.world.entity.ai.goal.WrappedGoal;
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.monster.Enemy;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;

import vazkii.botania.api.block_entity.FunctionalFlowerBlockEntity;
import vazkii.botania.api.block_entity.RadiusDescriptor;
import vazkii.botania.common.HurtByTargetGoalAccess;
import vazkii.botania.common.block.BotaniaFlowerBlocks;
import vazkii.botania.mixin.GoalSelectorAccessor;
import vazkii.botania.mixin.HurtByTargetGoalAccessor;
Expand Down Expand Up @@ -78,6 +80,7 @@ public static boolean brainwashEntity(Mob entity, List<Enemy> mobs) {

// Move any HurtByTargetGoal to highest priority
GoalSelector targetSelector = ((MobAccessor) entity).getTargetSelector();
boolean modifiedGoals = false;
for (WrappedGoal entry : ((GoalSelectorAccessor) targetSelector).getAvailableGoals()) {
if (entry.getGoal() instanceof HurtByTargetGoal goal) {
// Remove all ignorals. We can't actually resize or overwrite
Expand All @@ -89,10 +92,18 @@ public static boolean brainwashEntity(Mob entity, List<Enemy> mobs) {
// Concurrent modification OK since we break out of the loop
targetSelector.removeGoal(goal);
targetSelector.addGoal(-1, goal);

((HurtByTargetGoalAccess) goal).botania$setBrainwashed();
modifiedGoals = true;
break;
}
}

if (!modifiedGoals) {
// For entities using the newer Brain system
entity.getBrain().setMemory(MemoryModuleType.ATTACK_TARGET, mob);
}

// Now set last hurt by, which HurtByTargetGoal will pick up
entity.setLastHurtByMob(mob);
did = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package vazkii.botania.mixin;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
import net.minecraft.world.entity.ai.goal.target.TargetGoal;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;

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.Redirect;

import vazkii.botania.common.HurtByTargetGoalAccess;
import vazkii.botania.common.TargetingConditionsAccess;

@Mixin(HurtByTargetGoal.class)
abstract class HurtByTargetGoalMixin extends TargetGoal implements HurtByTargetGoalAccess {
@Shadow
@Final
private static TargetingConditions HURT_BY_TARGETING;
@Unique
private boolean brainwashed;

private static final TargetingConditions BRAINWASHED_CONDITIONS = HURT_BY_TARGETING.copy();

static {
((TargetingConditionsAccess) BRAINWASHED_CONDITIONS).botania$setBrainwashed();
}

public HurtByTargetGoalMixin(Mob $$0, boolean $$1) {
super($$0, $$1);
}

@Override
public void botania$setBrainwashed() {
this.brainwashed = true;
}

@Redirect(
method = "canUse()Z",
at = @At(
target = "Lnet/minecraft/world/entity/ai/goal/target/HurtByTargetGoal;canAttack(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/world/entity/ai/targeting/TargetingConditions;)Z",
value = "INVOKE"
)
)
boolean replaceAttackPredicate(HurtByTargetGoal instance, LivingEntity livingEntity, TargetingConditions targetingConditions) {
return this.canAttack(livingEntity, this.brainwashed ? BRAINWASHED_CONDITIONS : HURT_BY_TARGETING);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package vazkii.botania.mixin;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import vazkii.botania.common.TargetingConditionsAccess;

@Mixin(TargetingConditions.class)
abstract class TargetingConditionsMixin implements TargetingConditionsAccess {
private boolean brainwashed;

@Redirect(
method = "test",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/LivingEntity;canAttack(Lnet/minecraft/world/entity/LivingEntity;)Z"
)
)
boolean redirectCanAttack(LivingEntity instance, LivingEntity entity) {
return this.brainwashed || instance.canAttack(entity);
}

@Redirect(
method = "test",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/LivingEntity;canAttackType(Lnet/minecraft/world/entity/EntityType;)Z"
)
)
boolean redirectCanAttackType(LivingEntity instance, EntityType<?> entityType) {
return this.brainwashed || instance.canAttackType(entityType);
}

@Redirect(
method = "test",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/LivingEntity;isAlliedTo(Lnet/minecraft/world/entity/Entity;)Z"
)
)
boolean redirectIsAlliedTo(LivingEntity instance, Entity entity) {
return !this.brainwashed && instance.isAlliedTo(entity);
}

@Override
public void botania$setBrainwashed() {
this.brainwashed = true;
}
}
2 changes: 2 additions & 0 deletions Xplat/src/main/resources/botania_xplat.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"GoalSelectorAccessor",
"HopperBlockEntityAccessor",
"HurtByTargetGoalAccessor",
"HurtByTargetGoalMixin",
"IngredientAccessor",
"ItemEntityAccessor",
"ItemEntityMixin",
Expand All @@ -50,6 +51,7 @@
"SoundTypeAccessor",
"SpawnPlacementsMixin",
"StatsAccessor",
"TargetingConditionsMixin",
"TextureSlotAccessor",
"ThrowableProjectileMixin",
"WitherEntityAccessor"
Expand Down

0 comments on commit 4a5a2a2

Please sign in to comment.