Skip to content

Commit

Permalink
world.execute() now requires @ prefix in command name to show any war…
Browse files Browse the repository at this point in the history
…ning at all
  • Loading branch information
Darmo117 committed Dec 7, 2023
1 parent 19017ee commit 68367be
Showing 1 changed file with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -669,14 +669,25 @@ public Position locateBiome(final Scope scope, final ServerWorld self, final Str
},
returnTypeMetadata = @ReturnMeta(mayBeNull = true,
doc = "The result of the command or #null if an error occured."),
doc = "Executes a command. See the Minecraft Wiki for more information. The /trigger command is not accepted and will raise an error ")
doc = "Executes a command." +
" Prefix the command name with @ to print warnings to the server console and chat when any occur, they will be hidden otherwise." +
" See the Minecraft Wiki for more information on commands." +
" The /trigger command is not accepted and will raise an error.")
public Long executeCommand(final Scope scope, ServerWorld self, final String name, final Object... args) {
if ("trigger".equals(name)) {
throw new MCCodeRuntimeException(scope, name, "mccode.interpreter.error.illegal_command", name);
String cmdName;
boolean showWarnings = name.startsWith("@");
if (showWarnings) {
cmdName = name.substring(1);
} else {
cmdName = name;
}
if ("trigger".equals(cmdName.trim())) {
throw new MCCodeRuntimeException(scope, cmdName, "mccode.interpreter.error.illegal_command", cmdName);
}
return executeCommand(
scope, self,
name,
cmdName,
showWarnings,
Arrays.stream(args).map(o -> ProgramManager.getTypeForValue(o).toString(o)).toArray(String[]::new)
).orElse(null);
}
Expand All @@ -685,7 +696,8 @@ public Long executeCommand(final Scope scope, ServerWorld self, final String nam
* Utility methods
*/

private static Optional<Long> executeCommand(final Scope scope, ServerWorld self, final String commandName, final String... args) {
private static Optional<Long> executeCommand(final Scope scope, ServerWorld self, final String commandName,
boolean showWarnings, final String... args) {
MinecraftServer server = self.getServer();
String command = commandName;
if (args.length != 0) {
Expand All @@ -696,11 +708,13 @@ private static Optional<Long> executeCommand(final Scope scope, ServerWorld self
Vec2f execRot = program.getExecutorRotation();
CommandSourceStackWrapper commandSourceStack = new CommandSourceStackWrapper(server, self, execPos, execRot);
long result = server.getCommandManager().executeWithPrefix(commandSourceStack, command);
if (result == 0 && commandSourceStack.anyFailures) {
String dimension = Utils.getDimension(program.getProgramManager().getWorld());
String prefix = "[Program %s in %s] ".formatted(program.getName(), dimension);
printWarning(self, prefix + "Command error: " + command);
commandSourceStack.errors.forEach(text -> printWarning(self, prefix + text.getString()));
if (commandSourceStack.anyFailures) {
if (showWarnings) {
String dimension = Utils.getDimension(program.getProgramManager().getWorld());
String prefix = "[Program %s in %s] ".formatted(program.getName(), dimension);
printWarning(self, prefix + "Command error: " + command);
commandSourceStack.errors.forEach(text -> printWarning(self, prefix + text.getString()));
}
return Optional.empty();
}
return Optional.of(result);
Expand Down Expand Up @@ -807,11 +821,11 @@ private static class CommandSourceStackWrapper extends ServerCommandSource {
/**
* The list of all errors.
*/
final List<Text> errors;
final List<Text> errors = new LinkedList<>();
/**
* Whether any failures occured while executing the last command.
*/
boolean anyFailures;
boolean anyFailures = false;

/**
* Create a wrapper for a command source and level.
Expand All @@ -835,8 +849,6 @@ private static class CommandSourceStackWrapper extends ServerCommandSource {
SignedCommandArguments.EMPTY,
FutureQueue.NOOP
);
this.errors = new LinkedList<>();
this.anyFailures = false;
}

@Override
Expand Down

0 comments on commit 68367be

Please sign in to comment.