simplify brainsweeping

This commit is contained in:
yrsegal@gmail.com 2022-04-29 11:37:15 -04:00
parent 96d78297b6
commit 94ff6f0003
5 changed files with 8 additions and 35 deletions

View file

@ -2,13 +2,11 @@ package at.petrak.hexcasting.common.misc;
import at.petrak.hexcasting.common.network.HexMessages;
import at.petrak.hexcasting.common.network.MsgBrainsweepAck;
import at.petrak.hexcasting.mixin.AccessorLivingEntity;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.npc.VillagerDataHolder;
import net.minecraft.world.entity.raid.Raider;
@ -31,23 +29,13 @@ public class Brainsweeping {
}
public static void brainsweep(LivingEntity entity) {
if (isValidTarget(entity)) {
if (entity instanceof Mob mob && isValidTarget(entity)) {
entity.getPersistentData().putBoolean(TAG_BRAINSWEPT, true);
if (entity instanceof Mob mob)
mob.removeFreeWill();
mob.removeFreeWill();
if (entity instanceof Villager villager) {
Brain<Villager> brain = villager.getBrain();
if (entity.level instanceof ServerLevel slevel) {
brain.stopAll(slevel, villager);
}
((AccessorLivingEntity) entity).hex$SetBrain(brain.copyWithoutBehaviors());
}
if (entity.level instanceof ServerLevel) {
if (entity.level instanceof ServerLevel)
HexMessages.getNetwork().send(PacketDistributor.TRACKING_ENTITY.with(() -> entity), MsgBrainsweepAck.of(entity));
}
}
}

View file

@ -1,12 +0,0 @@
package at.petrak.hexcasting.mixin;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.Brain;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(LivingEntity.class)
public interface AccessorLivingEntity {
@Accessor("brain")
void hex$SetBrain(Brain<?> brain);
}

View file

@ -1,15 +1,13 @@
package at.petrak.hexcasting.mixin;
import at.petrak.hexcasting.common.misc.Brainsweeping;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.Mob;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
// Prevents the villager from any of its brain goals or making ambient noise
// Prevents brainswept mobs from having an AI tick
@Mixin(Mob.class)
public class MixinMob {
@Inject(method = "serverAiStep", at = @At("HEAD"), cancellable = true)
@ -20,11 +18,11 @@ public class MixinMob {
}
}
@Inject(method = "getAmbientSound", at = @At("HEAD"), cancellable = true)
protected void onGetAmbientSound(CallbackInfoReturnable<SoundEvent> ci) {
@Inject(method = "playAmbientSound", at = @At("HEAD"), cancellable = true)
protected void onPlayAmbientSound(CallbackInfo ci) {
var self = (Mob) (Object) this;
if (Brainsweeping.isBrainswept(self)) {
ci.setReturnValue(null);
ci.cancel();
}
}
}

View file

@ -7,7 +7,7 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
// Prevents the villager from any of its brain goals or making ambient noise
// Prevents the villager from any of its brain goals
@Mixin(Villager.class)
public class MixinVillager {
@Inject(method = "registerBrainGoals", at = @At("HEAD"), cancellable = true)

View file

@ -5,7 +5,6 @@
"refmap": "hexcasting.mixins.refmap.json",
"package": "at.petrak.hexcasting.mixin",
"mixins": [
"AccessorLivingEntity",
"MixinMob",
"MixinVillager"
]