Fixed PotionEffect ids

This commit is contained in:
Felix Cravic 2020-11-23 10:08:23 +01:00
parent daa72719a0
commit 38bcb755c2
10 changed files with 32 additions and 21 deletions

View File

@ -242,7 +242,7 @@ public enum EntityType {
} }
public static EntityType fromId(int id) { public static EntityType fromId(int id) {
if(id >= 0 && id < values().length) { if (id >= 0 && id < values().length) {
return values()[id]; return values()[id];
} }
return PIG; return PIG;

View File

@ -36,7 +36,7 @@ public enum Fluid {
} }
public static Fluid fromId(int id) { public static Fluid fromId(int id) {
if(id >= 0 && id < values().length) { if (id >= 0 && id < values().length) {
return values()[id]; return values()[id];
} }
return EMPTY; return EMPTY;

View File

@ -102,7 +102,7 @@ public enum Enchantment {
} }
public static Enchantment fromId(int id) { public static Enchantment fromId(int id) {
if(id >= 0 && id < values().length) { if (id >= 0 && id < values().length) {
return values()[id]; return values()[id];
} }
return null; return null;

View File

@ -170,7 +170,7 @@ public enum Particle {
} }
public static Particle fromId(int id) { public static Particle fromId(int id) {
if(id >= 0 && id < values().length) { if (id >= 0 && id < values().length) {
return values()[id]; return values()[id];
} }
return null; return null;

View File

@ -82,7 +82,7 @@ public enum PotionEffect {
} }
public int getId() { public int getId() {
return ordinal(); return ordinal() + 1;
} }
public String getNamespaceID() { public String getNamespaceID() {
@ -90,8 +90,8 @@ public enum PotionEffect {
} }
public static PotionEffect fromId(int id) { public static PotionEffect fromId(int id) {
if(id >= 0 && id < values().length) { if (id >= 0 && id < values().length + 1) {
return values()[id]; return values()[id - 1];
} }
return null; return null;
} }

View File

@ -112,7 +112,7 @@ public enum PotionType {
} }
public static PotionType fromId(int id) { public static PotionType fromId(int id) {
if(id >= 0 && id < values().length) { if (id >= 0 && id < values().length) {
return values()[id]; return values()[id];
} }
return EMPTY; return EMPTY;

View File

@ -2010,7 +2010,7 @@ public enum Sound {
} }
public static Sound fromId(int id) { public static Sound fromId(int id) {
if(id >= 0 && id < values().length) { if (id >= 0 && id < values().length) {
return values()[id]; return values()[id];
} }
return null; return null;

View File

@ -174,7 +174,7 @@ public enum StatisticType {
} }
public static StatisticType fromId(int id) { public static StatisticType fromId(int id) {
if(id >= 0 && id < values().length) { if (id >= 0 && id < values().length) {
return values()[id]; return values()[id];
} }
return null; return null;

View File

@ -20,13 +20,23 @@ public abstract class BasicEnumGenerator extends MinestomEnumGenerator<BasicEnum
private final boolean linear; private final boolean linear;
private NamespaceID defaultEntry; private NamespaceID defaultEntry;
protected BasicEnumGenerator(File targetFolder) throws IOException { /**
this(targetFolder, true); * True if the enum is linear and start by 1 instead of 0
*/
private boolean incrementOrdinal;
protected BasicEnumGenerator(File targetFolder, boolean linear, boolean incrementOrdinal) throws IOException {
this.linear = linear;
this.incrementOrdinal = incrementOrdinal;
generateTo(targetFolder);
} }
protected BasicEnumGenerator(File targetFolder, boolean linear) throws IOException { protected BasicEnumGenerator(File targetFolder, boolean linear) throws IOException {
this.linear = linear; this(targetFolder, linear, false);
generateTo(targetFolder); }
protected BasicEnumGenerator(File targetFolder) throws IOException {
this(targetFolder, true);
} }
@Override @Override
@ -59,17 +69,19 @@ public abstract class BasicEnumGenerator extends MinestomEnumGenerator<BasicEnum
ParameterSpec idParam = ParameterSpec.builder(TypeName.INT, "id").build(); ParameterSpec idParam = ParameterSpec.builder(TypeName.INT, "id").build();
ParameterSpec[] signature = new ParameterSpec[]{idParam}; ParameterSpec[] signature = new ParameterSpec[]{idParam};
if (linear) { if (linear) {
final String ordinalIncrementCondition = incrementOrdinal ? " + 1" : "";
final String ordinalIncrementIndex = incrementOrdinal ? " - 1" : "";
generator.addStaticMethod("fromId", signature, className, code -> { generator.addStaticMethod("fromId", signature, className, code -> {
code.beginControlFlow("if($N >= 0 && $N < values().length)", idParam, idParam) code.beginControlFlow("if ($N >= 0 && $N < values().length" + ordinalIncrementCondition + ")", idParam, idParam)
.addStatement("return values()[$N]", idParam) .addStatement("return values()[$N" + ordinalIncrementIndex + "]", idParam)
.endControlFlow() .endControlFlow()
.addStatement("return " + (defaultEntry == null ? "null" : identifier(defaultEntry))); .addStatement("return " + (defaultEntry == null ? "null" : identifier(defaultEntry)));
} }
); );
} else { } else {
generator.addStaticMethod("fromId", signature, className, code -> { generator.addStaticMethod("fromId", signature, className, code -> {
code.beginControlFlow("for($T o : values())") code.beginControlFlow("for ($T o : values())")
.beginControlFlow("if(o.getId() == id)") .beginControlFlow("if (o.getId() == id)")
.addStatement("return o") .addStatement("return o")
.endControlFlow() .endControlFlow()
.endControlFlow() .endControlFlow()
@ -94,7 +106,7 @@ public abstract class BasicEnumGenerator extends MinestomEnumGenerator<BasicEnum
ClassName registriesClass = ClassName.get(Registries.class); ClassName registriesClass = ClassName.get(Registries.class);
if (linear) { if (linear) {
generator.setParams(ParameterSpec.builder(ClassName.get(String.class), "namespaceID").build()); generator.setParams(ParameterSpec.builder(ClassName.get(String.class), "namespaceID").build());
generator.addMethod("getId", new ParameterSpec[0], TypeName.INT, code -> code.addStatement("return ordinal()")); generator.addMethod("getId", new ParameterSpec[0], TypeName.INT, code -> code.addStatement("return ordinal()" + (incrementOrdinal ? " + 1" : "")));
} else { } else {
generator.setParams(ParameterSpec.builder(ClassName.get(String.class), "namespaceID").build(), ParameterSpec.builder(TypeName.INT, "id").build()); generator.setParams(ParameterSpec.builder(ClassName.get(String.class), "namespaceID").build(), ParameterSpec.builder(TypeName.INT, "id").build());
generator.addMethod("getId", new ParameterSpec[0], TypeName.INT, code -> code.addStatement("return $N", "id")); generator.addMethod("getId", new ParameterSpec[0], TypeName.INT, code -> code.addStatement("return $N", "id"));

View File

@ -1,7 +1,6 @@
package net.minestom.codegen.potions; package net.minestom.codegen.potions;
import net.minestom.codegen.BasicEnumGenerator; import net.minestom.codegen.BasicEnumGenerator;
import net.minestom.codegen.stats.StatsEnumGenerator;
import net.minestom.server.registry.ResourceGatherer; import net.minestom.server.registry.ResourceGatherer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -42,7 +41,7 @@ public class PotionEffectEnumGenerator extends BasicEnumGenerator {
} }
private PotionEffectEnumGenerator(File targetFolder) throws IOException { private PotionEffectEnumGenerator(File targetFolder) throws IOException {
super(targetFolder); super(targetFolder, true, true);
} }
@Override @Override