mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-10 21:00:36 +01:00
Implement chat preview toggling and event
This commit is contained in:
parent
960d5c80db
commit
11ba78aa81
@ -2057,6 +2057,10 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
||||
}
|
||||
}
|
||||
|
||||
public void toggleChatPreview(boolean on) {
|
||||
playerConnection.sendPacket(on ? ServerDataPacket.TOGGLE_PREVIEW_ON : ServerDataPacket.TOGGLE_PREVIEW_OFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the main or off hand of the player.
|
||||
*/
|
||||
|
@ -0,0 +1,53 @@
|
||||
package net.minestom.server.event.player;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.trait.CancellableEvent;
|
||||
import net.minestom.server.event.trait.PlayerEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public final class PlayerChatPreviewEvent implements PlayerEvent, CancellableEvent {
|
||||
private final Player player;
|
||||
private final int id;
|
||||
private final String query;
|
||||
private boolean cancelled;
|
||||
private @Nullable Component result;
|
||||
|
||||
public PlayerChatPreviewEvent(Player player, int id, String query) {
|
||||
this.player = player;
|
||||
this.id = id;
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public @Nullable Component getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(@Nullable Component result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancel) {
|
||||
cancelled = cancel;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package net.minestom.server.listener;
|
||||
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.player.PlayerChatPreviewEvent;
|
||||
import net.minestom.server.network.packet.client.play.ClientChatPreviewPacket;
|
||||
import net.minestom.server.network.packet.server.play.ChatPreviewPacket;
|
||||
|
||||
public class ChatPreviewListener {
|
||||
public static void listener(ClientChatPreviewPacket packet, Player player) {
|
||||
final PlayerChatPreviewEvent event = new PlayerChatPreviewEvent(player, packet.queryId(), packet.query());
|
||||
MinecraftServer.getGlobalEventHandler().callCancellable(event,
|
||||
() -> player.sendPacket(new ChatPreviewPacket(event.getId(), event.getResult())));
|
||||
}
|
||||
}
|
@ -57,6 +57,7 @@ public final class PacketListenerManager {
|
||||
setListener(ClientAdvancementTabPacket.class, AdvancementTabListener::listener);
|
||||
setListener(ClientSpectatePacket.class, SpectateListener::listener);
|
||||
setListener(ClientEditBookPacket.class, BookListener::listener);
|
||||
setListener(ClientChatPreviewPacket.class, ChatPreviewListener::listener);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,14 +15,13 @@ import java.util.function.UnaryOperator;
|
||||
|
||||
public record ChatPreviewPacket(int queryId, @Nullable Component preview) implements ComponentHoldingServerPacket {
|
||||
public ChatPreviewPacket(BinaryReader reader) {
|
||||
this(reader.readInt(), reader.readBoolean() ? reader.readComponent() : null);
|
||||
this(reader.readInt(), reader.readNullableComponent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeInt(queryId);
|
||||
writer.writeBoolean(preview != null);
|
||||
if (preview != null) writer.writeComponent(preview);
|
||||
writer.writeNullableComponent(preview);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -10,17 +10,16 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public record ServerDataPacket(@Nullable Component motd, @Nullable String iconBase64,
|
||||
boolean previewsChat) implements ServerPacket {
|
||||
public static final ServerDataPacket TOGGLE_PREVIEW_ON = new ServerDataPacket(null, null, true);
|
||||
public static final ServerDataPacket TOGGLE_PREVIEW_OFF = new ServerDataPacket(null, null, false);
|
||||
public ServerDataPacket(BinaryReader reader) {
|
||||
this(reader.readBoolean() ? reader.readComponent() : null, reader.readBoolean() ? reader.readSizedString() : null,
|
||||
reader.readBoolean());
|
||||
this(reader.readNullableComponent(), reader.readNullableSizedString(), reader.readBoolean());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(@NotNull BinaryWriter writer) {
|
||||
writer.writeBoolean(motd != null);
|
||||
if (motd != null) writer.writeComponent(motd);
|
||||
writer.writeBoolean(iconBase64 != null);
|
||||
if (iconBase64 != null) writer.writeSizedString(iconBase64);
|
||||
writer.writeNullableComponent(motd);
|
||||
writer.writeNullableSizedString(iconBase64);
|
||||
writer.writeBoolean(previewsChat);
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,9 @@ import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public record SetChatPreviewPacket(boolean enable) implements ServerPacket {
|
||||
public static final SetChatPreviewPacket ENABLE = new SetChatPreviewPacket(true);
|
||||
public static final SetChatPreviewPacket DISABLE = new SetChatPreviewPacket(true);
|
||||
|
||||
public SetChatPreviewPacket(BinaryReader reader) {
|
||||
this(reader.readBoolean());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user