WIP tags update

This commit is contained in:
TheMode 2021-06-07 13:25:04 +02:00
parent d5987f91b5
commit 0da9c20b5a
3 changed files with 69 additions and 67 deletions

View File

@ -38,6 +38,7 @@ public class Tag {
/** /**
* Creates a new tag with the contents of the container * 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', * @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 * appends the contents of that pack to the one being constructed
@ -46,12 +47,12 @@ public class Tag {
public Tag(TagManager manager, NamespaceID name, String type, Tag lowerPriority, TagContainer container) throws FileNotFoundException { public Tag(TagManager manager, NamespaceID name, String type, Tag lowerPriority, TagContainer container) throws FileNotFoundException {
this.name = name; this.name = name;
values = new HashSet<>(); values = new HashSet<>();
if(!container.replace) { if (!container.replace) {
values.addAll(lowerPriority.values); values.addAll(lowerPriority.values);
} }
Objects.requireNonNull(container.values, "Attempted to load from a TagContainer with no 'values' array"); Objects.requireNonNull(container.values, "Attempted to load from a TagContainer with no 'values' array");
for(String line : container.values) { for (String line : container.values) {
if(line.startsWith("#")) { // pull contents from a tag if (line.startsWith("#")) { // pull contents from a tag
Tag subtag = manager.load(NamespaceID.from(line.substring(1)), type); Tag subtag = manager.load(NamespaceID.from(line.substring(1)), type);
values.addAll(subtag.values); values.addAll(subtag.values);
} else { } else {
@ -68,6 +69,7 @@ public class Tag {
/** /**
* Checks whether the given id in inside this tag * Checks whether the given id in inside this tag
*
* @param id the id to check against * @param id the id to check against
* @return 'true' iif this tag contains the given id * @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 * Returns an immutable set of values present in this tag
*
* @return immutable set of values present in this tag * @return immutable set of values present in this tag
*/ */
public Set<NamespaceID> getValues() { public Set<NamespaceID> getValues() {
@ -85,6 +88,7 @@ public class Tag {
/** /**
* Returns the name of this tag * Returns the name of this tag
*
* @return * @return
*/ */
public NamespaceID getName() { public NamespaceID getName() {
@ -92,9 +96,20 @@ public class Tag {
} }
public enum BasicTypes { public enum BasicTypes {
BLOCKS, BLOCKS("minecraft:block"),
ITEMS, ITEMS("minecraft:item"),
FLUIDS, FLUIDS("minecraft:fluid"),
ENTITY_TYPES 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;
}
} }
} }

View File

@ -13,6 +13,7 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.Reader; import java.io.Reader;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -250,28 +251,14 @@ public class TagManager {
/** /**
* Adds the required tags for the game to function correctly * 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) { for (RequiredTag requiredTag : requiredTags) {
Tag tag = silentLoad(requiredTag.getName(), requiredTag.getType().name().toLowerCase()); final Tag tag = silentLoad(requiredTag.getName(), requiredTag.getType().name().toLowerCase());
switch (requiredTag.getType()) { final String identifier = requiredTag.getType().getIdentifier();
case BLOCKS: var map = tagsPacket.tagsMap.computeIfAbsent(identifier, s -> new ArrayList<>());
tags.blockTags.add(tag); map.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;
}
} }
} }

View File

@ -1,31 +1,20 @@
package net.minestom.server.network.packet.server.play; package net.minestom.server.network.packet.server.play;
import net.minestom.server.MinecraftServer; 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.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.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier; 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.NamespaceID;
import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.HashSet; import java.util.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
public class TagsPacket implements ServerPacket { public class TagsPacket implements ServerPacket {
public List<Tag> blockTags = new LinkedList<>(); public Map<String, List<Tag>> tagsMap = new HashMap<>();
public List<Tag> itemTags = new LinkedList<>();
public List<Tag> fluidTags = new LinkedList<>();
public List<Tag> entityTags = new LinkedList<>();
private static final TagsPacket REQUIRED_TAGS_PACKET = new TagsPacket(); private static final TagsPacket REQUIRED_TAGS_PACKET = new TagsPacket();
@ -36,22 +25,50 @@ public class TagsPacket implements ServerPacket {
/** /**
* Default constructor, required for reflection operations. * Default constructor, required for reflection operations.
*/ */
public TagsPacket() {} public TagsPacket() {
}
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writeTags(writer, blockTags, name -> Registries.getBlock(name).ordinal()); writer.writeVarInt(tagsMap.size());
writeTags(writer, itemTags, name -> Registries.getMaterial(name).ordinal()); tagsMap.forEach((s, tags) -> {
writeTags(writer, fluidTags, name -> Registries.getFluid(name).ordinal()); writer.writeSizedString(s);
writeTags(writer, entityTags, name -> Registries.getEntityType(name).ordinal());
writer.writeVarInt(tags.size());
for (Tag tag : tags) {
// name
writer.writeSizedString(tag.getName().toString());
final Set<NamespaceID> values = tag.getValues();
// count
writer.writeVarInt(values.size());
// entries
for (NamespaceID name : values) {
// TODO id from namespace
writer.writeVarInt(0);
}
}
});
} }
@Override @Override
public void read(@NotNull BinaryReader reader) { public void read(@NotNull BinaryReader reader) {
readTags(reader, blockTags, id -> NamespaceID.from("minecraft", Block.values()[id].getName())); this.tagsMap = new HashMap<>();
readTags(reader, itemTags, id -> NamespaceID.from("minecraft", Material.values()[id].getName())); final int typeCount = reader.readVarInt();
readTags(reader, fluidTags, id -> Fluid.fromId(id.shortValue()).getNamespaceID()); for (int i = 0; i < typeCount; i++) {
readTags(reader, entityTags, id -> NamespaceID.from(EntityType.values()[id].getNamespaceID())); final String identifier = reader.readSizedString(Integer.MAX_VALUE);
List<Tag> tags = new ArrayList<>();
final int tagCount = reader.readVarInt();
Set<NamespaceID> 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<Tag> tags, Function<NamespaceID, Integer> idSupplier) { private void writeTags(BinaryWriter writer, List<Tag> tags, Function<NamespaceID, Integer> idSupplier) {
@ -70,23 +87,6 @@ public class TagsPacket implements ServerPacket {
} }
} }
public void readTags(BinaryReader reader, List<Tag> output, Function<Integer, NamespaceID> 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<NamespaceID> 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 @Override
public int getId() { public int getId() {
return ServerPacketIdentifier.TAGS; return ServerPacketIdentifier.TAGS;