Initial chat registry

This commit is contained in:
Noel Németh 2022-06-18 03:07:18 +02:00
parent c3960e6a47
commit 7c33dd914d
12 changed files with 160 additions and 215 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;
@ -17,6 +18,10 @@ import net.minestom.server.extras.optifine.OptifineSupport;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.rule.vanilla.RedstonePlacementRule;
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;
@ -105,6 +110,11 @@ 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));
PlayerInit.init();
OptifineSupport.enable();

View File

@ -10,6 +10,7 @@ import net.minestom.server.gamedata.tags.TagManager;
import net.minestom.server.instance.InstanceManager;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.listener.manager.PacketListenerManager;
import net.minestom.server.message.registry.ChatRegistryManager;
import net.minestom.server.monitoring.BenchmarkManager;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.PacketProcessor;
@ -186,6 +187,10 @@ public final class MinecraftServer {
return serverProcess.bossBar();
}
public static ChatRegistryManager getChatRegistryManager() {
return serverProcess.chatRegistry();
}
public static PacketProcessor getPacketProcessor() {
return serverProcess.packetProcessor();
}

View File

@ -12,6 +12,7 @@ import net.minestom.server.instance.InstanceManager;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.listener.manager.PacketListenerManager;
import net.minestom.server.message.registry.ChatRegistryManager;
import net.minestom.server.monitoring.BenchmarkManager;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.PacketProcessor;
@ -138,6 +139,8 @@ public interface ServerProcess extends Snapshotable {
*/
@NotNull Ticker ticker();
@NotNull ChatRegistryManager chatRegistry();
void start(@NotNull SocketAddress socketAddress);
void stop();

View File

@ -16,6 +16,7 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceManager;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.listener.manager.PacketListenerManager;
import net.minestom.server.message.registry.ChatRegistryManager;
import net.minestom.server.monitoring.BenchmarkManager;
import net.minestom.server.monitoring.TickMonitor;
import net.minestom.server.network.ConnectionManager;
@ -68,6 +69,7 @@ final class ServerProcessImpl implements ServerProcess {
private final ThreadDispatcher<Chunk> dispatcher;
private final Ticker ticker;
private final ChatRegistryManager chatRegistryManager;
private final AtomicBoolean started = new AtomicBoolean();
private final AtomicBoolean stopped = new AtomicBoolean();
@ -95,6 +97,7 @@ final class ServerProcessImpl implements ServerProcess {
this.dispatcher = ThreadDispatcher.singleThread();
this.ticker = new TickerImpl();
this.chatRegistryManager = new ChatRegistryManager();
}
@Override
@ -202,6 +205,11 @@ final class ServerProcessImpl implements ServerProcess {
return ticker;
}
@Override
public @NotNull ChatRegistryManager chatRegistry() {
return chatRegistryManager;
}
@Override
public void start(@NotNull SocketAddress socketAddress) {
if (!started.compareAndSet(false, true)) {

View File

@ -252,7 +252,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
this.dimensionType = spawnInstance.getDimensionType();
NBTCompound nbt = NBT.Compound(Map.of(
"minecraft:chat_type", Messenger.chatRegistry(),
"minecraft:chat_type", MinecraftServer.getChatRegistryManager().toNBT(),
"minecraft:dimension_type", MinecraftServer.getDimensionTypeManager().toNBT(),
"minecraft:worldgen/biome", MinecraftServer.getBiomeManager().toNBT()));
final JoinGamePacket joinGamePacket = new JoinGamePacket(getEntityId(), false, gameMode, null,

View File

@ -7,11 +7,7 @@ import net.minestom.server.network.packet.server.play.PlayerChatMessagePacket;
import net.minestom.server.network.packet.server.play.SystemChatPacket;
import net.minestom.server.utils.PacketUtils;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTException;
import org.jglrxavpok.hephaistos.parser.SNBTParser;
import java.io.StringReader;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;
@ -26,216 +22,6 @@ public final class Messenger {
public static final Component CANNOT_SEND_MESSAGE = Component.translatable("chat.cannotSend", NamedTextColor.RED);
private static final SystemChatPacket CANNOT_SEND_PACKET = new SystemChatPacket(CANNOT_SEND_MESSAGE, ChatPosition.SYSTEM_MESSAGE.getID());
private static final NBTCompound CHAT_REGISTRY;
static {
try {
CHAT_REGISTRY = (NBTCompound) new SNBTParser(new StringReader("""
{
"type":"minecraft:chat_type",
"value":[
{
"element":{
"chat":{
"decoration":{
"parameters":[
"sender",
"content"
],
"style":{
\s
},
"translation_key":"chat.type.text"
}
},
"narration":{
"decoration":{
"parameters":[
"sender",
"content"
],
"style":{
\s
},
"translation_key":"chat.type.text.narrate"
},
"priority":"chat"
}
},
"id":0,
"name":"minecraft:chat"
},
{
"element":{
"chat":{
\s
},
"narration":{
"priority":"system"
}
},
"id":1,
"name":"minecraft:system"
},
{
"element":{
"overlay":{
\s
}
},
"id":2,
"name":"minecraft:game_info"
},
{
"element":{
"chat":{
"decoration":{
"parameters":[
"sender",
"content"
],
"style":{
\s
},
"translation_key":"chat.type.announcement"
}
},
"narration":{
"decoration":{
"parameters":[
"sender",
"content"
],
"style":{
\s
},
"translation_key":"chat.type.text.narrate"
},
"priority":"chat"
}
},
"id":3,
"name":"minecraft:say_command"
},
{
"element":{
"chat":{
"decoration":{
"parameters":[
"sender",
"content"
],
"style":{
"color":"gray",
"italic":1b
},
"translation_key":"commands.message.display.incoming"
}
},
"narration":{
"decoration":{
"parameters":[
"sender",
"content"
],
"style":{
\s
},
"translation_key":"chat.type.text.narrate"
},
"priority":"chat"
}
},
"id":4,
"name":"minecraft:msg_command"
},
{
"element":{
"chat":{
"decoration":{
"parameters":[
"team_name",
"sender",
"content"
],
"style":{
\s
},
"translation_key":"chat.type.team.text"
}
},
"narration":{
"decoration":{
"parameters":[
"sender",
"content"
],
"style":{
\s
},
"translation_key":"chat.type.text.narrate"
},
"priority":"chat"
}
},
"id":5,
"name":"minecraft:team_msg_command"
},
{
"element":{
"chat":{
"decoration":{
"parameters":[
"sender",
"content"
],
"style":{
\s
},
"translation_key":"chat.type.emote"
}
},
"narration":{
"decoration":{
"parameters":[
"sender",
"content"
],
"style":{
\s
},
"translation_key":"chat.type.emote"
},
"priority":"chat"
}
},
"id":6,
"name":"minecraft:emote_command"
},
{
"element":{
"chat":{
\s
},
"narration":{
"priority":"chat"
}
},
"id":7,
"name":"minecraft:tellraw_command"
}
]
}
""")).parse();
} catch (NBTException e) {
throw new RuntimeException(e);
}
}
public static @NotNull NBTCompound chatRegistry() {
return CHAT_REGISTRY;
}
/**
* Sends a message to a player, respecting their chat settings.
*

View File

@ -0,0 +1,29 @@
package net.minestom.server.message.registry;
import net.kyori.adventure.text.format.Style;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTType;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
public record ChatDecoration(@NotNull String translationKey, @NotNull List<Parameter> params, @NotNull Style style) implements NBTCompoundWriteable {
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);
@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
}
public enum Parameter {
SENDER, TEAM_NAME, CONTENT
}
}

View File

@ -0,0 +1,42 @@
package net.minestom.server.message.registry;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTType;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
public final class ChatRegistryManager {
private final AtomicInteger chatTypesId = new AtomicInteger();
private final Int2ObjectMap<ChatType> idToType = new Int2ObjectOpenHashMap<>();
private final Object2IntMap<ChatType> typeToId = new Object2IntOpenHashMap<>();
public int addChatType(ChatType type) {
final int id = chatTypesId.getAndIncrement();
idToType.put(id, type);
typeToId.put(type, id);
return id;
}
public int getIdOf(ChatType type) {
return typeToId.getInt(type);
}
public NBTCompound toNBT() {
final MutableNBTCompound root = new MutableNBTCompound();
root.set("type", NBT.String("minecraft:chat_type"));
root.set("value", NBT.List(NBTType.TAG_Compound, idToType.int2ObjectEntrySet().stream().map(x -> {
final MutableNBTCompound compound = new MutableNBTCompound();
compound.setInt("id", x.getIntKey());
x.getValue().write(compound);
return compound.toCompound();
}).collect(Collectors.toList())));
return root.toCompound();
}
}

View File

@ -0,0 +1,21 @@
package net.minestom.server.message.registry;
import net.kyori.adventure.key.Key;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import static net.minestom.server.message.registry.NBTCompoundWriteable.writeIfPresent;
public record ChatType(Key name, @Nullable TextDisplay chat, @Nullable TextDisplay overlay, @Nullable Narration narration) implements NBTCompoundWriteable {
@Override
public void write(MutableNBTCompound compound) {
compound.setString("name", name.asString());
final MutableNBTCompound element = new MutableNBTCompound();
writeIfPresent("chat", chat, element);
writeIfPresent("overlay", overlay, element);
writeIfPresent("narration", narration, element);
compound.set("element", element.toCompound());
}
}

View File

@ -0,0 +1,16 @@
package net.minestom.server.message.registry;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
public interface NBTCompoundWriteable {
static void writeIfPresent(String name, @Nullable NBTCompoundWriteable writeable, MutableNBTCompound element) {
if (writeable != null) {
final MutableNBTCompound el = new MutableNBTCompound();
writeable.write(el);
element.set(name, el.toCompound());
}
}
void write(MutableNBTCompound compound);
}

View File

@ -0,0 +1,11 @@
package net.minestom.server.message.registry;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
//TODO
public record Narration() implements NBTCompoundWriteable {
@Override
public void write(MutableNBTCompound compound) {
}
}

View File

@ -0,0 +1,14 @@
package net.minestom.server.message.registry;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import static net.minestom.server.message.registry.NBTCompoundWriteable.writeIfPresent;
public record TextDisplay(@Nullable ChatDecoration decoration) implements NBTCompoundWriteable {
@Override
public void write(MutableNBTCompound compound) {
writeIfPresent("decoration", decoration, compound);
}
}