From 0da9c20b5a8b86099adc9289957b17cfa4da9a65 Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 7 Jun 2021 13:25:04 +0200 Subject: [PATCH] WIP tags update --- .../minestom/server/gamedata/tags/Tag.java | 31 ++++++-- .../server/gamedata/tags/TagManager.java | 27 ++----- .../packet/server/play/TagsPacket.java | 78 +++++++++---------- 3 files changed, 69 insertions(+), 67 deletions(-) diff --git a/src/main/java/net/minestom/server/gamedata/tags/Tag.java b/src/main/java/net/minestom/server/gamedata/tags/Tag.java index 5513f0609..e7ab4bc39 100644 --- a/src/main/java/net/minestom/server/gamedata/tags/Tag.java +++ b/src/main/java/net/minestom/server/gamedata/tags/Tag.java @@ -38,7 +38,8 @@ public class Tag { /** * Creates a new tag with the contents of the container - * @param manager Used to load tag contents (as tags are valid values inside 'values') + * + * @param manager Used to load tag contents (as tags are valid values inside 'values') * @param lowerPriority Tag contents from lower priority data packs. If 'replace' is false in 'container', * appends the contents of that pack to the one being constructed * @param container @@ -46,12 +47,12 @@ public class Tag { public Tag(TagManager manager, NamespaceID name, String type, Tag lowerPriority, TagContainer container) throws FileNotFoundException { this.name = name; values = new HashSet<>(); - if(!container.replace) { + if (!container.replace) { values.addAll(lowerPriority.values); } Objects.requireNonNull(container.values, "Attempted to load from a TagContainer with no 'values' array"); - for(String line : container.values) { - if(line.startsWith("#")) { // pull contents from a tag + for (String line : container.values) { + if (line.startsWith("#")) { // pull contents from a tag Tag subtag = manager.load(NamespaceID.from(line.substring(1)), type); values.addAll(subtag.values); } else { @@ -68,6 +69,7 @@ public class Tag { /** * Checks whether the given id in inside this tag + * * @param id the id to check against * @return 'true' iif this tag contains the given id */ @@ -77,6 +79,7 @@ public class Tag { /** * Returns an immutable set of values present in this tag + * * @return immutable set of values present in this tag */ public Set getValues() { @@ -85,6 +88,7 @@ public class Tag { /** * Returns the name of this tag + * * @return */ public NamespaceID getName() { @@ -92,9 +96,20 @@ public class Tag { } public enum BasicTypes { - BLOCKS, - ITEMS, - FLUIDS, - ENTITY_TYPES + BLOCKS("minecraft:block"), + ITEMS("minecraft:item"), + FLUIDS("minecraft:fluid"), + ENTITY_TYPES("minecraft:entity_type"), + GAME_EVENT("minecraft:game_event"); + + private final String identifier; + + BasicTypes(String identifier) { + this.identifier = identifier; + } + + public String getIdentifier() { + return identifier; + } } } 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 d028d7c2e..c4029b59d 100644 --- a/src/main/java/net/minestom/server/gamedata/tags/TagManager.java +++ b/src/main/java/net/minestom/server/gamedata/tags/TagManager.java @@ -13,6 +13,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.Reader; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -250,28 +251,14 @@ public class TagManager { /** * Adds the required tags for the game to function correctly * - * @param tags the packet to add the tags to + * @param tagsPacket the packet to add the tags to */ - public void addRequiredTagsToPacket(TagsPacket tags) { + public void addRequiredTagsToPacket(TagsPacket tagsPacket) { for (RequiredTag requiredTag : requiredTags) { - Tag tag = silentLoad(requiredTag.getName(), requiredTag.getType().name().toLowerCase()); - switch (requiredTag.getType()) { - case BLOCKS: - tags.blockTags.add(tag); - break; - - case ITEMS: - tags.itemTags.add(tag); - break; - - case FLUIDS: - tags.fluidTags.add(tag); - break; - - case ENTITY_TYPES: - tags.entityTags.add(tag); - break; - } + 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<>()); + 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 b4e374314..d8490564c 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,31 +1,20 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.MinecraftServer; -import net.minestom.server.entity.EntityType; -import net.minestom.server.fluid.Fluid; import net.minestom.server.gamedata.tags.Tag; -import net.minestom.server.instance.block.Block; -import net.minestom.server.item.Material; 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; import org.jetbrains.annotations.NotNull; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.function.Function; public class TagsPacket implements ServerPacket { - public List blockTags = new LinkedList<>(); - public List itemTags = new LinkedList<>(); - public List fluidTags = new LinkedList<>(); - public List entityTags = new LinkedList<>(); + public Map> tagsMap = new HashMap<>(); private static final TagsPacket REQUIRED_TAGS_PACKET = new TagsPacket(); @@ -36,22 +25,50 @@ public class TagsPacket implements ServerPacket { /** * Default constructor, required for reflection operations. */ - public TagsPacket() {} + public TagsPacket() { + } @Override public void write(@NotNull BinaryWriter writer) { - writeTags(writer, blockTags, name -> Registries.getBlock(name).ordinal()); - writeTags(writer, itemTags, name -> Registries.getMaterial(name).ordinal()); - writeTags(writer, fluidTags, name -> Registries.getFluid(name).ordinal()); - writeTags(writer, entityTags, name -> Registries.getEntityType(name).ordinal()); + writer.writeVarInt(tagsMap.size()); + tagsMap.forEach((s, tags) -> { + writer.writeSizedString(s); + + 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 id from namespace + writer.writeVarInt(0); + } + } + }); } @Override public void read(@NotNull BinaryReader reader) { - readTags(reader, blockTags, id -> NamespaceID.from("minecraft", Block.values()[id].getName())); - readTags(reader, itemTags, id -> NamespaceID.from("minecraft", Material.values()[id].getName())); - readTags(reader, fluidTags, id -> Fluid.fromId(id.shortValue()).getNamespaceID()); - readTags(reader, entityTags, id -> NamespaceID.from(EntityType.values()[id].getNamespaceID())); + this.tagsMap = new HashMap<>(); + final int typeCount = reader.readVarInt(); + for (int i = 0; i < typeCount; i++) { + final String identifier = reader.readSizedString(Integer.MAX_VALUE); + List tags = new ArrayList<>(); + final int tagCount = reader.readVarInt(); + Set values = new HashSet<>(); + for (int j = 0; j < tagCount; j++) { + int protocolID = reader.readVarInt(); + // TODO tag from id + values.add(null); + } + + tags.add(new Tag(NamespaceID.from(identifier), values)); + this.tagsMap.put(identifier, tags); + } } private void writeTags(BinaryWriter writer, List tags, Function idSupplier) { @@ -70,23 +87,6 @@ public class TagsPacket implements ServerPacket { } } - public void readTags(BinaryReader reader, List output, Function idSupplier) { - output.clear(); - int length = reader.readVarInt(); - for (int i = 0; i < length; i++) { - String name = reader.readSizedString(Integer.MAX_VALUE); - - int count = reader.readVarInt(); - Set values = new HashSet<>(); - for (int j = 0; j < count; j++) { - int protocolID = reader.readVarInt(); - values.add(idSupplier.apply(protocolID)); - } - - output.add(new Tag(NamespaceID.from(name), values)); - } - } - @Override public int getId() { return ServerPacketIdentifier.TAGS;