Fix and improve tags

This commit is contained in:
Articdive 2021-06-09 21:23:35 +02:00
parent 9e64935af8
commit 2976be947b
No known key found for this signature in database
GPG Key ID: B069585F0F7D90DE
2 changed files with 126 additions and 18 deletions

View File

@ -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);
}
}

View File

@ -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<String, List<Tag>> tagsMap = new HashMap<>();
public Map<Tag.BasicTypes, List<Tag>> 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<Tag> 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<NamespaceID> 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<Tag> tags = tagsMap.get(type);
final Set<NamespaceID> 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<NamespaceID> 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<Tag> 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<NamespaceID> values = tag.getValues();
// count
writer.writeVarInt(values.size());
// entries
for (NamespaceID name : values) {
writer.writeVarInt(Registries.getFluid(name).ordinal());
}
}
break;
}
case GAME_EVENTS: {
List<Tag> 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<NamespaceID> values = tag.getValues();
// count
writer.writeVarInt(values.size());
// entries
for (NamespaceID name : values) {
// TODO: GameEvents
writer.writeVarInt(-1);
}
}
break;
}
case ITEMS: {
List<Tag> 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<NamespaceID> 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);
}
}