Implement commands

This commit is contained in:
2025-11-09 13:49:28 +01:00
parent d655f6a25d
commit 28a0327363
4 changed files with 130 additions and 13 deletions
@@ -1,8 +1,10 @@
package net.fretux.ascend; package net.fretux.ascend;
import net.fretux.ascend.command.AscendCommand;
import net.fretux.ascend.config.AscendConfig; import net.fretux.ascend.config.AscendConfig;
import net.fretux.ascend.network.PacketHandler; import net.fretux.ascend.network.PacketHandler;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig; import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
@@ -28,4 +30,9 @@ public class AscendMod {
System.out.println("[Ascend] Registering packets... Side=" + FMLEnvironment.dist); System.out.println("[Ascend] Registering packets... Side=" + FMLEnvironment.dist);
}); });
} }
@SubscribeEvent
public static void onRegisterCommands(net.minecraftforge.event.RegisterCommandsEvent event) {
AscendCommand.register(event.getDispatcher());
}
} }
@@ -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<CommandSourceStack> 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<CommandSourceStack> 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<CommandSourceStack> 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<CommandSourceStack> 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<CommandSourceStack> 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())
);
}
}
@@ -12,7 +12,9 @@ import net.minecraftforge.fml.common.Mod;
@Mod.EventBusSubscriber(modid = AscendMod.MODID) @Mod.EventBusSubscriber(modid = AscendMod.MODID)
public class WeaponMagicScalingEvents { public class WeaponMagicScalingEvents {
static double scale = AscendConfig.COMMON.attributeScalingMultiplier.get(); private static double getScaling() {
return AscendConfig.COMMON.attributeScalingMultiplier.get();
}
@SubscribeEvent @SubscribeEvent
public static void onLivingHurt(LivingHurtEvent event) { public static void onLivingHurt(LivingHurtEvent event) {
@@ -27,7 +29,7 @@ public class WeaponMagicScalingEvents {
if (magicScaling > 0) { if (magicScaling > 0) {
double perPoint = 0.005d; double perPoint = 0.005d;
double maxBonus = 0.40d; 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); modified = (float) (modified * bonusMult);
} }
} else { } else {
@@ -46,7 +48,7 @@ public class WeaponMagicScalingEvents {
key.equals("medium_scaling") ? 0.004d : key.equals("medium_scaling") ? 0.004d :
0.005d; 0.005d;
double maxBonus = 0.40d; 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); modified = (float) (modified * bonusMult);
} }
} }
@@ -26,7 +26,9 @@ public final class StatEffects {
private 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) { public static void applyAll(Player player) {
player.getCapability(PlayerStatsProvider.PLAYER_STATS).ifPresent(stats -> { player.getCapability(PlayerStatsProvider.PLAYER_STATS).ifPresent(stats -> {
@@ -41,12 +43,11 @@ public final class StatEffects {
} }
public static void applyStrength(Player player, int strengthLevel) { public static void applyStrength(Player player, int strengthLevel) {
// Damage
AttributeInstance atk = player.getAttribute(Attributes.ATTACK_DAMAGE); AttributeInstance atk = player.getAttribute(Attributes.ATTACK_DAMAGE);
if (atk != null) { if (atk != null) {
atk.removeModifier(STRENGTH_DAMAGE_UUID); atk.removeModifier(STRENGTH_DAMAGE_UUID);
if (strengthLevel > 0) { if (strengthLevel > 0) {
double dmgBonus = strengthLevel * 0.1d * scale; double dmgBonus = strengthLevel * 0.1d * getScaling();
atk.addTransientModifier(new AttributeModifier( atk.addTransientModifier(new AttributeModifier(
STRENGTH_DAMAGE_UUID, STRENGTH_DAMAGE_UUID,
"Ascend Strength damage bonus", "Ascend Strength damage bonus",
@@ -61,7 +62,7 @@ public final class StatEffects {
if (kb != null) { if (kb != null) {
kb.removeModifier(STRENGTH_KNOCKBACK_UUID); kb.removeModifier(STRENGTH_KNOCKBACK_UUID);
if (strengthLevel > 0) { if (strengthLevel > 0) {
double kbBonus = strengthLevel * 0.03d * scale; double kbBonus = strengthLevel * 0.03d * getScaling();
kb.addTransientModifier(new AttributeModifier( kb.addTransientModifier(new AttributeModifier(
STRENGTH_KNOCKBACK_UUID, STRENGTH_KNOCKBACK_UUID,
"Ascend Strength knockback bonus", "Ascend Strength knockback bonus",
@@ -77,7 +78,7 @@ public final class StatEffects {
if (move != null) { if (move != null) {
move.removeModifier(AGILITY_SPEED_UUID); move.removeModifier(AGILITY_SPEED_UUID);
if (agilityLevel > 0) { if (agilityLevel > 0) {
double bonus = agilityLevel * 0.002d * scale; double bonus = agilityLevel * 0.002d * getScaling();
move.addTransientModifier(new AttributeModifier( move.addTransientModifier(new AttributeModifier(
AGILITY_SPEED_UUID, AGILITY_SPEED_UUID,
"Ascend Agility speed bonus", "Ascend Agility speed bonus",
@@ -93,7 +94,7 @@ public final class StatEffects {
if (hp != null) { if (hp != null) {
hp.removeModifier(FORTITUDE_HEALTH_UUID); hp.removeModifier(FORTITUDE_HEALTH_UUID);
if (fortitudeLevel > 0) { if (fortitudeLevel > 0) {
double healthBonus = fortitudeLevel * 0.25d * scale; double healthBonus = fortitudeLevel * 0.25d * getScaling();
hp.addTransientModifier(new AttributeModifier( hp.addTransientModifier(new AttributeModifier(
FORTITUDE_HEALTH_UUID, FORTITUDE_HEALTH_UUID,
"Ascend Fortitude health bonus", "Ascend Fortitude health bonus",
@@ -106,7 +107,7 @@ public final class StatEffects {
if (kbRes != null) { if (kbRes != null) {
kbRes.removeModifier(FORTITUDE_KB_RESIST_UUID); kbRes.removeModifier(FORTITUDE_KB_RESIST_UUID);
if (fortitudeLevel > 0) { 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( kbRes.addTransientModifier(new AttributeModifier(
FORTITUDE_KB_RESIST_UUID, FORTITUDE_KB_RESIST_UUID,
"Ascend Fortitude knockback resist", "Ascend Fortitude knockback resist",
@@ -119,13 +120,13 @@ public final class StatEffects {
public static float getWillpowerSanityMultiplier(int willpowerLevel) { public static float getWillpowerSanityMultiplier(int willpowerLevel) {
if (willpowerLevel <= 0) return 1.0f; 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); return (float) (1.0d - reduction);
} }
public static float getWillpowerTempoMultiplier(int willpowerLevel) { public static float getWillpowerTempoMultiplier(int willpowerLevel) {
if (willpowerLevel <= 0) return 1.0f; 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); return (float) (1.0d + bonus);
} }
@@ -149,7 +150,7 @@ public final class StatEffects {
if (charismaLevel <= 0) return 0.0d; if (charismaLevel <= 0) return 0.0d;
double perPoint = 0.01d; double perPoint = 0.01d;
double max = 0.30d; double max = 0.30d;
return Math.min(charismaLevel * perPoint * scale, max); return Math.min(charismaLevel * perPoint * getScaling(), max);
} }
public static double getWeaponScalingMultiplier(int scalingLevel) { public static double getWeaponScalingMultiplier(int scalingLevel) {