Skip to content

Commit

Permalink
使用try-catch包裹命令,以抑制MJ的命令错误输出
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Nov 9, 2024
1 parent 8ae9edc commit 9a77538
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/main/java/dev/anvilcraft/rg/api/server/ServerRGRuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.anvilcraft.rg.api.ConfigUtil;
import dev.anvilcraft.rg.api.RGEnvironment;
import dev.anvilcraft.rg.api.RGRule;
import dev.anvilcraft.rg.api.RGRuleException;
import dev.anvilcraft.rg.api.RGRuleManager;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
Expand Down Expand Up @@ -274,30 +275,40 @@ private int categoryCommand(@NotNull CommandContext<CommandSourceStack> context)
}

private <T> int setRuleCommand(@NotNull CommandContext<CommandSourceStack> context, @NotNull RGRule<T> rule, String value) {
rule.setFieldValue(value);
MutableComponent result = TranslationUtil
.trans("rolling_gate.command.rule.set", rule.name(), value)
.withStyle(ChatFormatting.GRAY);
MutableComponent setDefault = Component.literal("[")
.append(TranslationUtil
.trans("rolling_gate.command.rule.set.default.button", rule.name(), value))
.append("]")
.withStyle(Style.EMPTY
.applyFormat(ChatFormatting.AQUA)
.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/%s default %s %s".formatted(literal, rule.name(), value)))
);
result.append(" ").append(setDefault);
context.getSource().sendSuccess(() -> result, false);
return 1;
try {
rule.setFieldValue(value);
MutableComponent result = TranslationUtil
.trans("rolling_gate.command.rule.set", rule.name(), value)
.withStyle(ChatFormatting.GRAY);
MutableComponent setDefault = Component.literal("[")
.append(TranslationUtil
.trans("rolling_gate.command.rule.set.default.button", rule.name(), value))
.append("]")
.withStyle(Style.EMPTY
.applyFormat(ChatFormatting.AQUA)
.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/%s default %s %s".formatted(literal, rule.name(), value)))
);
result.append(" ").append(setDefault);
context.getSource().sendSuccess(() -> result, false);
return 1;
} catch (RGRuleException exception) {
context.getSource().sendFailure(Component.literal(exception.getMessage()).withStyle(ChatFormatting.RED));
return 0;
}
}

private <T> int defaultRuleCommand(@NotNull CommandContext<CommandSourceStack> context, @NotNull RGRule<T> rule, String value) {
setWorldConfig(context.getSource().getServer(), rule, rule.codec().decode(value));
MutableComponent result = TranslationUtil
.trans("rolling_gate.command.rule.set.default", rule.name(), value)
.withStyle(ChatFormatting.GRAY);
context.getSource().sendSuccess(() -> result, false);
return 1;
try {
setWorldConfig(context.getSource().getServer(), rule, rule.codec().decode(value));
MutableComponent result = TranslationUtil
.trans("rolling_gate.command.rule.set.default", rule.name(), value)
.withStyle(ChatFormatting.GRAY);
context.getSource().sendSuccess(() -> result, false);
return 1;
} catch (RGRuleException exception) {
context.getSource().sendFailure(Component.literal(exception.getMessage()).withStyle(ChatFormatting.RED));
return 0;
}
}
}
}

0 comments on commit 9a77538

Please sign in to comment.