Files
src
build.gradle
gradle.properties
settings.gradle
Code
10 180
src/main/java/com/kodari/rubyfear/RubyFear.java
package com.kodari.rubyfear;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.core.Registry;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ToolMaterial;
public final class RubyFear implements ModInitializer {
private static final ResourceKey<Item> SWORD_KEY = ResourceKey.create(
Registries.ITEM, Identifier.fromNamespaceAndPath("rubyfear", "ruby_sword"));
private static final Item RUBY_SWORD = Registry.register(BuiltInRegistries.ITEM, SWORD_KEY,
new Item(new Item.Properties().setId(SWORD_KEY).sword(ToolMaterial.DIAMOND, 3.0F, -2.4F)));
private static final Map<PathfinderMob, Fear> FEAR = new HashMap<>();
private record Fear(AvoidEntityGoal<Player> goal, long expiresAt) {}
@Override
public void onInitialize() {
CreativeModeTabEvents.modifyOutputEvent(ResourceKey.create(Registries.CREATIVE_MODE_TAB,
Identifier.fromNamespaceAndPath("minecraft", "combat"))).register(output -> output.accept(RUBY_SWORD));
ServerLivingEntityEvents.AFTER_DEATH.register((victim, damage) -> {
if (!(victim instanceof Mob) || !(victim.level() instanceof ServerLevel level)
|| !(damage.getEntity() instanceof Player player) || damage.getDirectEntity() != player
|| player.getMainHandItem().getItem() != RUBY_SWORD) {
return;
}
boolean witnessed = false;
for (PathfinderMob mob : level.getEntitiesOfClass(PathfinderMob.class,
victim.getBoundingBox().inflate(12.0, 6.0, 12.0))) {
if (mob == victim || !mob.isAlive() || !mob.hasLineOfSight(victim)) {
continue;
}
Fear previous = FEAR.remove(mob);
if (previous != null) {
mob.getGoalSelector().removeGoal(previous.goal());
}
mob.setTarget(null);
AvoidEntityGoal<Player> goal = new AvoidEntityGoal<>(mob, Player.class,
24.0F, 1.2, 1.5, target -> target == player);
mob.getGoalSelector().addGoal(0, goal);
FEAR.put(mob, new Fear(goal, level.getServer().getTickCount() + 200L));
level.sendParticles(ParticleTypes.POOF, mob.getX(), mob.getY() + 1.0, mob.getZ(),
5, 0.3, 0.3, 0.3, 0.03);
witnessed = true;
}
if (witnessed) {
level.playSound(null, victim.getX(), victim.getY(), victim.getZ(),
SoundEvents.PLAYER_ATTACK_CRIT, SoundSource.PLAYERS, 1.0F, 0.7F);
}
});
ServerTickEvents.END_SERVER_TICK.register(server -> {
Iterator<Map.Entry<PathfinderMob, Fear>> iterator = FEAR.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<PathfinderMob, Fear> entry = iterator.next();
PathfinderMob mob = entry.getKey();
if (!mob.isAlive() || mob.isRemoved() || server.getTickCount() >= entry.getValue().expiresAt()) {
mob.getGoalSelector().removeGoal(entry.getValue().goal());
iterator.remove();
}
}
});
}
}