Deduplicate more entitytype code

This commit is contained in:
KennyTV 2021-02-16 12:24:35 +01:00
parent 1da0b38849
commit 01277d147e
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
6 changed files with 49 additions and 103 deletions

View File

@ -1,10 +1,6 @@
package us.myles.ViaVersion.api.entities;
import us.myles.ViaVersion.api.Via;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import us.myles.ViaVersion.util.EntityTypeUtil;
public enum Entity1_14Types implements EntityType {
@ -207,23 +203,10 @@ public enum Entity1_14Types implements EntityType {
}
static {
List<Entity1_14Types> types = new ArrayList<>();
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]);
TYPES = EntityTypeUtil.toOrderedArray(values());
}
public static EntityType getTypeFromId(int typeId) {
EntityType type;
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;
return EntityTypeUtil.getTypeFromId(TYPES, typeId, ENTITY);
}
}

View File

@ -1,10 +1,6 @@
package us.myles.ViaVersion.api.entities;
import us.myles.ViaVersion.api.Via;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import us.myles.ViaVersion.util.EntityTypeUtil;
public enum Entity1_15Types implements EntityType {
@ -209,23 +205,10 @@ public enum Entity1_15Types implements EntityType {
}
static {
List<Entity1_15Types> types = new ArrayList<>();
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]);
TYPES = EntityTypeUtil.toOrderedArray(values());
}
public static EntityType getTypeFromId(int typeId) {
EntityType type;
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;
return EntityTypeUtil.getTypeFromId(TYPES, typeId, ENTITY);
}
}

View File

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

View File

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

View File

@ -1,11 +1,6 @@
package us.myles.ViaVersion.api.entities;
import us.myles.ViaVersion.api.Via;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import us.myles.ViaVersion.util.EntityTypeUtil;
public enum Entity1_17Types implements EntityType {
@ -222,23 +217,10 @@ public enum Entity1_17Types implements EntityType {
}
static {
List<Entity1_17Types> types = new ArrayList<>();
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]);
TYPES = EntityTypeUtil.toOrderedArray(values());
}
public static EntityType getTypeFromId(int typeId) {
EntityType type;
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;
return EntityTypeUtil.getTypeFromId(TYPES, typeId, ENTITY);
}
}

View File

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