From 2def0dec3c277c2fad437afc2897a816aaf7584f Mon Sep 17 00:00:00 2001 From: f1den Date: Sun, 12 Jul 2026 02:07:00 +0300 Subject: [PATCH] Sloped test commands system --- .../nightly/sablegravity/SableGravityMod.java | 230 ++++++++++++++---- 1 file changed, 177 insertions(+), 53 deletions(-) diff --git a/src/main/java/dev/nightly/sablegravity/SableGravityMod.java b/src/main/java/dev/nightly/sablegravity/SableGravityMod.java index a309a25..7fd645e 100644 --- a/src/main/java/dev/nightly/sablegravity/SableGravityMod.java +++ b/src/main/java/dev/nightly/sablegravity/SableGravityMod.java @@ -2,6 +2,8 @@ package dev.nightly.sablegravity; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.DoubleArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.suggestion.SuggestionProvider; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.commands.arguments.EntityArgument; @@ -15,13 +17,28 @@ import net.neoforged.neoforge.event.RegisterCommandsEvent; import java.util.Collection; import java.util.List; +import java.util.Locale; +import java.util.Map; @Mod(SableGravityMod.MOD_ID) public final class SableGravityMod { public static final String MOD_ID = "sablegravity"; public static final float VANILLA_STRENGTH = 9.8F; + private static volatile float globalStrength = VANILLA_STRENGTH; + private static final Map TEST_FUNCTIONS = Map.of( + "axistestb", SableGravityMod::axisTestB + ); + + private static final SuggestionProvider TEST_SUGGESTIONS = + (context, builder) -> { + TEST_FUNCTIONS.keySet().stream() + .sorted() + .forEach(builder::suggest); + return builder.buildFuture(); + }; + public SableGravityMod(ModContainer container, IEventBus modBus) { NeoForge.EVENT_BUS.addListener(this::registerCommands); } @@ -32,94 +49,201 @@ public final class SableGravityMod { private static void register(CommandDispatcher dispatcher) { dispatcher.register( - Commands.literal("sablegravity") - .requires(source -> source.hasPermission(2)) - .then(Commands.literal("strength") - .executes(context -> showStrength(context.getSource())) - .then(Commands.argument("value", DoubleArgumentType.doubleArg(0.0D, 100.0D)) - .executes(context -> setStrength( - context.getSource(), - (float) DoubleArgumentType.getDouble(context, "value") - )) - ) - ) - .then(Commands.literal("enable") - .executes(context -> setEnabled( - context.getSource(), - List.of(context.getSource().getPlayerOrException()), - true - )) - .then(Commands.argument("targets", EntityArgument.players()) - .executes(context -> setEnabled( - context.getSource(), - EntityArgument.getPlayers(context, "targets"), - true - )) - ) - ) - .then(Commands.literal("disable") - .executes(context -> setEnabled( - context.getSource(), - List.of(context.getSource().getPlayerOrException()), - false - )) - .then(Commands.argument("targets", EntityArgument.players()) - .executes(context -> setEnabled( - context.getSource(), - EntityArgument.getPlayers(context, "targets"), - false - )) - ) - ) + Commands.literal("sablegravity") + .requires(source -> source.hasPermission(2)) + + .then(Commands.literal("strength") + .executes(context -> showStrength(context.getSource())) + .then(Commands.argument("value", DoubleArgumentType.doubleArg(0.0D, 100.0D)) + .executes(context -> setStrength( + context.getSource(), + (float) DoubleArgumentType.getDouble(context, "value") + )) + ) + ) + + .then(Commands.literal("test") + .executes(context -> listTestFunctions(context.getSource())) + .then(Commands.argument("function", StringArgumentType.word()) + .suggests(TEST_SUGGESTIONS) + .executes(context -> runTestFunction( + context.getSource(), + StringArgumentType.getString(context, "function") + )) + ) + ) + + .then(Commands.literal("enable") + .executes(context -> setEnabled( + context.getSource(), + List.of(context.getSource().getPlayerOrException()), + true + )) + .then(Commands.argument("targets", EntityArgument.players()) + .executes(context -> setEnabled( + context.getSource(), + EntityArgument.getPlayers(context, "targets"), + true + )) + ) + ) + + .then(Commands.literal("disable") + .executes(context -> setEnabled( + context.getSource(), + List.of(context.getSource().getPlayerOrException()), + false + )) + .then(Commands.argument("targets", EntityArgument.players()) + .executes(context -> setEnabled( + context.getSource(), + EntityArgument.getPlayers(context, "targets"), + false + )) + ) + ) ); } private static int showStrength(CommandSourceStack source) { source.sendSuccess( - () -> Component.literal("Sable gravity strength: " + format(globalStrength) + " m/s²"), - false + () -> Component.literal( + "Sable gravity strength: " + format(globalStrength) + " m/s²" + ), + false ); + return 1; } private static int setStrength(CommandSourceStack source, float strength) { globalStrength = strength; + for (ServerPlayer player : source.getServer().getPlayerList().getPlayers()) { SableGravityState state = (SableGravityState) player; state.sablegravity$setStrength(strength); } + source.sendSuccess( - () -> Component.literal( - "Sable gravity strength set to " + format(strength) + " m/s²" - + (strength == VANILLA_STRENGTH ? " (vanilla)" : "") - ), - true + () -> Component.literal( + "Sable gravity strength set to " + format(strength) + " m/s²" + + (strength == VANILLA_STRENGTH ? " (vanilla)" : "") + ), + true ); + return 1; } - private static int setEnabled(CommandSourceStack source, Collection targets, boolean enabled) { + private static int setEnabled( + CommandSourceStack source, + Collection targets, + boolean enabled + ) { for (ServerPlayer player : targets) { SableGravityState state = (SableGravityState) player; + state.sablegravity$setStrength(globalStrength); state.sablegravity$setEnabled(enabled); state.sablegravity$setLocalStateValid(false); } + int count = targets.size(); + source.sendSuccess( - () -> Component.literal( - "Sable gravity " + (enabled ? "enabled" : "disabled") - + " for " + count + " player" + (count == 1 ? "" : "s") - ), - true + () -> Component.literal( + "Sable gravity " + (enabled ? "enabled" : "disabled") + + " for " + count + " player" + (count == 1 ? "" : "s") + ), + true ); + return count; } + private static int listTestFunctions(CommandSourceStack source) { + String functions = String.join( + ", ", + TEST_FUNCTIONS.keySet().stream().sorted().toList() + ); + + source.sendSuccess( + () -> Component.literal("Available test functions: " + functions), + false + ); + + return TEST_FUNCTIONS.size(); + } + + private static int runTestFunction(CommandSourceStack source, String functionName) { + String normalizedName = functionName.toLowerCase(Locale.ROOT); + TestFunction function = TEST_FUNCTIONS.get(normalizedName); + + if (function == null) { + String functions = String.join( + ", ", + TEST_FUNCTIONS.keySet().stream().sorted().toList() + ); + + source.sendFailure( Component.literal("Unknown test function: " + functionName + ". Available: " + functions) ); + + return 0; + } + + try { + function.run(source); + + source.sendSuccess( + () -> Component.literal("Executed test function: " + normalizedName), + false + ); + + return 1; + } catch (Exception exception) { + source.sendFailure( + Component.literal( + "Test function " + normalizedName + " failed: " + + exception.getClass().getSimpleName() + + ": " + String.valueOf(exception.getMessage()) + ) + ); + + exception.printStackTrace(); + return 0; + } + } + + private static void axisTestB(CommandSourceStack source) { + try { + ServerPlayer player = source.getPlayer(); + + if (player == null) { + source.sendFailure(Component.literal("This test must be executed by a player")); + return; + } + + player.getBoundingBox(); + // Тут вызываешь свою void-функцию: + // + // SomeClass.axisTestB(player); + // someVoidFunction(); + + source.sendSuccess(() -> Component.literal("Axis test B executed"),false ); + } catch (Exception e) { + source.sendFailure(Component.literal("ERROR:\n " + e.toString())); + } + } + private static String format(float value) { if (value == (long) value) { return Long.toString((long) value); } + return Float.toString(value); } -} + + @FunctionalInterface + private interface TestFunction { + void run(CommandSourceStack source) throws Exception; + } +} \ No newline at end of file