Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 230
src/main/java/com/kodari/manhunt/ManhuntMod.java
package com.kodari.manhunt;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.LodestoneTrackerComponent;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.boss.dragon.EnderDragonEntity;
import net.minecraft.entity.mob.HuskEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.math.GlobalPos;
import java.util.Optional;
import java.util.UUID;
public class ManhuntMod implements ModInitializer {
private static UUID activePlayerUuid;
private static ServerPlayerEntity activePlayer;
private static HuskEntity activeHunter;
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
dispatcher.register(CommandManager.literal("manhunt")
.then(CommandManager.literal("start").executes(context -> {
ServerPlayerEntity player = context.getSource().getPlayerOrThrow();
if (activeHunter != null && !activeHunter.isRemoved()) {
activeHunter.discard();
}
activePlayerUuid = player.getUuid();
activePlayer = player;
activeHunter = spawnHunter((ServerWorld) player.getEntityWorld(), player);
if (activeHunter == null) {
activePlayerUuid = null;
activePlayer = null;
player.sendMessage(Text.literal("Could not spawn the Hunter."), false);
return 0;
}
player.sendMessage(Text.literal("Manhunt started! The Hunter is after you."), false);
return 1;
}))));
ServerTickEvents.END_SERVER_TICK.register(ManhuntMod::tickHunter);
ServerLivingEntityEvents.AFTER_DEATH.register(ManhuntMod::afterDeath);
}
private static HuskEntity spawnHunter(ServerWorld world, ServerPlayerEntity player) {
HuskEntity hunter = EntityType.HUSK.create(world, SpawnReason.COMMAND);
if (hunter == null) {
return null;
}
hunter.refreshPositionAndAngles(player.getX() + 5.0, player.getY(), player.getZ() + 5.0, 0.0F, 0.0F);
hunter.setCustomName(Text.literal("Hunter"));
hunter.setCustomNameVisible(true);
hunter.setPersistent();
hunter.setTarget(player);
EntityAttributeInstance movementSpeed = hunter.getAttributeInstance(EntityAttributes.MOVEMENT_SPEED);
if (movementSpeed != null) {
movementSpeed.setBaseValue(0.32);
}
hunter.equipStack(EquipmentSlot.MAINHAND, new ItemStack(Items.IRON_SWORD));
ItemStack compass = new ItemStack(Items.COMPASS);
updateCompass(compass, world, player);
hunter.equipStack(EquipmentSlot.OFFHAND, compass);
return world.spawnEntity(hunter) ? hunter : null;
}
private static void tickHunter(MinecraftServer server) {
if (activePlayerUuid == null || activeHunter == null || activeHunter.isRemoved()) {
return;
}
ServerPlayerEntity player = server.getPlayerManager().getPlayer(activePlayerUuid);
if (player == null || !player.isAlive()) {
return;
}
activePlayer = player;
ServerWorld playerWorld = (ServerWorld) player.getEntityWorld();
if (activeHunter.getEntityWorld() != playerWorld) {
activeHunter.discard();
activeHunter = spawnHunter(playerWorld, player);
if (activeHunter == null) {
return;
}
}
activeHunter.setTarget(player);
activeHunter.setSprinting(true);
updateCompass(activeHunter.getEquippedStack(EquipmentSlot.OFFHAND), playerWorld, player);
}
private static void updateCompass(ItemStack compass, ServerWorld world, ServerPlayerEntity player) {
compass.set(DataComponentTypes.LODESTONE_TRACKER, new LodestoneTrackerComponent(
Optional.of(GlobalPos.create(world.getRegistryKey(), player.getBlockPos())), true));
}
private static void afterDeath(LivingEntity entity, net.minecraft.entity.damage.DamageSource damageSource) {
if (entity instanceof ServerPlayerEntity player
&& activePlayerUuid != null
&& activePlayerUuid.equals(player.getUuid())) {
player.sendMessage(Text.literal("Hunter Wins!"), false);
endHunt();
} else if (entity instanceof EnderDragonEntity
&& damageSource.getAttacker() instanceof ServerPlayerEntity killer
&& activePlayerUuid != null
&& activePlayerUuid.equals(killer.getUuid())
&& activePlayer != null
&& activePlayer.isAlive()) {
activePlayer.sendMessage(Text.literal("Speedrunner Wins!"), false);
endHunt();
}
}
private static void endHunt() {
if (activeHunter != null && !activeHunter.isRemoved()) {
activeHunter.discard();
}
activeHunter = null;
activePlayer = null;
activePlayerUuid = null;
}
}