Fix preview

This commit is contained in:
Noel Németh 2022-06-19 12:26:41 +02:00
parent 45d1d19dd0
commit 5175384f2a
5 changed files with 40 additions and 14 deletions

View File

@ -1,5 +1,6 @@
package net.minestom.demo;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
@ -112,6 +113,8 @@ public class Main {
final ChatRegistryManager chatRegistryManager = MinecraftServer.getChatRegistryManager();
chatRegistryManager.addChatType(ChatType.chat(CommonChatType.CHAT.getName(),
ChatDecoration.full("%s | %s> %s", Style.style(NamedTextColor.DARK_RED)).toTextDisplay()));
chatRegistryManager.addChatType(ChatType.chat(CommonChatType.SYSTEM.getName(),
ChatDecoration.content("SYSTEM: %s", Style.style(NamedTextColor.AQUA).font(Key.key("minecraft:uniform"))).toTextDisplay()));
PlayerInit.init();

View File

@ -41,19 +41,24 @@ public class ChatPreviewCommand extends Command {
addSyntax(((sender, context) -> {
if (sender instanceof Player player) {
if (context.get(on)) {
player.sendMessage("Chat preview: on");
player.toggleChatPreview(true);
} else {
player.sendMessage("Chat preview: off");
player.toggleChatPreview(false);
}
final Boolean enable = context.get(on);
player.enableChatPreview(enable);
player.sendMessage("Chat preview: " + (enable ? "enabled" : "disabled"));
}
}), on);
}), ArgumentType.Literal("enable"), on);
addSyntax(((sender, context) -> {
handler = context.getOrDefault(handlerArg, PreviewHandler.NULL);
sender.sendMessage("Preview handler set to: " + handler.name());
}), handlerArg);
}), ArgumentType.Literal("handler"), handlerArg);
addSyntax(((sender, context) -> {
if (sender instanceof Player player) {
final Boolean enable = context.get(on);
player.toggleChatPreview(enable);
player.sendMessage("Chat preview: " + (enable ? "on" : "off"));
}
}), ArgumentType.Literal("toggle"), on);
}
}

View File

@ -2067,8 +2067,26 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
}
}
/**
* Used to enable or disable chat preview, can be called multiple times during play state
* however each invocation with enabled=true will result in a warning dialog client side.
* If you later want to toggle the preview serverside consider using {@link #toggleChatPreview(boolean)}.
*
* @param enable preview enabled?
*/
public void enableChatPreview(boolean enable) {
playerConnection.sendPacket(enable ? ServerDataPacket.PREVIEW_ENABLE : ServerDataPacket.PREVIEW_DISABLE);
}
/**
* Used to toggle chat preview without prompting the player on each enable call.
* <p>
* NOTE: This method only has effect if chat preview was enabled using {@link #enableChatPreview(boolean)}!
*
* @param on
*/
public void toggleChatPreview(boolean on) {
playerConnection.sendPacket(on ? ServerDataPacket.TOGGLE_PREVIEW_ON : ServerDataPacket.TOGGLE_PREVIEW_OFF);
playerConnection.sendPacket(on ? SetChatPreviewPacket.ON : SetChatPreviewPacket.OFF);
}
public @Nullable Component getLastPreviewedMessage() {

View File

@ -10,8 +10,8 @@ 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 static final ServerDataPacket PREVIEW_ENABLE = new ServerDataPacket(null, null, true);
public static final ServerDataPacket PREVIEW_DISABLE = new ServerDataPacket(null, null, false);
public ServerDataPacket(BinaryReader reader) {
this(reader.readNullableComponent(), reader.readNullableSizedString(), reader.readBoolean());
}

View File

@ -7,8 +7,8 @@ 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 static final SetChatPreviewPacket ON = new SetChatPreviewPacket(true);
public static final SetChatPreviewPacket OFF = new SetChatPreviewPacket(false);
public SetChatPreviewPacket(BinaryReader reader) {
this(reader.readBoolean());