3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Deduplicate more entitytype code

Dieser Commit ist enthalten in:
KennyTV 2021-02-16 12:24:35 +01:00
Ursprung 1da0b38849
Commit 01277d147e
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
6 geänderte Dateien mit 49 neuen und 103 gelöschten Zeilen

Datei anzeigen

@ -1,10 +1,6 @@
package us.myles.ViaVersion.api.entities; package us.myles.ViaVersion.api.entities;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.util.EntityTypeUtil;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public enum Entity1_14Types implements EntityType { public enum Entity1_14Types implements EntityType {
@ -207,23 +203,10 @@ public enum Entity1_14Types implements EntityType {
} }
static { static {
List<Entity1_14Types> types = new ArrayList<>(); TYPES = EntityTypeUtil.toOrderedArray(values());
for (Entity1_14Types type : values()) {
if (type.id != -1) {
types.add(type);
}
}
types.sort(Comparator.comparingInt(Entity1_14Types::getId));
TYPES = types.toArray(new EntityType[0]);
} }
public static EntityType getTypeFromId(int typeId) { public static EntityType getTypeFromId(int typeId) {
EntityType type; return EntityTypeUtil.getTypeFromId(TYPES, typeId, ENTITY);
if (typeId < 0 || typeId >= TYPES.length || (type = TYPES[typeId]) == null) {
Via.getPlatform().getLogger().severe("Could not find 1.14 type id " + typeId);
return ENTITY;
}
return type;
} }
} }

Datei anzeigen

@ -1,10 +1,6 @@
package us.myles.ViaVersion.api.entities; package us.myles.ViaVersion.api.entities;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.util.EntityTypeUtil;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public enum Entity1_15Types implements EntityType { public enum Entity1_15Types implements EntityType {
@ -209,23 +205,10 @@ public enum Entity1_15Types implements EntityType {
} }
static { static {
List<Entity1_15Types> types = new ArrayList<>(); TYPES = EntityTypeUtil.toOrderedArray(values());
for (Entity1_15Types type : values()) {
if (type.id != -1) {
types.add(type);
}
}
types.sort(Comparator.comparingInt(Entity1_15Types::getId));
TYPES = types.toArray(new EntityType[0]);
} }
public static EntityType getTypeFromId(int typeId) { public static EntityType getTypeFromId(int typeId) {
EntityType type; return EntityTypeUtil.getTypeFromId(TYPES, typeId, ENTITY);
if (typeId < 0 || typeId >= TYPES.length || (type = TYPES[typeId]) == null) {
Via.getPlatform().getLogger().severe("Could not find 1.15 type id " + typeId);
return ENTITY;
}
return type;
} }
} }

Datei anzeigen

@ -1,10 +1,6 @@
package us.myles.ViaVersion.api.entities; package us.myles.ViaVersion.api.entities;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.util.EntityTypeUtil;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public enum Entity1_16Types implements EntityType { public enum Entity1_16Types implements EntityType {
@ -214,23 +210,10 @@ public enum Entity1_16Types implements EntityType {
} }
static { static {
List<Entity1_16Types> types = new ArrayList<>(); TYPES = EntityTypeUtil.toOrderedArray(values());
for (Entity1_16Types type : values()) {
if (type.id != -1) {
types.add(type);
}
} }
types.sort(Comparator.comparingInt(Entity1_16Types::getId)); public static EntityType getTypeFromId(int typeId) {
TYPES = types.toArray(new EntityType[0]); return EntityTypeUtil.getTypeFromId(TYPES, typeId, ENTITY);
}
public static us.myles.ViaVersion.api.entities.EntityType getTypeFromId(int typeId) {
us.myles.ViaVersion.api.entities.EntityType type;
if (typeId < 0 || typeId >= TYPES.length || (type = TYPES[typeId]) == null) {
Via.getPlatform().getLogger().severe("Could not find 1.16 type id " + typeId);
return ENTITY;
}
return type;
} }
} }

Datei anzeigen

@ -1,10 +1,6 @@
package us.myles.ViaVersion.api.entities; package us.myles.ViaVersion.api.entities;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.util.EntityTypeUtil;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public enum Entity1_16_2Types implements EntityType { public enum Entity1_16_2Types implements EntityType {
@ -218,23 +214,10 @@ public enum Entity1_16_2Types implements EntityType {
} }
static { static {
List<Entity1_16_2Types> types = new ArrayList<>(); TYPES = EntityTypeUtil.toOrderedArray(values());
for (Entity1_16_2Types type : values()) {
if (type.id != -1) {
types.add(type);
}
} }
types.sort(Comparator.comparingInt(Entity1_16_2Types::getId)); public static EntityType getTypeFromId(int typeId) {
TYPES = types.toArray(new EntityType[0]); return EntityTypeUtil.getTypeFromId(TYPES, typeId, ENTITY);
}
public static us.myles.ViaVersion.api.entities.EntityType getTypeFromId(int typeId) {
EntityType type;
if (typeId < 0 || typeId >= TYPES.length || (type = TYPES[typeId]) == null) {
Via.getPlatform().getLogger().severe("Could not find 1.16.2 type id " + typeId);
return ENTITY;
}
return type;
} }
} }

Datei anzeigen

@ -1,11 +1,6 @@
package us.myles.ViaVersion.api.entities; package us.myles.ViaVersion.api.entities;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.util.EntityTypeUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public enum Entity1_17Types implements EntityType { public enum Entity1_17Types implements EntityType {
@ -222,23 +217,10 @@ public enum Entity1_17Types implements EntityType {
} }
static { static {
List<Entity1_17Types> types = new ArrayList<>(); TYPES = EntityTypeUtil.toOrderedArray(values());
for (Entity1_17Types type : values()) {
if (type.id != -1) {
types.add(type);
}
}
types.sort(Comparator.comparingInt(Entity1_17Types::getId));
TYPES = types.toArray(new EntityType[0]);
} }
public static EntityType getTypeFromId(int typeId) { public static EntityType getTypeFromId(int typeId) {
EntityType type; return EntityTypeUtil.getTypeFromId(TYPES, typeId, ENTITY);
if (typeId < 0 || typeId >= TYPES.length || (type = TYPES[typeId]) == null) {
Via.getPlatform().getLogger().severe("Could not find 1.17 type id " + typeId);
return ENTITY;
}
return type;
} }
} }

Datei anzeigen

@ -0,0 +1,32 @@
package us.myles.ViaVersion.util;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.entities.EntityType;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class EntityTypeUtil {
public static EntityType[] toOrderedArray(EntityType[] values) {
List<EntityType> types = new ArrayList<>();
for (EntityType type : values) {
if (type.getId() != -1) {
types.add(type);
}
}
types.sort(Comparator.comparingInt(EntityType::getId));
return types.toArray(new EntityType[0]);
}
public static EntityType getTypeFromId(EntityType[] values, int typeId, EntityType fallback) {
EntityType type;
if (typeId < 0 || typeId >= values.length || (type = values[typeId]) == null) {
Via.getPlatform().getLogger().severe("Could not find " + fallback.getClass().getSimpleName() + " type id " + typeId);
return fallback;
}
return type;
}
}