Skip to content

Commit 6bcbe0e

Browse files
committed
fix: apply cooldown after dispatch, escape MiniMessage in error messages, reuse PlaceholderStage
1 parent 71c1587 commit 6bcbe0e

4 files changed

Lines changed: 25 additions & 17 deletions

File tree

velocity/src/main/java/dev/objz/commandbridge/velocity/dispatch/CommandDispatcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ private Set<String> buildOPPermissions(Script script, CmdMapping currentCmd) {
132132
private void notifyExecutionError(CommandSource source, String command, String errorMessage) {
133133
if (source == null)
134134
return;
135-
source.sendMessage(MM.parse("<red>⚠</red> <gray>Execution failed: " + errorMessage + "</gray>"));
135+
String safe = errorMessage != null ? errorMessage.replace("<", "\\<") : "unknown";
136+
source.sendMessage(MM.parse("<red>⚠</red> <gray>Execution failed: " + safe + "</gray>"));
136137
}
137138
}

velocity/src/main/java/dev/objz/commandbridge/velocity/dispatch/CommandEntry.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public final class CommandEntry {
5656
private final UserCache userCache;
5757
private final List<Pipeline> pipelineStages;
5858
private final PlatformFeatures platformFeatures;
59+
private final PlaceholderStage placeholderStage;
5960

6061
public CommandEntry(
6162
ProxyServer proxy,
@@ -84,6 +85,7 @@ public CommandEntry(
8485
this.dispatcher = new CommandDispatcher(sessions, outNode, velocityExecutor, playerTracker);
8586

8687
this.cooldowns = new CooldownManager();
88+
this.placeholderStage = new PlaceholderStage(platformFeatures);
8789
this.pipelineStages = List.of(
8890
new ScriptResolutionStage(scriptManager),
8991
new ArgumentMappingStage(),
@@ -138,6 +140,12 @@ private void runPipelineStages(Iterator<Pipeline> stages, ExecutionContext ctx,
138140
}
139141

140142
private void executeCommands(ExecutionContext ctx) {
143+
if (ctx.script() != null && ctx.source() instanceof Player player) {
144+
Duration scriptCooldown = ctx.script().defaults().cooldown();
145+
if (scriptCooldown != null && !scriptCooldown.isZero()) {
146+
cooldowns.setCooldown(ctx.script().name(), player.getUniqueId(), scriptCooldown);
147+
}
148+
}
141149
Optional.ofNullable(ctx.script())
142150
.map(Script::commands)
143151
.filter(Predicate.not(List::isEmpty))
@@ -161,15 +169,16 @@ private void processCommandAt(ExecutionContext ctx, List<CmdMapping> commands, i
161169
}
162170
}
163171

164-
new PlaceholderStage(platformFeatures).process(nextCtx, result -> {
172+
placeholderStage.process(nextCtx, result -> {
165173
if (result instanceof ExecutionResult.Continue c) {
166-
if (ctx.source() instanceof Player player && cmd.cooldown() != null
167-
&& !cmd.cooldown().isZero() && !cmd.cooldown().isNegative()) {
168-
String cooldownKey = ctx.script().name() + ":" + index;
169-
cooldowns.setCooldown(cooldownKey, player.getUniqueId(), cmd.cooldown());
170-
}
171-
resolvePlayerArg(c.context()).thenAccept(resolved ->
172-
scheduleAndExecute(resolved, () -> processCommandAt(ctx, commands, index + 1)));
174+
resolvePlayerArg(c.context()).thenAccept(resolved -> {
175+
if (ctx.source() instanceof Player player && cmd.cooldown() != null
176+
&& !cmd.cooldown().isZero() && !cmd.cooldown().isNegative()) {
177+
String cooldownKey = ctx.script().name() + ":" + index;
178+
cooldowns.setCooldown(cooldownKey, player.getUniqueId(), cmd.cooldown());
179+
}
180+
scheduleAndExecute(resolved, () -> processCommandAt(ctx, commands, index + 1));
181+
});
173182
}
174183
});
175184
}

velocity/src/main/java/dev/objz/commandbridge/velocity/dispatch/stage/CooldownStage.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ public void process(ExecutionContext context, Consumer<ExecutionResult> next) {
3636
return;
3737
}
3838

39-
Duration duration = script.defaults().cooldown();
40-
if (duration != null && !duration.isZero()) {
41-
cooldownManager.setCooldown(script.name(), player.getUniqueId(), duration);
42-
}
43-
4439
next.accept(ExecutionResult.ok(context));
4540
}
4641
}

velocity/src/main/java/dev/objz/commandbridge/velocity/net/in/ExecuteCommandHandler.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,14 @@ private void notifyPlayer(UUID playerUuid, String command, String errorMessage,
8181
MM.parse("<dark_gray>If this persists, please contact an administrator</dark_gray>"));
8282

8383
if (player.hasPermission("commandbridge.admin")) {
84-
player.sendMessage(MM.parse("<dark_gray>Command: </dark_gray><white>" + command + "</white>"));
85-
player.sendMessage(MM.parse("<dark_gray>Backend: </dark_gray><white>" + clientId + "</white>"));
84+
String safeCmd = command != null ? command.replace("<", "\\<") : "unknown";
85+
String safeClient = clientId != null ? clientId.replace("<", "\\<") : "unknown";
86+
player.sendMessage(MM.parse("<dark_gray>Command: </dark_gray><white>" + safeCmd + "</white>"));
87+
player.sendMessage(MM.parse("<dark_gray>Backend: </dark_gray><white>" + safeClient + "</white>"));
8688
if (errorMessage != null) {
89+
String safeError = errorMessage.replace("<", "\\<");
8790
player.sendMessage(MM.parse(
88-
"<dark_gray>Error: </dark_gray><red>" + errorMessage + "</red>"));
91+
"<dark_gray>Error: </dark_gray><red>" + safeError + "</red>"));
8992
}
9093
}
9194
}

0 commit comments

Comments
 (0)