Sloped test commands system

This commit is contained in:
f1den
2026-07-12 02:07:00 +03:00
parent ba22b12998
commit e1aa22858e
@@ -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<String, TestFunction> TEST_FUNCTIONS = Map.of(
"axistestb", SableGravityMod::axisTestB
);
private static final SuggestionProvider<CommandSourceStack> 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<CommandSourceStack> 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<ServerPlayer> targets, boolean enabled) {
private static int setEnabled(
CommandSourceStack source,
Collection<ServerPlayer> 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;
}
}