Skip to content

Commit

Permalink
fix: replacing placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
onebeastchris committed Mar 27, 2024
1 parent 5bb92af commit 2654255
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ static CompletableFuture<ResultType> executeCommand(GeyserConnection connection,
value = ((StepSliderComponent) component).steps().get(response.asStepSlider(i));
}
MagicMenu.debug("Replacing: " + replace + " with " + value);
finalCommand.set(finalCommand.get().replaceFirst("!%" + replace + "%", String.valueOf(value)));
replacePlaceholder(finalCommand, replace, value);
} else {
MagicMenu.debug("Replacing: " + replace + " with default: " + defaultValue);
finalCommand.set(finalCommand.get().replaceFirst("!%" + replace + "%", defaultValue));
replacePlaceholder(finalCommand, replace, value);
}
}
sendCommand(connection, finalCommand.get());
Expand All @@ -228,6 +228,19 @@ static CompletableFuture<ResultType> executeCommand(GeyserConnection connection,
return completableFuture;
}

// using replaceFirst with the regex does not seem to work reliabl
private static void replacePlaceholder(AtomicReference<String> finalCommand, String replace, Object value) {
MagicMenu.debug("attempt to replace: " + replace + " with value " + value);
String temp = finalCommand.get();
int index = temp.indexOf("!%" + replace + "%");
if (index != -1) {
String before = temp.substring(0, index);
String after = temp.substring(index + ("!%" + replace + "%").length());
temp = before + value + after;
}
finalCommand.set(temp);
}

private static void sendCommand(GeyserConnection connection, String command) {
GeyserSession session = (GeyserSession) connection;

Expand Down

0 comments on commit 2654255

Please sign in to comment.