Created
September 22, 2025 00:00
-
-
Save mworzala/175b829e634a2d1dd7dc1e8e03698e4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static { | |
| var packetListenerManager = MinecraftServer.getPacketListenerManager(); | |
| packetListenerManager.setPlayListener(ClientCommandChatPacket.class, CommandHandlingPlayer::execCommand); | |
| packetListenerManager.setPlayListener(ClientTabCompletePacket.class, CommandHandlingPlayer::tabCommand); | |
| } | |
| private static void execCommand(@NotNull ClientCommandChatPacket packet, @NotNull Player player) { | |
| final String command = packet.message(); | |
| if (Messenger.canReceiveCommand(player)) { | |
| Thread.startVirtualThread(() -> { | |
| try { | |
| var manager = CommandHandlingPlayer.asHandled(player).getCommandManager(); | |
| switch (manager.execute(player, command)) { | |
| case CommandResult.Success ignored -> { | |
| } | |
| case CommandResult.Denied ignored -> | |
| player.sendMessage(Component.translatable("command.not_found")); | |
| case CommandResult.NotFound ignored -> | |
| player.sendMessage(Component.translatable("command.not_found")); | |
| case CommandResult.SyntaxError result -> { | |
| var errorMessage = result.message(); | |
| var builder = Component.text() | |
| .append(Component.text("Syntax error:", NamedTextColor.RED)) | |
| .appendNewline() | |
| .append(Component.text(command.substring(0, result.start()), NamedTextColor.GRAY)) | |
| .append(Component.text(command.substring(result.start()), NamedTextColor.RED, TextDecoration.UNDERLINED)) | |
| .append(Component.text("<--[HERE] ", NamedTextColor.RED)); | |
| if (errorMessage != null) { | |
| builder.append(Component.text(errorMessage, NamedTextColor.RED)); | |
| } | |
| player.sendMessage(builder.build()); | |
| } | |
| case CommandResult.ExecutionError result -> { | |
| player.sendMessage(Component.translatable("generic.unknown_error")); | |
| var syntheticException = new RuntimeException( | |
| "An unhandled exception occurred while executing the command '" + command + "'", result.cause()); | |
| PostHog.captureException(syntheticException, player.getUuid().toString()); | |
| log.error("command eval failure", syntheticException); | |
| } | |
| } | |
| } catch (Exception e) { | |
| PostHog.captureException(e, player.getUuid().toString()); | |
| log.error("command failure", e); | |
| } | |
| }); | |
| } else { | |
| Messenger.sendRejectionMessage(player); | |
| } | |
| } | |
| private static void tabCommand(@NotNull ClientTabCompletePacket packet, @NotNull Player player) { | |
| if (!Messenger.canReceiveCommand(player)) return; | |
| var manager = CommandHandlingPlayer.asHandled(player).getCommandManager(); | |
| // Collect the suggestions | |
| Thread.startVirtualThread(() -> { | |
| try { | |
| var text = packet.text(); | |
| if (text.startsWith("/")) | |
| text = text.substring(1); | |
| var suggestion = manager.suggest(player, text); | |
| if (!suggestion.isEmpty()) { | |
| player.sendPacket(new TabCompletePacket( | |
| packet.transactionId(), | |
| suggestion.getStart() + 1, | |
| suggestion.getLength(), | |
| suggestion.getEntries().stream() | |
| .map(entry -> new TabCompletePacket.Match( | |
| entry.replacement(), | |
| entry.tooltip() | |
| )) | |
| .toList()) | |
| ); | |
| } | |
| } catch (Exception e) { | |
| PostHog.captureException(e, player.getUuid().toString()); | |
| log.error("command suggestion failure on: '" + packet.text() + "'", e); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment