mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-31 21:48:08 +01:00
Returning null as not existing entity type, optimized EntityType#fromId
This commit is contained in:
parent
ebb061ae39
commit
54e1f59b2e
@ -337,6 +337,8 @@ public enum EntityType {
|
||||
|
||||
FISHING_BOBBER("minecraft:fishing_bobber", 0.25, 0.25, FishingHookMeta::new);
|
||||
|
||||
private static final EntityType[] VALUES = values();
|
||||
|
||||
@NotNull
|
||||
private String namespaceID;
|
||||
|
||||
@ -377,9 +379,9 @@ public enum EntityType {
|
||||
}
|
||||
|
||||
public static EntityType fromId(short id) {
|
||||
if(id >= 0 && id < values().length) {
|
||||
return values()[id];
|
||||
if(id >= 0 && id < VALUES.length) {
|
||||
return VALUES[id];
|
||||
}
|
||||
return PIG;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ public class EnumGenerator implements CodeGenerator {
|
||||
private final String enumName;
|
||||
private ParameterSpec[] parameters;
|
||||
private List<Method> methods = new LinkedList<>();
|
||||
private List<Field> fields = new LinkedList<>();
|
||||
private List<Instance> instances = new LinkedList<>();
|
||||
private List<Field> hardcodedFields = new LinkedList<>();
|
||||
private List<AnnotationSpec> annotations = new LinkedList<>();
|
||||
@ -50,6 +51,10 @@ public class EnumGenerator implements CodeGenerator {
|
||||
methods.add(new Method(true, name, signature, returnType, code, false));
|
||||
}
|
||||
|
||||
public void addStaticField(TypeName type, String name, String value) {
|
||||
fields.add(new Field(type, name, value));
|
||||
}
|
||||
|
||||
public void addInstance(String name, Object... parameters) {
|
||||
instances.add(new Instance(name, parameters));
|
||||
}
|
||||
@ -95,6 +100,13 @@ public class EnumGenerator implements CodeGenerator {
|
||||
.build());
|
||||
}
|
||||
|
||||
for (Field field : fields) {
|
||||
enumClass.addField(FieldSpec.builder(field.type, field.name)
|
||||
.initializer("$L", field.value)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC)
|
||||
.build());
|
||||
}
|
||||
|
||||
// hard coded fields
|
||||
for (Field hardcoded : hardcodedFields) {
|
||||
enumClass.addField(FieldSpec.builder(hardcoded.type, hardcoded.name)
|
||||
|
@ -8,6 +8,7 @@ import net.minestom.codegen.ConstructorLambda;
|
||||
import net.minestom.codegen.EnumGenerator;
|
||||
import net.minestom.codegen.MinestomEnumGenerator;
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.entity.Metadata;
|
||||
import net.minestom.server.entity.metadata.EntityMeta;
|
||||
import net.minestom.server.registry.Registries;
|
||||
@ -24,7 +25,6 @@ import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Generates an EntityType enum containing all data about entity types
|
||||
@ -164,11 +164,13 @@ public class EntityTypeEnumGenerator extends MinestomEnumGenerator<EntityTypeCon
|
||||
code -> code.addStatement("return this.metaConstructor")
|
||||
);
|
||||
|
||||
generator.addStaticField(ArrayTypeName.of(ClassName.get(EntityType.class)), "VALUES", "values()");
|
||||
|
||||
generator.addStaticMethod("fromId", new ParameterSpec[]{ParameterSpec.builder(TypeName.SHORT, "id").build()}, className, code -> {
|
||||
code.beginControlFlow("if(id >= 0 && id < values().length)")
|
||||
.addStatement("return values()[id]")
|
||||
code.beginControlFlow("if(id >= 0 && id < VALUES.length)")
|
||||
.addStatement("return VALUES[id]")
|
||||
.endControlFlow()
|
||||
.addStatement("return PIG");
|
||||
.addStatement("return null");
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user