Finish chat registry descriptor objects

This commit is contained in:
Noel Németh 2022-06-18 23:07:51 +02:00
parent 7c33dd914d
commit 77b5df0d94
5 changed files with 134 additions and 8 deletions

View File

@ -21,7 +21,6 @@ import net.minestom.server.message.MessageSender;
import net.minestom.server.message.registry.ChatDecoration;
import net.minestom.server.message.registry.ChatRegistryManager;
import net.minestom.server.message.registry.ChatType;
import net.minestom.server.message.registry.TextDisplay;
import net.minestom.server.ping.ResponseData;
import net.minestom.server.utils.identity.NamedAndIdentified;
import net.minestom.server.utils.time.TimeUnit;
@ -111,9 +110,8 @@ public class Main {
});
final ChatRegistryManager chatRegistryManager = MinecraftServer.getChatRegistryManager();
chatRegistryManager.addChatType(new ChatType(Key.key("minestom:chat"),
new TextDisplay(new ChatDecoration("%s|%s> %s", ChatDecoration.PARAM_ALL,
Style.style(NamedTextColor.DARK_RED))), null, null));
chatRegistryManager.addChatType(ChatType.chat(Key.key("minestom:chat"),
ChatDecoration.full("%s | %s> %s", Style.style(NamedTextColor.DARK_RED)).toTextDisplay()));
PlayerInit.init();

View File

@ -1,26 +1,82 @@
package net.minestom.server.message.registry;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minestom.server.MinecraftServer;
import org.intellij.lang.annotations.Pattern;
import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTException;
import org.jglrxavpok.hephaistos.nbt.NBTType;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import org.jglrxavpok.hephaistos.parser.SNBTParser;
import java.io.StringReader;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Defines how the message will appear
*
* @param translationKey translation used to provide the message format, can also be
* custom string with %s as param placeholder
* @param params params that will be used in place of %s in the translationKey
* @param style style of the template, params can override this
*/
public record ChatDecoration(@NotNull String translationKey, @NotNull List<Parameter> params, @NotNull Style style) implements NBTCompoundWriteable {
private static final String REGEX_ONE_PARAM = "^(?:(?!%s).)*%s(?!.*%s).*$";
private static final String REGEX_TWO_PARAM = "^(?:(?!%s).)*%s(?:(?!%s).)*%s(?!.*%s).*$";
private static final String REGEX_THREE_PARAM = "^(?:(?!%s).)*%s(?:(?!%s).)*%s(?:(?!%s).)*%s(?!.*%s).*$";
public static final List<Parameter> PARAM_ALL = List.of(Parameter.TEAM_NAME, Parameter.SENDER, Parameter.CONTENT);
public static final List<Parameter> PARAM_NAME = List.of(Parameter.SENDER, Parameter.CONTENT);
public static ChatDecoration content(@Subst("%s") @Pattern(REGEX_ONE_PARAM) String template) {
return content(template, Style.style().build());
}
public static ChatDecoration content(@Pattern(REGEX_ONE_PARAM) String template, Style style) {
return new ChatDecoration(template, List.of(Parameter.CONTENT), style);
}
public static ChatDecoration contentWithSender(@Subst("%s%s") @Pattern(REGEX_TWO_PARAM) String template) {
return contentWithSender(template, Style.style().build());
}
public static ChatDecoration contentWithSender(@Pattern(REGEX_TWO_PARAM) String template, Style style) {
return new ChatDecoration(template, PARAM_NAME, style);
}
public static ChatDecoration full(@Subst("%s%s%s") @Pattern(REGEX_THREE_PARAM) String template) {
return full(template, Style.style().build());
}
public static ChatDecoration full(@Pattern(REGEX_THREE_PARAM) String template, Style style) {
return new ChatDecoration(template, PARAM_ALL, style);
}
public TextDisplay toTextDisplay() {
return new TextDisplay(this);
}
@Override
public void write(MutableNBTCompound compound) {
compound.setString("translation_key", translationKey);
compound.set("parameters", NBT.List(NBTType.TAG_String, params.stream().map(x -> NBT.String(x.name()
.toLowerCase(Locale.ROOT))).collect(Collectors.toList())));
compound.set("style", NBT.Compound(Map.of()));//TODO
MutableNBTCompound styleCompound;
try {
styleCompound = new MutableNBTCompound((NBTCompound) new SNBTParser(new StringReader(GsonComponentSerializer.gson().serialize(Component.empty().style(style)))).parse());
} catch (NBTException e) {
MinecraftServer.getExceptionManager().handleException(e);
MinecraftServer.LOGGER.error("Exception while parsing chat decoration style, falling back to empty style!", e);
styleCompound = new MutableNBTCompound();
}
styleCompound.remove("text");
compound.set("style", styleCompound.toCompound());
}
public enum Parameter {

View File

@ -6,8 +6,36 @@ import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import static net.minestom.server.message.registry.NBTCompoundWriteable.writeIfPresent;
/**
* Describes a chat type
*
* @param name name of this type
* @param chat if present the message sent with this type will show in chat
* @param overlay if present the message will show in the action bar
* @param narration id present the message can be narrated by the client if it's enabled clientside
*/
public record ChatType(Key name, @Nullable TextDisplay chat, @Nullable TextDisplay overlay, @Nullable Narration narration) implements NBTCompoundWriteable {
public static ChatType chat(Key name, TextDisplay display) {
return chat(name, display, null);
}
public static ChatType chat(Key name, TextDisplay display, Narration narration) {
return new ChatType(name, display, null, narration);
}
public static ChatType actionbar(Key name, TextDisplay display) {
return actionbar(name, display, null);
}
public static ChatType actionbar(Key name, TextDisplay display, Narration narration) {
return new ChatType(name, null, display, narration);
}
public static ChatType narration(Key name, Narration narration) {
return new ChatType(name, null, null, narration);
}
@Override
public void write(MutableNBTCompound compound) {
compound.setString("name", name.asString());

View File

@ -1,11 +1,46 @@
package net.minestom.server.message.registry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
//TODO
public record Narration() implements NBTCompoundWriteable {
import java.util.Locale;
import static net.minestom.server.message.registry.NBTCompoundWriteable.writeIfPresent;
/**
* Used to define how the message can be narrated
*
* @param decoration can be used to customize the sentence e.g. "playerName says: message"
* @param priority priority of the narration
*/
public record Narration(@Nullable ChatDecoration decoration, @NotNull Priority priority) implements NBTCompoundWriteable {
public static Narration system() {
return system(null);
}
public static Narration system(ChatDecoration decoration) {
return new Narration(decoration, Priority.SYSTEM);
}
public static Narration chat() {
return chat(null);
}
public static Narration chat(ChatDecoration decoration) {
return new Narration(decoration, Priority.CHAT);
}
@Override
public void write(MutableNBTCompound compound) {
writeIfPresent("decoration", decoration, compound);
compound.set("priority", NBT.String(priority().name().toLowerCase(Locale.ROOT)));
}
public enum Priority {
CHAT, SYSTEM
}
}

View File

@ -5,8 +5,17 @@ import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import static net.minestom.server.message.registry.NBTCompoundWriteable.writeIfPresent;
/**
* Used to display text either in chat or actionbar via {@link ChatType}
*
* @param decoration defines how this text will appear, if null only the message will show
*/
public record TextDisplay(@Nullable ChatDecoration decoration) implements NBTCompoundWriteable {
public static TextDisplay undecorated() {
return new TextDisplay(null);
}
@Override
public void write(MutableNBTCompound compound) {
writeIfPresent("decoration", decoration, compound);