Files
src
build.gradle
gradle.properties
IMPORTANT.txt
settings.gradle
Code
42 909
src/main/java/com/kodari/supernatural/SupernaturalMod.java
package com.kodari.supernatural;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.portal.TeleportTransition;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
import net.minecraftforge.fml.common.Mod;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;
@Mod("supernatural")
public final class SupernaturalMod {
private static final ResourceKey<Level> HEAVEN = ResourceKey.create(Registries.DIMENSION, ResourceLocation.fromNamespaceAndPath("supernatural", "heaven"));
private static final ResourceKey<Level> HELL = ResourceKey.create(Registries.DIMENSION, ResourceLocation.fromNamespaceAndPath("supernatural", "hell"));
private static final List<String> SPELLS = List.of("heal", "ward", "smite", "hex", "exorcise", "blink",
"heavenly_passage", "infernal_passage", "fireball", "lightning", "speed", "invisibility",
"regenerate", "telekinesis", "drain", "soul_bind", "freeze");
private static ProfileManager profiles;
public SupernaturalMod() {
profiles = new ProfileManager();
MinecraftForge.EVENT_BUS.addListener(this::onPlayerLoggedIn);
MinecraftForge.EVENT_BUS.addListener(this::onLivingDeath);
MinecraftForge.EVENT_BUS.addListener(this::onRegisterCommands);
MinecraftForge.EVENT_BUS.addListener(this::onServerStopping);
}
private void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof ServerPlayer player)) {
return;
}
PlayerProfile profile = profiles.get(player.getUUID());
if (profile.getRace() == null) {
player.sendSystemMessage(Component.literal("Choose your Supernatural race with /sn choose, then /sn race <race>."));
}
}
private void onLivingDeath(LivingDeathEvent event) {
LivingEntity entity = event.getEntity();
if (entity instanceof ServerPlayer || !(event.getSource().getEntity() instanceof ServerPlayer killer)) {
return;
}
PlayerProfile profile = profiles.get(killer.getUUID());
profile.setKills(profile.getKills() + 1);
if (profile.getQuestStage() == 1) {
int questKills = Math.min(3, profile.getQuestKills() + 1);
profile.setQuestKills(questKills);
if (questKills == 3) {
profile.setQuestStage(2);
int questLevels = profiles.addXp(killer.getUUID(), 150);
killer.sendSystemMessage(Component.literal("Hunter's Call complete! You earned 150 XP."));
if (questLevels > 0) {
killer.sendSystemMessage(Component.literal("Level up! You are now level " + profile.getLevel() + "."));
}
} else {
killer.sendSystemMessage(Component.literal("Hunter's Call progress: " + questKills + "/3 monsters."));
}
}
advancePsychicQuest(killer, profile);
int levels = profiles.addXp(killer.getUUID(), 25);
killer.sendSystemMessage(Component.literal("+25 XP for the hunt."));
if (levels > 0) {
killer.sendSystemMessage(Component.literal("Level up! You are now level " + profile.getLevel() + "."));
}
profiles.save();
}
private void onRegisterCommands(RegisterCommandsEvent event) {
registerCommands(event.getDispatcher(), "sn");
registerCommands(event.getDispatcher(), "supernatural");
}
private void onServerStopping(ServerStoppingEvent event) {
profiles.save();
}
private static void registerCommands(CommandDispatcher<CommandSourceStack> dispatcher, String name) {
dispatcher.register(literal(name)
.executes(context -> showHelp(context.getSource()))
.then(literal("help").executes(context -> showHelp(context.getSource())))
.then(literal("choose").executes(context -> {
ServerPlayer player = context.getSource().getPlayerOrException();
player.sendSystemMessage(Component.literal("Available races: " + raceNames()));
player.sendSystemMessage(Component.literal("Select one with /sn race <race>."));
return 1;
}))
.then(literal("race").then(argument("race", StringArgumentType.word())
.suggests((context, builder) -> SharedSuggestionProvider.suggest(
Arrays.stream(Race.values()).map(race -> race.name().toLowerCase(Locale.ROOT)).toList(), builder))
.executes(context -> {
ServerPlayer player = context.getSource().getPlayerOrException();
Race race = Race.fromName(StringArgumentType.getString(context, "race"));
if (race == null) {
context.getSource().sendFailure(Component.literal("Unknown race. Available races: " + raceNames()));
return 0;
}
profiles.setRace(player.getUUID(), race);
player.sendSystemMessage(Component.literal("You chose the " + race.displayName() + " path."));
if (race == Race.DEMON) {
player.sendSystemMessage(Component.literal("Choose a rarity with /sn setdemon <black|red|white|yellow>."));
}
return 1;
})))
.then(literal("stats").executes(context -> showStats(context.getSource().getPlayerOrException())))
.then(literal("setdemon").then(argument("rarity", StringArgumentType.word())
.suggests((context, builder) -> SharedSuggestionProvider.suggest(
Arrays.stream(DemonRarity.values()).map(rarity -> rarity.name().toLowerCase(Locale.ROOT)).toList(), builder))
.executes(context -> {
ServerPlayer player = context.getSource().getPlayerOrException();
if (profiles.getRace(player.getUUID()) != Race.DEMON) {
context.getSource().sendFailure(Component.literal("Only demons can choose a demon rarity."));
return 0;
}
DemonRarity rarity = DemonRarity.fromName(StringArgumentType.getString(context, "rarity"));
profiles.setDemonRarity(player.getUUID(), rarity);
player.sendSystemMessage(Component.literal("Demon rarity set to " + rarity.displayName() + "."));
return 1;
})))
.then(literal("dimension").then(argument("realm", StringArgumentType.word())
.suggests((context, builder) -> SharedSuggestionProvider.suggest(List.of("heaven", "hell", "overworld"), builder))
.executes(context -> travel(context.getSource(), StringArgumentType.getString(context, "realm")))))
.then(literal("quest").executes(context -> showQuest(context.getSource().getPlayerOrException()))
.then(literal("start").executes(context -> startHuntQuest(context.getSource().getPlayerOrException())))
.then(literal("hellgate").executes(context -> startPsychicQuest(context.getSource().getPlayerOrException()))))
.then(literal("spells").executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("Spells: " + String.join(", ", SPELLS)), false);
return 1;
}))
.then(literal("cast").then(argument("spell", StringArgumentType.word())
.suggests((context, builder) -> SharedSuggestionProvider.suggest(SPELLS, builder))
.executes(context -> cast(context.getSource().getPlayerOrException(),
StringArgumentType.getString(context, "spell"))))));
}
private static int showHelp(CommandSourceStack source) {
source.sendSuccess(() -> Component.literal("Supernatural RPG: /sn choose, /sn race <race>, /sn stats, /sn quest, /sn spells, /sn cast <spell>, /sn dimension <heaven|hell|overworld>"), false);
return 1;
}
private static String raceNames() {
return String.join(", ", Arrays.stream(Race.values()).map(Race::displayName).toList());
}
private static int showStats(ServerPlayer player) {
PlayerProfile profile = profiles.get(player.getUUID());
Race race = profiles.getRace(player.getUUID());
String raceName = race == null ? "Unchosen" : race.displayName();
if (race == Race.DEMON) {
raceName += " (" + profiles.getDemonRarity(player.getUUID()).displayName() + ")";
}
player.sendSystemMessage(Component.literal("--- Supernatural Profile ---"));
player.sendSystemMessage(Component.literal("Race: " + raceName));
player.sendSystemMessage(Component.literal("Level: " + profile.getLevel() + " | XP: " + profile.getXp() + "/" + profiles.xpToNextLevel(profile.getLevel())));
player.sendSystemMessage(Component.literal("Monsters hunted: " + profile.getKills()));
return 1;
}
private static int travel(CommandSourceStack source, String realmName) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
String realm = realmName.toLowerCase(Locale.ROOT);
ServerLevel destination;
if (realm.equals("overworld")) {
destination = source.getServer().overworld();
} else if (realm.equals("heaven")) {
destination = source.getServer().getLevel(HEAVEN);
} else if (realm.equals("hell")) {
destination = source.getServer().getLevel(HELL);
} else {
source.sendFailure(Component.literal("Choose heaven, hell, or overworld."));
return 0;
}
if (destination == null) {