simplify brainsweeping

This commit is contained in:
yrsegal@gmail.com 2022-04-29 11:37:15 -04:00
parent 733eac1b88
commit fa343ff75b
8 changed files with 26 additions and 30 deletions

View file

@ -17,11 +17,13 @@ import org.jetbrains.annotations.Nullable;
public class Brainsweeping { public class Brainsweeping {
// Keeping these functions in Brainsweeping just so we have to change less code // Keeping these functions in Brainsweeping just so we have to change less code
public static void brainsweep(Mob entity) { public static void brainsweep(Mob entity) {
IXplatAbstractions.INSTANCE.brainsweep(entity); if (isValidTarget(entity)) {
IXplatAbstractions.INSTANCE.brainsweep(entity);
}
} }
public static boolean isBrainswept(Mob entity) { public static boolean isBrainswept(Mob entity) {
return IXplatAbstractions.INSTANCE.isBrainswept(entity); return isValidTarget(entity) && IXplatAbstractions.INSTANCE.isBrainswept(entity);
} }
// TODO: make this a tag // TODO: make this a tag

View file

@ -1,15 +1,13 @@
package at.petrak.hexcasting.mixin; package at.petrak.hexcasting.mixin;
import at.petrak.hexcasting.common.misc.Brainsweeping; import at.petrak.hexcasting.common.misc.Brainsweeping;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.Mob;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; 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) @Mixin(Mob.class)
public class MixinMob { public class MixinMob {
@Inject(method = "serverAiStep", at = @At("HEAD"), cancellable = true) @Inject(method = "serverAiStep", at = @At("HEAD"), cancellable = true)
@ -20,11 +18,11 @@ public class MixinMob {
} }
} }
@Inject(method = "getAmbientSound", at = @At("HEAD"), cancellable = true) @Inject(method = "playAmbientSound", at = @At("HEAD"), cancellable = true)
protected void onGetAmbientSound(CallbackInfoReturnable<SoundEvent> ci) { protected void onPlayAmbientSound(CallbackInfo ci) {
var self = (Mob) (Object) this; var self = (Mob) (Object) this;
if (Brainsweeping.isBrainswept(self)) { 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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; 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) @Mixin(Villager.class)
public class MixinVillager { public class MixinVillager {
@Inject(method = "registerBrainGoals", at = @At("HEAD"), cancellable = true) @Inject(method = "registerBrainGoals", at = @At("HEAD"), cancellable = true)

View file

@ -1,12 +0,0 @@
package at.petrak.hexcasting.mixin.accessor;
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

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

View file

@ -3,7 +3,9 @@ package at.petrak.hexcasting.fabric.cc;
import dev.onyxstudios.cca.api.v3.component.Component; import dev.onyxstudios.cca.api.v3.component.Component;
import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent; import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
public class CCBrainswept implements Component, AutoSyncedComponent { public class CCBrainswept implements Component, AutoSyncedComponent {
public static final String TAG_BRAINSWEPT = "brainswept"; public static final String TAG_BRAINSWEPT = "brainswept";
@ -25,6 +27,13 @@ public class CCBrainswept implements Component, AutoSyncedComponent {
HexCardinalComponents.BRAINSWEPT.sync(this.owner); HexCardinalComponents.BRAINSWEPT.sync(this.owner);
} }
@Override
public void applySyncPacket(FriendlyByteBuf buf) {
AutoSyncedComponent.super.applySyncPacket(buf);
if (owner instanceof Mob mob && brainswept)
mob.removeFreeWill();
}
@Override @Override
public void readFromNbt(CompoundTag tag) { public void readFromNbt(CompoundTag tag) {
this.brainswept = tag.getBoolean(TAG_BRAINSWEPT); this.brainswept = tag.getBoolean(TAG_BRAINSWEPT);

View file

@ -84,6 +84,8 @@ public class FabricXplatImpl implements IXplatAbstractions {
var cc = HexCardinalComponents.BRAINSWEPT.get(mob); var cc = HexCardinalComponents.BRAINSWEPT.get(mob);
cc.setBrainswept(true); cc.setBrainswept(true);
// CC API does the syncing for us // CC API does the syncing for us
mob.removeFreeWill();
} }
@Override @Override

View file

@ -13,7 +13,6 @@ import at.petrak.hexcasting.api.spell.casting.CastingHarness;
import at.petrak.hexcasting.api.spell.casting.ResolvedPattern; import at.petrak.hexcasting.api.spell.casting.ResolvedPattern;
import at.petrak.hexcasting.api.utils.HexUtils; import at.petrak.hexcasting.api.utils.HexUtils;
import at.petrak.hexcasting.common.lib.HexItems; import at.petrak.hexcasting.common.lib.HexItems;
import at.petrak.hexcasting.common.misc.Brainsweeping;
import at.petrak.hexcasting.common.network.IMessage; import at.petrak.hexcasting.common.network.IMessage;
import at.petrak.hexcasting.forge.cap.CapSyncers; import at.petrak.hexcasting.forge.cap.CapSyncers;
import at.petrak.hexcasting.forge.cap.HexCapabilities; import at.petrak.hexcasting.forge.cap.HexCapabilities;
@ -81,15 +80,13 @@ public class ForgeXplatImpl implements IXplatAbstractions {
@Override @Override
public void brainsweep(Mob mob) { public void brainsweep(Mob mob) {
if (Brainsweeping.isValidTarget(mob)) { mob.getPersistentData().putBoolean(TAG_BRAINSWEPT, true);
mob.getPersistentData().putBoolean(TAG_BRAINSWEPT, true);
mob.removeFreeWill(); mob.removeFreeWill();
if (mob.level instanceof ServerLevel) { if (mob.level instanceof ServerLevel) {
ForgePacketHandler.getNetwork() ForgePacketHandler.getNetwork()
.send(PacketDistributor.TRACKING_ENTITY.with(() -> mob), MsgBrainsweepAck.of(mob)); .send(PacketDistributor.TRACKING_ENTITY.with(() -> mob), MsgBrainsweepAck.of(mob));
}
} }
} }