Sloped test commands system
This commit is contained in:
1 parent
55213cb5aa
commit
2def0dec3c
1 file changed
+177
-53
@@ -2,6 +2,8 @@ package dev.nightly.sablegravity;
|
|||||||
|
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
import com.mojang.brigadier.arguments.DoubleArgumentType;
|
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.CommandSourceStack;
|
||||||
import net.minecraft.commands.Commands;
|
import net.minecraft.commands.Commands;
|
||||||
import net.minecraft.commands.arguments.EntityArgument;
|
import net.minecraft.commands.arguments.EntityArgument;
|
||||||
@@ -15,13 +17,28 @@ import net.neoforged.neoforge.event.RegisterCommandsEvent;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Mod(SableGravityMod.MOD_ID)
|
@Mod(SableGravityMod.MOD_ID)
|
||||||
public final class SableGravityMod {
|
public final class SableGravityMod {
|
||||||
public static final String MOD_ID = "sablegravity";
|
public static final String MOD_ID = "sablegravity";
|
||||||
public static final float VANILLA_STRENGTH = 9.8F;
|
public static final float VANILLA_STRENGTH = 9.8F;
|
||||||
|
|
||||||
private static volatile float globalStrength = VANILLA_STRENGTH;
|
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) {
|
public SableGravityMod(ModContainer container, IEventBus modBus) {
|
||||||
NeoForge.EVENT_BUS.addListener(this::registerCommands);
|
NeoForge.EVENT_BUS.addListener(this::registerCommands);
|
||||||
}
|
}
|
||||||
@@ -32,94 +49,201 @@ public final class SableGravityMod {
|
|||||||
|
|
||||||
private static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
|
private static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
|
||||||
dispatcher.register(
|
dispatcher.register(
|
||||||
Commands.literal("sablegravity")
|
Commands.literal("sablegravity")
|
||||||
.requires(source -> source.hasPermission(2))
|
.requires(source -> source.hasPermission(2))
|
||||||
.then(Commands.literal("strength")
|
|
||||||
.executes(context -> showStrength(context.getSource()))
|
.then(Commands.literal("strength")
|
||||||
.then(Commands.argument("value", DoubleArgumentType.doubleArg(0.0D, 100.0D))
|
.executes(context -> showStrength(context.getSource()))
|
||||||
.executes(context -> setStrength(
|
.then(Commands.argument("value", DoubleArgumentType.doubleArg(0.0D, 100.0D))
|
||||||
context.getSource(),
|
.executes(context -> setStrength(
|
||||||
(float) DoubleArgumentType.getDouble(context, "value")
|
context.getSource(),
|
||||||
))
|
(float) DoubleArgumentType.getDouble(context, "value")
|
||||||
)
|
))
|
||||||
)
|
)
|
||||||
.then(Commands.literal("enable")
|
)
|
||||||
.executes(context -> setEnabled(
|
|
||||||
context.getSource(),
|
.then(Commands.literal("test")
|
||||||
List.of(context.getSource().getPlayerOrException()),
|
.executes(context -> listTestFunctions(context.getSource()))
|
||||||
true
|
.then(Commands.argument("function", StringArgumentType.word())
|
||||||
))
|
.suggests(TEST_SUGGESTIONS)
|
||||||
.then(Commands.argument("targets", EntityArgument.players())
|
.executes(context -> runTestFunction(
|
||||||
.executes(context -> setEnabled(
|
context.getSource(),
|
||||||
context.getSource(),
|
StringArgumentType.getString(context, "function")
|
||||||
EntityArgument.getPlayers(context, "targets"),
|
))
|
||||||
true
|
)
|
||||||
))
|
)
|
||||||
)
|
|
||||||
)
|
.then(Commands.literal("enable")
|
||||||
.then(Commands.literal("disable")
|
.executes(context -> setEnabled(
|
||||||
.executes(context -> setEnabled(
|
context.getSource(),
|
||||||
context.getSource(),
|
List.of(context.getSource().getPlayerOrException()),
|
||||||
List.of(context.getSource().getPlayerOrException()),
|
true
|
||||||
false
|
))
|
||||||
))
|
.then(Commands.argument("targets", EntityArgument.players())
|
||||||
.then(Commands.argument("targets", EntityArgument.players())
|
.executes(context -> setEnabled(
|
||||||
.executes(context -> setEnabled(
|
context.getSource(),
|
||||||
context.getSource(),
|
EntityArgument.getPlayers(context, "targets"),
|
||||||
EntityArgument.getPlayers(context, "targets"),
|
true
|
||||||
false
|
))
|
||||||
))
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
.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) {
|
private static int showStrength(CommandSourceStack source) {
|
||||||
source.sendSuccess(
|
source.sendSuccess(
|
||||||
() -> Component.literal("Sable gravity strength: " + format(globalStrength) + " m/s²"),
|
() -> Component.literal(
|
||||||
false
|
"Sable gravity strength: " + format(globalStrength) + " m/s²"
|
||||||
|
),
|
||||||
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int setStrength(CommandSourceStack source, float strength) {
|
private static int setStrength(CommandSourceStack source, float strength) {
|
||||||
globalStrength = strength;
|
globalStrength = strength;
|
||||||
|
|
||||||
for (ServerPlayer player : source.getServer().getPlayerList().getPlayers()) {
|
for (ServerPlayer player : source.getServer().getPlayerList().getPlayers()) {
|
||||||
SableGravityState state = (SableGravityState) player;
|
SableGravityState state = (SableGravityState) player;
|
||||||
state.sablegravity$setStrength(strength);
|
state.sablegravity$setStrength(strength);
|
||||||
}
|
}
|
||||||
|
|
||||||
source.sendSuccess(
|
source.sendSuccess(
|
||||||
() -> Component.literal(
|
() -> Component.literal(
|
||||||
"Sable gravity strength set to " + format(strength) + " m/s²"
|
"Sable gravity strength set to " + format(strength) + " m/s²"
|
||||||
+ (strength == VANILLA_STRENGTH ? " (vanilla)" : "")
|
+ (strength == VANILLA_STRENGTH ? " (vanilla)" : "")
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
return 1;
|
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) {
|
for (ServerPlayer player : targets) {
|
||||||
SableGravityState state = (SableGravityState) player;
|
SableGravityState state = (SableGravityState) player;
|
||||||
|
|
||||||
state.sablegravity$setStrength(globalStrength);
|
state.sablegravity$setStrength(globalStrength);
|
||||||
state.sablegravity$setEnabled(enabled);
|
state.sablegravity$setEnabled(enabled);
|
||||||
state.sablegravity$setLocalStateValid(false);
|
state.sablegravity$setLocalStateValid(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
int count = targets.size();
|
int count = targets.size();
|
||||||
|
|
||||||
source.sendSuccess(
|
source.sendSuccess(
|
||||||
() -> Component.literal(
|
() -> Component.literal(
|
||||||
"Sable gravity " + (enabled ? "enabled" : "disabled")
|
"Sable gravity " + (enabled ? "enabled" : "disabled")
|
||||||
+ " for " + count + " player" + (count == 1 ? "" : "s")
|
+ " for " + count + " player" + (count == 1 ? "" : "s")
|
||||||
),
|
),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
return count;
|
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) {
|
private static String format(float value) {
|
||||||
if (value == (long) value) {
|
if (value == (long) value) {
|
||||||
return Long.toString((long) value);
|
return Long.toString((long) value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Float.toString(value);
|
return Float.toString(value);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@FunctionalInterface
|
||||||
|
private interface TestFunction {
|
||||||
|
void run(CommandSourceStack source) throws Exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user