Add API for custom chat completions like on Paper (#1232)

* Add API for custom chat completions like on Paper, renamed enum constant to match its function, NMS and wiki-vg

* add null and version checks
This commit is contained in:
itsTyrion
2024-02-09 22:23:16 +01:00
committed by GitHub
parent 7bff6b19eb
commit d8bb4e97e4
3 changed files with 64 additions and 2 deletions

View File

@@ -309,4 +309,33 @@ public interface Player extends
* @return the player's client brand * @return the player's client brand
*/ */
@Nullable String getClientBrand(); @Nullable String getClientBrand();
/**
* Add custom chat completion suggestions shown to the player while typing a message.
*
* @param completions the completions to send
*/
void addCustomChatCompletions(@NotNull Collection<String> completions);
/**
* Remove custom chat completion suggestions shown to the player while typing a message.
*
* <p>Online player names can't be removed with this method, it will only affect
* custom completions added by {@link #addCustomChatCompletions(Collection)}
* or {@link #setCustomChatCompletions(Collection)}.
*
* @param completions the completions to remove
*/
void removeCustomChatCompletions(@NotNull Collection<String> completions);
/**
* Set the list of chat completion suggestions shown to the player while typing a message.
*
* <p>If completions were set previously, this method will remove them all
* and replace them with the provided completions.
*
* @param completions the completions to set
*/
void setCustomChatCompletions(@NotNull Collection<String> completions);
} }

View File

@@ -69,6 +69,7 @@ import com.velocitypowered.proxy.protocol.packet.RemoveResourcePackPacket;
import com.velocitypowered.proxy.protocol.packet.chat.ChatQueue; import com.velocitypowered.proxy.protocol.packet.chat.ChatQueue;
import com.velocitypowered.proxy.protocol.packet.chat.ChatType; import com.velocitypowered.proxy.protocol.packet.chat.ChatType;
import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder;
import com.velocitypowered.proxy.protocol.packet.chat.PlayerChatCompletionPacket;
import com.velocitypowered.proxy.protocol.packet.chat.builder.ChatBuilderFactory; import com.velocitypowered.proxy.protocol.packet.chat.builder.ChatBuilderFactory;
import com.velocitypowered.proxy.protocol.packet.chat.legacy.LegacyChatPacket; import com.velocitypowered.proxy.protocol.packet.chat.legacy.LegacyChatPacket;
import com.velocitypowered.proxy.protocol.packet.config.StartUpdatePacket; import com.velocitypowered.proxy.protocol.packet.config.StartUpdatePacket;
@@ -961,6 +962,31 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
this.clientBrand = clientBrand; this.clientBrand = clientBrand;
} }
@Override
public void addCustomChatCompletions(@NotNull Collection<String> completions) {
Preconditions.checkNotNull(completions, "completions");
this.sendCustomChatCompletionPacket(completions, PlayerChatCompletionPacket.Action.ADD);
}
@Override
public void removeCustomChatCompletions(@NotNull Collection<String> completions) {
Preconditions.checkNotNull(completions, "completions");
this.sendCustomChatCompletionPacket(completions, PlayerChatCompletionPacket.Action.REMOVE);
}
@Override
public void setCustomChatCompletions(@NotNull Collection<String> completions) {
Preconditions.checkNotNull(completions, "completions");
this.sendCustomChatCompletionPacket(completions, PlayerChatCompletionPacket.Action.SET);
}
private void sendCustomChatCompletionPacket(@NotNull Collection<String> completions,
PlayerChatCompletionPacket.Action action) {
if (connection.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19_1)) {
connection.write(new PlayerChatCompletionPacket(completions.toArray(new String[0]), action));
}
}
@Override @Override
public void spoofChatInput(String input) { public void spoofChatInput(String input) {
Preconditions.checkArgument(input.length() <= LegacyChatPacket.MAX_SERVERBOUND_MESSAGE_LENGTH, Preconditions.checkArgument(input.length() <= LegacyChatPacket.MAX_SERVERBOUND_MESSAGE_LENGTH,

View File

@@ -28,6 +28,13 @@ public class PlayerChatCompletionPacket implements MinecraftPacket {
private String[] completions; private String[] completions;
private Action action; private Action action;
public PlayerChatCompletionPacket() {
}
public PlayerChatCompletionPacket(String[] completions, Action action) {
this.completions = completions;
this.action = action;
}
public String[] getCompletions() { public String[] getCompletions() {
return completions; return completions;
@@ -64,9 +71,9 @@ public class PlayerChatCompletionPacket implements MinecraftPacket {
return handler.handle(this); return handler.handle(this);
} }
enum Action { public enum Action {
ADD, ADD,
REMOVE, REMOVE,
ALTER SET
} }
} }