Skip to content

Commit

Permalink
add gamerule to show world.execute() warnings in chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Darmo117 committed Dec 6, 2023
1 parent ad618c3 commit 69baedd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/main/java/net/darmo_creations/mccode/MCCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class MCCode implements ModInitializer {
GameRules.Category.MISC,
GameRuleFactory.createBooleanRule(true)
);
public static final GameRules.Key<GameRules.BooleanRule> GR_SHOW_WARNING_MESSAGES = GameRuleRegistry.register(
"show_mccode_warning_messages",
GameRules.Category.MISC,
GameRuleFactory.createBooleanRule(false)
);

private static MCCode instance;

Expand Down Expand Up @@ -156,9 +161,9 @@ private void onWorldTickStart(ServerWorld world) {
if (message != null) {
message.setStyle(Style.EMPTY.withColor(Formatting.RED));
}
// Only show error messages to players that can use the /program command
if (world.getGameRules().getBoolean(GR_SHOW_ERROR_MESSAGES)) {
final Text m = message;
// Only show error messages to players that can use the /program command
world.getPlayers(PlayerEntity::isCreativeLevelTwoOp)
.forEach(player -> player.sendMessage(m, false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.minecraft.command.argument.NbtPathArgumentType;
import net.minecraft.command.argument.RegistryPredicateArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.*;
import net.minecraft.nbt.*;
import net.minecraft.network.message.SignedCommandArguments;
import net.minecraft.predicate.NbtPredicate;
Expand All @@ -41,10 +42,8 @@
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralTextContent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.text.*;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.Vec2f;
Expand Down Expand Up @@ -698,17 +697,27 @@ private static Optional<Long> executeCommand(final Scope scope, ServerWorld self
CommandSourceStackWrapper commandSourceStack = new CommandSourceStackWrapper(server, self, execPos, execRot);
long result = server.getCommandManager().executeWithPrefix(commandSourceStack, command);
if (result == 0 && commandSourceStack.anyFailures) {
final String dimension = Utils.getDimension(program.getProgramManager().getWorld());
final String cmd = command;
commandSourceStack.errors.forEach(text -> {
String prefix = "[Program %s in %s] world.execute(\"/%s\"): ".formatted(program.getName(), dimension, cmd);
MCCode.LOGGER.warn(prefix + text.getString());
});
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);
}

private static void printWarning(ServerWorld self, String message) {
MCCode.LOGGER.warn(message);
if (self.getGameRules().getBoolean(MCCode.GR_SHOW_WARNING_MESSAGES)) {
// Only show warning messages to players that can use the /program command
self.getPlayers(PlayerEntity::isCreativeLevelTwoOp).forEach(player -> {
Text m = Text.literal(message)
.setStyle(Style.EMPTY.withColor(Formatting.GOLD));
player.sendMessage(m, false);
});
}
}

/**
* Returns a list of entities that match the given target selector or null if the selector is invalid.
*/
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/mccode/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"chat.function_doc.tooltip": "Click to show doc for function %s.",

"gamerule.show_mccode_error_messages": "Show MC Code error messages",
"gamerule.show_mccode_warning_messages": "Show MC Code warning messages",

"commands.program.feedback.program_launched": "Program “%s” launched.",
"commands.program.feedback.program_stopped": "Program “%s” stopped and unloaded.",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/mccode/lang/eo_uy.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"chat.function_doc.tooltip": "Alklaku por montri la dok. por la funkcio %s.",

"gamerule.show_mccode_error_messages": "Montri la erarmensaĝojn de MC Code",
"gamerule.show_mccode_warning_messages": "Montri la avertmensaĝojn de MC Code",

"commands.program.feedback.program_launched": "Programo “%s” lanĉita.",
"commands.program.feedback.program_stopped": "Programo “%s” haltita kaj malŝarĝita.",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/mccode/lang/fr_fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"chat.function_doc.tooltip": "Cliquer pour afficher la doc de la fonction %s.",

"gamerule.show_mccode_error_messages": "Montrer les messages d’erreur de MC Code",
"gamerule.show_mccode_warning_messages": "Montrer les messages d’avertissement de MC Code",

"commands.program.feedback.program_launched": "Programme « %s » lancé.",
"commands.program.feedback.program_stopped": "Programme « %s » stoppé et déchargé.",
Expand Down

0 comments on commit 69baedd

Please sign in to comment.