diff --git a/src/main/java/net/minestom/server/gamedata/tags/TagManager.java b/src/main/java/net/minestom/server/gamedata/tags/TagManager.java index a3d203c9d..30e46a0c9 100644 --- a/src/main/java/net/minestom/server/gamedata/tags/TagManager.java +++ b/src/main/java/net/minestom/server/gamedata/tags/TagManager.java @@ -310,8 +310,7 @@ public class TagManager { public void addRequiredTagsToPacket(TagsPacket tagsPacket) { for (RequiredTag requiredTag : requiredTags) { final Tag tag = silentLoad(requiredTag.getName(), requiredTag.getType().name().toLowerCase()); - final String identifier = requiredTag.getType().getIdentifier(); - var map = tagsPacket.tagsMap.computeIfAbsent(identifier, s -> new ArrayList<>()); + var map = tagsPacket.tagsMap.computeIfAbsent(requiredTag.getType(), s -> new ArrayList<>()); map.add(tag); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java index d8490564c..b055cad4b 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java @@ -1,9 +1,12 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.MinecraftServer; +import net.minestom.server.entity.EntityType; import net.minestom.server.gamedata.tags.Tag; +import net.minestom.server.instance.block.Block; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; +import net.minestom.server.registry.Registries; import net.minestom.server.utils.NamespaceID; import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.binary.BinaryWriter; @@ -14,7 +17,7 @@ import java.util.function.Function; public class TagsPacket implements ServerPacket { - public Map> tagsMap = new HashMap<>(); + public Map> tagsMap = new HashMap<>(); private static final TagsPacket REQUIRED_TAGS_PACKET = new TagsPacket(); @@ -31,28 +34,133 @@ public class TagsPacket implements ServerPacket { @Override public void write(@NotNull BinaryWriter writer) { writer.writeVarInt(tagsMap.size()); - tagsMap.forEach((s, tags) -> { - writer.writeSizedString(s); + for (Tag.BasicTypes type : Tag.BasicTypes.values()) { + switch (type) { + case BLOCKS: { + List tags = tagsMap.get(type); - writer.writeVarInt(tags.size()); - for (Tag tag : tags) { - // name - writer.writeSizedString(tag.getName().toString()); + // Tag type + writer.writeSizedString(type.getIdentifier()); + // Number of tags + writer.writeVarInt(tags.size()); + for (Tag tag : tags) { + // name + writer.writeSizedString(tag.getName().toString()); + final Set values = tag.getValues(); + // count + writer.writeVarInt(values.size()); + // entries + for (NamespaceID name : values) { + Block b = Registries.getBlock(name); + if (b == Block.AIR && !name.toString().equals("minecraft:air")) { + writer.writeVarInt(-1); + } + writer.writeVarInt(b.ordinal()); + } + } + break; + } + case ENTITY_TYPES: { + List tags = tagsMap.get(type); - final Set values = tag.getValues(); - // count - writer.writeVarInt(values.size()); - // entries - for (NamespaceID name : values) { - // TODO id from namespace - writer.writeVarInt(0); + // Tag type + writer.writeSizedString(type.getIdentifier()); + + // Number of tags + writer.writeVarInt(tags.size()); + for (Tag tag : tags) { + // name + writer.writeSizedString(tag.getName().toString()); + + final Set values = tag.getValues(); + // count + writer.writeVarInt(values.size()); + // entries + for (NamespaceID name : values) { + EntityType et = Registries.getEntityType(name); + if (et == null) { + writer.writeVarInt(-1); + } else { + writer.writeVarInt(et.ordinal()); + } + } + } + break; + } + case FLUIDS: { + List tags = tagsMap.get(type); + + // Tag type + writer.writeSizedString(type.getIdentifier()); + + // Number of tags + writer.writeVarInt(tags.size()); + for (Tag tag : tags) { + // name + writer.writeSizedString(tag.getName().toString()); + + final Set values = tag.getValues(); + // count + writer.writeVarInt(values.size()); + // entries + for (NamespaceID name : values) { + writer.writeVarInt(Registries.getFluid(name).ordinal()); + } + } + break; + } + case GAME_EVENTS: { + List tags = tagsMap.get(type); + + // Tag type + writer.writeSizedString(type.getIdentifier()); + + // Number of tags + writer.writeVarInt(tags.size()); + for (Tag tag : tags) { + // name + writer.writeSizedString(tag.getName().toString()); + + final Set values = tag.getValues(); + // count + writer.writeVarInt(values.size()); + // entries + for (NamespaceID name : values) { + // TODO: GameEvents + writer.writeVarInt(-1); + } + } + break; + } + case ITEMS: { + List tags = tagsMap.get(type); + + // Tag type + writer.writeSizedString(type.getIdentifier()); + + // Number of tags + writer.writeVarInt(tags.size()); + for (Tag tag : tags) { + // name + writer.writeSizedString(tag.getName().toString()); + + final Set values = tag.getValues(); + // count + writer.writeVarInt(values.size()); + // entries + for (NamespaceID name : values) { + writer.writeVarInt(Registries.getMaterial(name).ordinal()); + } + } + break; } } - }); + } } @Override public void read(@NotNull BinaryReader reader) { + // TODO: revamp this. this.tagsMap = new HashMap<>(); final int typeCount = reader.readVarInt(); for (int i = 0; i < typeCount; i++) { @@ -67,7 +175,8 @@ public class TagsPacket implements ServerPacket { } tags.add(new Tag(NamespaceID.from(identifier), values)); - this.tagsMap.put(identifier, tags); + // TODO: Convert identifier to TagType +// this.tagsMap.put(identifier, tags); } }