Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-11-16 21:10:30 +01:00
Introduce VelocityBrigadierMessage (#588)
Dieser Commit ist enthalten in:
Ursprung
7c858424b5
Commit
111d14c3fb
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 Velocity Contributors
|
||||||
|
*
|
||||||
|
* The Velocity API is licensed under the terms of the MIT License. For more details,
|
||||||
|
* reference the LICENSE file in the api top-level directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.velocitypowered.api.command;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
import com.mojang.brigadier.Message;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.ComponentLike;
|
||||||
|
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an implementation of brigadier's {@link Message}, providing support for {@link
|
||||||
|
* Component} messages.
|
||||||
|
*/
|
||||||
|
public final class VelocityBrigadierMessage implements Message, ComponentLike {
|
||||||
|
|
||||||
|
public static VelocityBrigadierMessage tooltip(Component message) {
|
||||||
|
return new VelocityBrigadierMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Component message;
|
||||||
|
|
||||||
|
private VelocityBrigadierMessage(Component message) {
|
||||||
|
this.message = Preconditions.checkNotNull(message, "message");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@NotNull
|
||||||
|
public Component asComponent() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the message as a plain text.
|
||||||
|
*
|
||||||
|
* @return message as plain text
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getString() {
|
||||||
|
return PlainTextComponentSerializer.plainText().serialize(message);
|
||||||
|
}
|
||||||
|
}
|
@ -24,9 +24,9 @@ import com.mojang.brigadier.CommandDispatcher;
|
|||||||
import com.mojang.brigadier.ParseResults;
|
import com.mojang.brigadier.ParseResults;
|
||||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
import com.mojang.brigadier.suggestion.Suggestion;
|
import com.mojang.brigadier.suggestion.Suggestion;
|
||||||
|
import com.mojang.brigadier.suggestion.Suggestions;
|
||||||
import com.mojang.brigadier.tree.CommandNode;
|
import com.mojang.brigadier.tree.CommandNode;
|
||||||
import com.mojang.brigadier.tree.RootCommandNode;
|
import com.mojang.brigadier.tree.RootCommandNode;
|
||||||
import com.spotify.futures.CompletableFutures;
|
|
||||||
import com.velocitypowered.api.command.BrigadierCommand;
|
import com.velocitypowered.api.command.BrigadierCommand;
|
||||||
import com.velocitypowered.api.command.Command;
|
import com.velocitypowered.api.command.Command;
|
||||||
import com.velocitypowered.api.command.CommandManager;
|
import com.velocitypowered.api.command.CommandManager;
|
||||||
@ -47,7 +47,6 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.locks.ReadWriteLock;
|
import java.util.concurrent.locks.ReadWriteLock;
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import net.kyori.adventure.identity.Identity;
|
import net.kyori.adventure.identity.Identity;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
@ -259,14 +258,26 @@ public class VelocityCommandManager implements CommandManager {
|
|||||||
*/
|
*/
|
||||||
public CompletableFuture<List<String>> offerSuggestions(final CommandSource source,
|
public CompletableFuture<List<String>> offerSuggestions(final CommandSource source,
|
||||||
final String cmdLine) {
|
final String cmdLine) {
|
||||||
|
return offerBrigadierSuggestions(source, cmdLine)
|
||||||
|
.thenApply(suggestions -> Lists.transform(suggestions.getList(), Suggestion::getText));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns suggestions to fill in the given command.
|
||||||
|
*
|
||||||
|
* @param source the source to execute the command for
|
||||||
|
* @param cmdLine the partially completed command
|
||||||
|
* @return a {@link CompletableFuture} eventually completed with {@link Suggestions},
|
||||||
|
* possibly empty
|
||||||
|
*/
|
||||||
|
public CompletableFuture<Suggestions> offerBrigadierSuggestions(
|
||||||
|
final CommandSource source, final String cmdLine) {
|
||||||
Preconditions.checkNotNull(source, "source");
|
Preconditions.checkNotNull(source, "source");
|
||||||
Preconditions.checkNotNull(cmdLine, "cmdLine");
|
Preconditions.checkNotNull(cmdLine, "cmdLine");
|
||||||
|
|
||||||
final String normalizedInput = VelocityCommands.normalizeInput(cmdLine, false);
|
final String normalizedInput = VelocityCommands.normalizeInput(cmdLine, false);
|
||||||
try {
|
try {
|
||||||
return suggestionsProvider.provideSuggestions(normalizedInput, source)
|
return suggestionsProvider.provideSuggestions(normalizedInput, source);
|
||||||
.thenApply(suggestions ->
|
|
||||||
Lists.transform(suggestions.getList(), Suggestion::getText));
|
|
||||||
} catch (final Throwable e) {
|
} catch (final Throwable e) {
|
||||||
// Again, plugins are naughty
|
// Again, plugins are naughty
|
||||||
return CompletableFuture.failedFuture(
|
return CompletableFuture.failedFuture(
|
||||||
|
@ -23,6 +23,8 @@ import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_8;
|
|||||||
import static com.velocitypowered.proxy.protocol.util.PluginMessageUtil.constructChannelsPacket;
|
import static com.velocitypowered.proxy.protocol.util.PluginMessageUtil.constructChannelsPacket;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import com.mojang.brigadier.suggestion.Suggestion;
|
||||||
|
import com.velocitypowered.api.command.VelocityBrigadierMessage;
|
||||||
import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult;
|
import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult;
|
||||||
import com.velocitypowered.api.event.connection.PluginMessageEvent;
|
import com.velocitypowered.api.event.connection.PluginMessageEvent;
|
||||||
import com.velocitypowered.api.event.player.PlayerChannelRegisterEvent;
|
import com.velocitypowered.api.event.player.PlayerChannelRegisterEvent;
|
||||||
@ -497,15 +499,21 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
server.getCommandManager().offerSuggestions(player, command)
|
server.getCommandManager().offerBrigadierSuggestions(player, command)
|
||||||
.thenAcceptAsync(suggestions -> {
|
.thenAcceptAsync(suggestions -> {
|
||||||
if (suggestions.isEmpty()) {
|
if (suggestions.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Offer> offers = new ArrayList<>();
|
List<Offer> offers = new ArrayList<>();
|
||||||
for (String offer : suggestions) {
|
for (Suggestion suggestion : suggestions.getList()) {
|
||||||
offers.add(new Offer(offer));
|
String offer = suggestion.getText();
|
||||||
|
Component tooltip = null;
|
||||||
|
if (suggestion.getTooltip() != null
|
||||||
|
&& suggestion.getTooltip() instanceof VelocityBrigadierMessage) {
|
||||||
|
tooltip = ((VelocityBrigadierMessage) suggestion.getTooltip()).asComponent();
|
||||||
|
}
|
||||||
|
offers.add(new Offer(offer, tooltip));
|
||||||
}
|
}
|
||||||
int startPos = packet.getCommand().lastIndexOf(' ') + 1;
|
int startPos = packet.getCommand().lastIndexOf(' ') + 1;
|
||||||
if (startPos > 0) {
|
if (startPos > 0) {
|
||||||
@ -555,16 +563,22 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
|
|||||||
|
|
||||||
private void finishCommandTabComplete(TabCompleteRequest request, TabCompleteResponse response) {
|
private void finishCommandTabComplete(TabCompleteRequest request, TabCompleteResponse response) {
|
||||||
String command = request.getCommand().substring(1);
|
String command = request.getCommand().substring(1);
|
||||||
server.getCommandManager().offerSuggestions(player, command)
|
server.getCommandManager().offerBrigadierSuggestions(player, command)
|
||||||
.thenAcceptAsync(offers -> {
|
.thenAcceptAsync(offers -> {
|
||||||
boolean legacy = player.getProtocolVersion().compareTo(MINECRAFT_1_13) < 0;
|
boolean legacy = player.getProtocolVersion().compareTo(MINECRAFT_1_13) < 0;
|
||||||
try {
|
try {
|
||||||
for (String offer : offers) {
|
for (Suggestion suggestion : offers.getList()) {
|
||||||
|
String offer = suggestion.getText();
|
||||||
offer = legacy && !offer.startsWith("/") ? "/" + offer : offer;
|
offer = legacy && !offer.startsWith("/") ? "/" + offer : offer;
|
||||||
if (legacy && offer.startsWith(command)) {
|
if (legacy && offer.startsWith(command)) {
|
||||||
offer = offer.substring(command.length());
|
offer = offer.substring(command.length());
|
||||||
}
|
}
|
||||||
response.getOffers().add(new Offer(offer, null));
|
Component tooltip = null;
|
||||||
|
if (suggestion.getTooltip() != null
|
||||||
|
&& suggestion.getTooltip() instanceof VelocityBrigadierMessage) {
|
||||||
|
tooltip = ((VelocityBrigadierMessage) suggestion.getTooltip()).asComponent();
|
||||||
|
}
|
||||||
|
response.getOffers().add(new Offer(offer, tooltip));
|
||||||
}
|
}
|
||||||
response.getOffers().sort(null);
|
response.getOffers().sort(null);
|
||||||
player.getConnection().write(response);
|
player.getConnection().write(response);
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren