zombie villagers maintain brainswept status

including when cured
This commit is contained in:
yrsegal@gmail.com 2022-04-11 14:30:35 -04:00
parent 895e53e9db
commit 31ca7dca21
6 changed files with 58 additions and 25 deletions

View file

@ -23,7 +23,7 @@ object OpLastNToList : Operator {
val arg = stack.takeLast(1).getChecked<Double>(0)
val datum = stack[stack.lastIndex]
stack.removeLast()
if (arg < 0 || arg > stack.size || abs(arg.roundToInt() - arg) < 0.05f) {
if (arg < 0 || arg > stack.size || abs(arg.roundToInt() - arg) >= 0.05f) {
throw MishapInvalidIota(
datum,
0,

View file

@ -14,7 +14,7 @@ import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.npc.VillagerDataHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
@ -57,7 +57,7 @@ public class HexCapabilities {
new CapSentinel(false, false, Vec3.ZERO, Level.OVERWORLD));
evt.addCapability(new ResourceLocation(HexMod.MOD_ID, CapPreferredColorizer.CAP_NAME),
new CapPreferredColorizer(FrozenColorizer.DEFAULT));
} else if (evt.getObject() instanceof Villager) {
} else if (evt.getObject() instanceof VillagerDataHolder) {
evt.addCapability(new ResourceLocation(HexMod.MOD_ID, Brainsweeping.CAP_NAME),
new Brainsweeping.Cap());
}

View file

@ -5,7 +5,10 @@ import at.petrak.hexcasting.mixin.AccessorLivingEntity;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.npc.VillagerDataHolder;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
@ -34,21 +37,23 @@ public class Brainsweeping {
}
}
public static boolean isBrainswept(Villager villager) {
var maybeCap = villager.getCapability(HexCapabilities.BRAINSWEPT).resolve();
public static boolean isBrainswept(LivingEntity entity) {
var maybeCap = entity.getCapability(HexCapabilities.BRAINSWEPT).resolve();
return maybeCap.map(cap -> cap.brainswept).orElse(false);
}
public static void brainsweep(Villager villager) {
var maybeCap = villager.getCapability(HexCapabilities.BRAINSWEPT).resolve();
public static void brainsweep(LivingEntity entity) {
var maybeCap = entity.getCapability(HexCapabilities.BRAINSWEPT).resolve();
maybeCap.ifPresent(cap -> {
cap.brainswept = true;
var brain = villager.getBrain();
if (villager.level instanceof ServerLevel slevel) {
brain.stopAll(slevel, villager);
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());
}
((AccessorLivingEntity) villager).hex$SetBrain(brain.copyWithoutBehaviors());
});
}
@ -60,7 +65,13 @@ public class Brainsweeping {
}
@SubscribeEvent
public static void copyBrainsweepToZombie(LivingConversionEvent evt) {
public static void copyBrainsweepBetweenZombieAndVillager(LivingConversionEvent.Post evt) {
var outcome = evt.getOutcome();
var original = evt.getEntityLiving();
if (outcome instanceof VillagerDataHolder && original instanceof VillagerDataHolder) {
original.reviveCaps();
if (isBrainswept(original)) brainsweep(outcome);
original.invalidateCaps();
}
}
}

View file

@ -0,0 +1,30 @@
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
@Mixin(Mob.class)
public class MixinMob {
@Inject(method = "serverAiStep", at = @At("HEAD"), cancellable = true)
private void onRegisterBrainGoals(CallbackInfo ci) {
var self = (Mob) (Object) this;
if (Brainsweeping.isBrainswept(self)) {
ci.cancel();
}
}
@Inject(method = "getAmbientSound", at = @At("HEAD"), cancellable = true)
protected void onGetAmbientSound(CallbackInfoReturnable<SoundEvent> ci) {
var self = (Mob) (Object) this;
if (Brainsweeping.isBrainswept(self)) {
ci.setReturnValue(null);
}
}
}

View file

@ -1,13 +1,11 @@
package at.petrak.hexcasting.mixin;
import at.petrak.hexcasting.common.misc.Brainsweeping;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.npc.Villager;
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
@Mixin(Villager.class)
@ -19,12 +17,4 @@ public class MixinVillager {
ci.cancel();
}
}
@Inject(method = "getAmbientSound", at = @At("HEAD"), cancellable = true)
protected void onGetAmbientSound(CallbackInfoReturnable<SoundEvent> ci) {
var self = (Villager) (Object) this;
if (Brainsweeping.isBrainswept(self)) {
ci.setReturnValue(null);
}
}
}

View file

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