Finish TagsPacket read method

This commit is contained in:
Articdive 2021-06-10 09:45:30 +02:00
parent 29bac6fe24
commit 9ae8c96091
No known key found for this signature in database
GPG Key ID: B069585F0F7D90DE
4 changed files with 163 additions and 29 deletions

View File

@ -108,6 +108,13 @@ public final class BlockGenerator extends MinestomCodeGenerator {
.addAnnotation(NotNull.class)
.build()
);
// static field
blockClass.addField(
FieldSpec.builder(ArrayTypeName.of(blockClassName), "VALUES")
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
.initializer("values()")
.build()
);
// Block constructor
blockClass.addMethod(
MethodSpec.constructorBuilder()
@ -279,6 +286,19 @@ public final class BlockGenerator extends MinestomCodeGenerator {
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.build()
);
// fromId Method
blockClass.addMethod(
MethodSpec.methodBuilder("fromId")
.returns(blockClassName)
.addAnnotation(Nullable.class)
.addParameter(TypeName.SHORT, "id")
.beginControlFlow("if(id >= 0 && id < VALUES.length)")
.addStatement("return VALUES[id]")
.endControlFlow()
.addStatement("return null")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.build()
);
// toString method
blockClass.addMethod(
MethodSpec.methodBuilder("toString")

View File

@ -2362,6 +2362,8 @@ public enum Block implements Keyed {
POTTED_FLOWERING_AZALEA(NamespaceID.from("minecraft:potted_flowering_azalea_bush"), (short) 20341, 0.0, 0.0, false, false, false, true);
private static final Block[] VALUES = values();
static {
GrassBlock.initStates();
Podzol.initStates();
@ -3028,6 +3030,14 @@ public enum Block implements Keyed {
return BlockArray.blocks[blockStateId];
}
@Nullable
public static Block fromId(short id) {
if(id >= 0 && id < VALUES.length) {
return VALUES[id];
}
return null;
}
@NotNull
@Override
public String toString() {

View File

@ -1,6 +1,8 @@
package net.minestom.server.gamedata.tags;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.FileNotFoundException;
import java.util.HashSet;
@ -102,14 +104,26 @@ public class Tag {
ENTITY_TYPES("minecraft:entity_type"),
GAME_EVENTS("minecraft:game_event");
private final static BasicTypes[] VALUES = values();
private final String identifier;
BasicTypes(String identifier) {
BasicTypes(@NotNull String identifier) {
this.identifier = identifier;
}
@NotNull
public String getIdentifier() {
return identifier;
}
@Nullable
public static BasicTypes fromIdentifer(@NotNull String identifier) {
for (BasicTypes value : VALUES) {
if (value.identifier.equals(identifier)) {
return value;
}
}
return null;
}
}
}

View File

@ -2,8 +2,10 @@ 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;
@ -13,18 +15,17 @@ import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.function.Function;
public class TagsPacket implements ServerPacket {
public Map<Tag.BasicTypes, List<Tag>> tagsMap = new HashMap<>();
private static final TagsPacket REQUIRED_TAGS_PACKET = new TagsPacket();
static {
MinecraftServer.getTagManager().addRequiredTagsToPacket(REQUIRED_TAGS_PACKET);
}
public Map<Tag.BasicTypes, List<Tag>> tagsMap = new HashMap<>();
/**
* Default constructor, required for reflection operations.
*/
@ -160,38 +161,127 @@ public class TagsPacket implements ServerPacket {
@Override
public void read(@NotNull BinaryReader reader) {
// TODO: revamp this.
this.tagsMap = new HashMap<>();
// Read amount of tag types
final int typeCount = reader.readVarInt();
for (int i = 0; i < typeCount; i++) {
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);
// Read tag type
final Tag.BasicTypes tagType = Tag.BasicTypes.fromIdentifer(reader.readSizedString(Integer.MAX_VALUE));
if (tagType == null) {
throw new IllegalArgumentException("Tag type could not be resolved");
}
switch (tagType) {
case BLOCKS: {
// Read tag ID
final String identifier = reader.readSizedString(Integer.MAX_VALUE);
List<Tag> tags = new ArrayList<>();
// Read amount of tags
final int tagCount = reader.readVarInt();
Set<NamespaceID> values = new HashSet<>();
// Read tags
for (int j = 0; j < tagCount; j++) {
int protocolID = reader.readVarInt();
Block b = Block.fromId((short) protocolID);
if (b == null) {
throw new IllegalArgumentException("Block with id '" + protocolID + "' could not be resolved for a tag.");
} else {
values.add(NamespaceID.from(b.getName()));
}
}
tags.add(new Tag(NamespaceID.from(identifier), values));
// TODO: Convert identifier to TagType
// this.tagsMap.put(identifier, tags);
}
}
tags.add(new Tag(NamespaceID.from(identifier), values));
this.tagsMap.put(Tag.BasicTypes.BLOCKS, tags);
break;
}
case ENTITY_TYPES: {
// Read tag ID
final String identifier = reader.readSizedString(Integer.MAX_VALUE);
List<Tag> tags = new ArrayList<>();
// Read amount of tags
final int tagCount = reader.readVarInt();
Set<NamespaceID> values = new HashSet<>();
// Read tags
for (int j = 0; j < tagCount; j++) {
int protocolID = reader.readVarInt();
EntityType et = EntityType.fromId((short) protocolID);
if (et == null) {
throw new IllegalArgumentException("Entity type with id '" + protocolID + "' could not be resolved for a tag.");
} else {
values.add(et.getNamespaceID());
}
}
private void writeTags(BinaryWriter writer, List<Tag> tags, Function<NamespaceID, Integer> idSupplier) {
writer.writeVarInt(tags.size());
for (Tag tag : tags) {
// name
writer.writeSizedString(tag.getName().toString());
tags.add(new Tag(NamespaceID.from(identifier), values));
this.tagsMap.put(Tag.BasicTypes.ENTITY_TYPES, tags);
break;
}
case FLUIDS: {
// Read tag ID
final String identifier = reader.readSizedString(Integer.MAX_VALUE);
List<Tag> tags = new ArrayList<>();
// Read amount of tags
final int tagCount = reader.readVarInt();
Set<NamespaceID> values = new HashSet<>();
// Read tags
for (int j = 0; j < tagCount; j++) {
int protocolID = reader.readVarInt();
Fluid f = Fluid.fromId((short) protocolID);
if (f == null) {
throw new IllegalArgumentException("Fluid with id '" + protocolID + "' could not be resolved for a tag.");
} else {
values.add(f.getNamespaceID());
}
}
final Set<NamespaceID> values = tag.getValues();
// count
writer.writeVarInt(values.size());
// entries
for (NamespaceID name : values) {
writer.writeVarInt(idSupplier.apply(name));
tags.add(new Tag(NamespaceID.from(identifier), values));
this.tagsMap.put(Tag.BasicTypes.FLUIDS, tags);
break;
}
case GAME_EVENTS: {
// Read tag ID
final String identifier = reader.readSizedString(Integer.MAX_VALUE);
List<Tag> tags = new ArrayList<>();
// Read amount of tags
final int tagCount = reader.readVarInt();
Set<NamespaceID> values = new HashSet<>();
// Read tags
for (int j = 0; j < tagCount; j++) {
int protocolID = reader.readVarInt();
// TODO: GameEvent
// GameEvent ge = GameEvent.fromId((short) protocolID);
// if (ge == null) {
// throw new IllegalArgumentException("Game event with id '" + protocolID + "' could not be resolved for a tag.");
// } else {
// values.add(ge.getNamespaceID());
// }
}
tags.add(new Tag(NamespaceID.from(identifier), values));
this.tagsMap.put(Tag.BasicTypes.GAME_EVENTS, tags);
break;
}
case ITEMS: {
// Read tag ID
final String identifier = reader.readSizedString(Integer.MAX_VALUE);
List<Tag> tags = new ArrayList<>();
// Read amount of tags
final int tagCount = reader.readVarInt();
Set<NamespaceID> values = new HashSet<>();
// Read tags
for (int j = 0; j < tagCount; j++) {
int protocolID = reader.readVarInt();
Material m = Material.fromId((short) protocolID);
if (m == null) {
throw new IllegalArgumentException("Item with id '" + protocolID + "' could not be resolved for a tag.");
} else {
values.add(m.getNamespaceID());
}
}
tags.add(new Tag(NamespaceID.from(identifier), values));
this.tagsMap.put(Tag.BasicTypes.ITEMS, tags);
break;
}
}
}
}