From 28a032736393716c71a7c2498e11077e7482564c Mon Sep 17 00:00:00 2001 From: fretuks Date: Sun, 9 Nov 2025 13:49:28 +0100 Subject: [PATCH] Implement commands --- .../java/net/fretux/ascend/AscendMod.java | 7 ++ .../fretux/ascend/command/AscendCommand.java | 107 ++++++++++++++++++ .../event/WeaponMagicScalingEvents.java | 8 +- .../net/fretux/ascend/player/StatEffects.java | 21 ++-- 4 files changed, 130 insertions(+), 13 deletions(-) create mode 100644 src/main/java/net/fretux/ascend/command/AscendCommand.java diff --git a/src/main/java/net/fretux/ascend/AscendMod.java b/src/main/java/net/fretux/ascend/AscendMod.java index 13118f8..9c351cc 100644 --- a/src/main/java/net/fretux/ascend/AscendMod.java +++ b/src/main/java/net/fretux/ascend/AscendMod.java @@ -1,8 +1,10 @@ package net.fretux.ascend; +import net.fretux.ascend.command.AscendCommand; import net.fretux.ascend.config.AscendConfig; import net.fretux.ascend.network.PacketHandler; import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.config.ModConfig; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; @@ -28,4 +30,9 @@ public class AscendMod { System.out.println("[Ascend] Registering packets... Side=" + FMLEnvironment.dist); }); } + + @SubscribeEvent + public static void onRegisterCommands(net.minecraftforge.event.RegisterCommandsEvent event) { + AscendCommand.register(event.getDispatcher()); + } } \ No newline at end of file diff --git a/src/main/java/net/fretux/ascend/command/AscendCommand.java b/src/main/java/net/fretux/ascend/command/AscendCommand.java new file mode 100644 index 0000000..ea4e98e --- /dev/null +++ b/src/main/java/net/fretux/ascend/command/AscendCommand.java @@ -0,0 +1,107 @@ +package net.fretux.ascend.command; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.fretux.ascend.player.PlayerStats; +import net.fretux.ascend.player.PlayerStatsProvider; +import net.fretux.ascend.player.StatEffects; +import net.fretux.ascend.network.PacketHandler; +import net.fretux.ascend.network.ClientboundSyncStatsPacket; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.commands.arguments.EntityArgument; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.PacketDistributor; + +public class AscendCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register( + Commands.literal("ascend") + .requires(src -> src.hasPermission(2)) + .then(Commands.literal("get") + .then(Commands.argument("player", EntityArgument.player()) + .executes(AscendCommand::getStats))) + .then(Commands.literal("set") + .then(Commands.argument("player", EntityArgument.player()) + .then(Commands.argument("attribute", StringArgumentType.string()) + .then(Commands.argument("value", IntegerArgumentType.integer(0)) + .executes(AscendCommand::setAttribute))))) + .then(Commands.literal("addxp") + .then(Commands.argument("player", EntityArgument.player()) + .then(Commands.argument("amount", IntegerArgumentType.integer(1)) + .executes(AscendCommand::addXP)))) + .then(Commands.literal("reset") + .then(Commands.argument("player", EntityArgument.player()) + .executes(AscendCommand::resetStats))) + ); + } + + private static int getStats(CommandContext ctx) throws CommandSyntaxException { + ServerPlayer target = EntityArgument.getPlayer(ctx, "player"); + target.getCapability(PlayerStatsProvider.PLAYER_STATS).ifPresent(stats -> { + ctx.getSource().sendSuccess(() -> Component.literal("=== Ascend Stats for " + target.getName().getString() + " ==="), false); + ctx.getSource().sendSuccess(() -> + Component.literal("Level: " + stats.getAscendLevel() + " | XP: " + stats.getAscendXP() + "/" + stats.getXPToNextAscendLevel()), false); + ctx.getSource().sendSuccess(() -> + Component.literal("Unspent Points: " + stats.getUnspentPoints()), false); + CompoundTag attrTag = stats.serializeNBT().getCompound("Attributes"); + for (String key : attrTag.getAllKeys()) { + int val = attrTag.getInt(key); + ctx.getSource().sendSuccess(() -> Component.literal(" - " + key + ": " + val), false); + } + }); + return 1; + } + + private static int setAttribute(CommandContext ctx) throws CommandSyntaxException { + ServerPlayer target = EntityArgument.getPlayer(ctx, "player"); + String attr = StringArgumentType.getString(ctx, "attribute"); + int value = IntegerArgumentType.getInteger(ctx, "value"); + target.getCapability(PlayerStatsProvider.PLAYER_STATS).ifPresent(stats -> { + stats.setAttributeLevel(attr, value); + StatEffects.applyAll(target); + sync(target, stats); + ctx.getSource().sendSuccess(() -> + Component.literal("Set " + target.getName().getString() + "'s " + attr + " to " + value), true); + }); + return 1; + } + + private static int addXP(CommandContext ctx) throws CommandSyntaxException { + ServerPlayer target = EntityArgument.getPlayer(ctx, "player"); + int amount = IntegerArgumentType.getInteger(ctx, "amount"); + target.getCapability(PlayerStatsProvider.PLAYER_STATS).ifPresent(stats -> { + stats.addAscendXP(amount); + StatEffects.applyAll(target); + sync(target, stats); + ctx.getSource().sendSuccess(() -> + Component.literal("Added " + amount + " Ascend XP to " + target.getName().getString()), true); + }); + return 1; + } + + private static int resetStats(CommandContext ctx) throws CommandSyntaxException { + ServerPlayer target = EntityArgument.getPlayer(ctx, "player"); + target.getCapability(PlayerStatsProvider.PLAYER_STATS).ifPresent(stats -> { + PlayerStats fresh = new PlayerStats(); + stats.deserializeNBT(fresh.serializeNBT()); + StatEffects.applyAll(target); + sync(target, stats); + ctx.getSource().sendSuccess(() -> + Component.literal("Reset " + target.getName().getString() + "'s Ascend stats."), true); + }); + return 1; + } + + private static void sync(ServerPlayer player, PlayerStats stats) { + PacketHandler.INSTANCE.send( + PacketDistributor.PLAYER.with(() -> player), + new ClientboundSyncStatsPacket(stats.serializeNBT()) + ); + } +} diff --git a/src/main/java/net/fretux/ascend/event/WeaponMagicScalingEvents.java b/src/main/java/net/fretux/ascend/event/WeaponMagicScalingEvents.java index bd957db..698b523 100644 --- a/src/main/java/net/fretux/ascend/event/WeaponMagicScalingEvents.java +++ b/src/main/java/net/fretux/ascend/event/WeaponMagicScalingEvents.java @@ -12,7 +12,9 @@ import net.minecraftforge.fml.common.Mod; @Mod.EventBusSubscriber(modid = AscendMod.MODID) public class WeaponMagicScalingEvents { - static double scale = AscendConfig.COMMON.attributeScalingMultiplier.get(); + private static double getScaling() { + return AscendConfig.COMMON.attributeScalingMultiplier.get(); + } @SubscribeEvent public static void onLivingHurt(LivingHurtEvent event) { @@ -27,7 +29,7 @@ public class WeaponMagicScalingEvents { if (magicScaling > 0) { double perPoint = 0.005d; double maxBonus = 0.40d; - double bonusMult = 1.0d + Math.min(magicScaling * perPoint * scale, maxBonus); + double bonusMult = 1.0d + Math.min(magicScaling * perPoint * getScaling(), maxBonus); modified = (float) (modified * bonusMult); } } else { @@ -46,7 +48,7 @@ public class WeaponMagicScalingEvents { key.equals("medium_scaling") ? 0.004d : 0.005d; double maxBonus = 0.40d; - double bonusMult = 1.0d + Math.min(level * perPoint * scale, maxBonus); + double bonusMult = 1.0d + Math.min(level * perPoint * getScaling(), maxBonus); modified = (float) (modified * bonusMult); } } diff --git a/src/main/java/net/fretux/ascend/player/StatEffects.java b/src/main/java/net/fretux/ascend/player/StatEffects.java index 0a2ba62..ad3b45c 100644 --- a/src/main/java/net/fretux/ascend/player/StatEffects.java +++ b/src/main/java/net/fretux/ascend/player/StatEffects.java @@ -26,7 +26,9 @@ public final class StatEffects { private StatEffects() {} - static double scale = AscendConfig.COMMON.attributeScalingMultiplier.get(); + private static double getScaling() { + return AscendConfig.COMMON.attributeScalingMultiplier.get(); + } public static void applyAll(Player player) { player.getCapability(PlayerStatsProvider.PLAYER_STATS).ifPresent(stats -> { @@ -41,12 +43,11 @@ public final class StatEffects { } public static void applyStrength(Player player, int strengthLevel) { - // Damage AttributeInstance atk = player.getAttribute(Attributes.ATTACK_DAMAGE); if (atk != null) { atk.removeModifier(STRENGTH_DAMAGE_UUID); if (strengthLevel > 0) { - double dmgBonus = strengthLevel * 0.1d * scale; + double dmgBonus = strengthLevel * 0.1d * getScaling(); atk.addTransientModifier(new AttributeModifier( STRENGTH_DAMAGE_UUID, "Ascend Strength damage bonus", @@ -61,7 +62,7 @@ public final class StatEffects { if (kb != null) { kb.removeModifier(STRENGTH_KNOCKBACK_UUID); if (strengthLevel > 0) { - double kbBonus = strengthLevel * 0.03d * scale; + double kbBonus = strengthLevel * 0.03d * getScaling(); kb.addTransientModifier(new AttributeModifier( STRENGTH_KNOCKBACK_UUID, "Ascend Strength knockback bonus", @@ -77,7 +78,7 @@ public final class StatEffects { if (move != null) { move.removeModifier(AGILITY_SPEED_UUID); if (agilityLevel > 0) { - double bonus = agilityLevel * 0.002d * scale; + double bonus = agilityLevel * 0.002d * getScaling(); move.addTransientModifier(new AttributeModifier( AGILITY_SPEED_UUID, "Ascend Agility speed bonus", @@ -93,7 +94,7 @@ public final class StatEffects { if (hp != null) { hp.removeModifier(FORTITUDE_HEALTH_UUID); if (fortitudeLevel > 0) { - double healthBonus = fortitudeLevel * 0.25d * scale; + double healthBonus = fortitudeLevel * 0.25d * getScaling(); hp.addTransientModifier(new AttributeModifier( FORTITUDE_HEALTH_UUID, "Ascend Fortitude health bonus", @@ -106,7 +107,7 @@ public final class StatEffects { if (kbRes != null) { kbRes.removeModifier(FORTITUDE_KB_RESIST_UUID); if (fortitudeLevel > 0) { - double res = Math.min(0.006d * fortitudeLevel * scale, 0.60d); + double res = Math.min(0.006d * fortitudeLevel * getScaling(), 0.60d); kbRes.addTransientModifier(new AttributeModifier( FORTITUDE_KB_RESIST_UUID, "Ascend Fortitude knockback resist", @@ -119,13 +120,13 @@ public final class StatEffects { public static float getWillpowerSanityMultiplier(int willpowerLevel) { if (willpowerLevel <= 0) return 1.0f; - double reduction = Math.min(willpowerLevel * 0.005d * scale, 0.5d); + double reduction = Math.min(willpowerLevel * 0.005d * getScaling(), 0.5d); return (float) (1.0d - reduction); } public static float getWillpowerTempoMultiplier(int willpowerLevel) { if (willpowerLevel <= 0) return 1.0f; - double bonus = Math.min(willpowerLevel * 0.004d * scale, 0.4d); // up to +40% + double bonus = Math.min(willpowerLevel * 0.004d * getScaling(), 0.4d); return (float) (1.0d + bonus); } @@ -149,7 +150,7 @@ public final class StatEffects { if (charismaLevel <= 0) return 0.0d; double perPoint = 0.01d; double max = 0.30d; - return Math.min(charismaLevel * perPoint * scale, max); + return Math.min(charismaLevel * perPoint * getScaling(), max); } public static double getWeaponScalingMultiplier(int scalingLevel) {