|
| 1 | +package com.comphenix.packetwrapper.util; |
| 2 | + |
| 3 | +import org.bukkit.entity.EntityType; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | + |
| 6 | +import java.util.*; |
| 7 | +import java.util.function.Function; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +/** |
| 11 | + * Utilities related to entities. |
| 12 | + */ |
| 13 | +@SuppressWarnings("deprecation") // legacy methods |
| 14 | +public final class EntityUtil { |
| 15 | + |
| 16 | + private static final int MINOR_VERSION; |
| 17 | + private static final Map<EntityType, Integer> ENTITY_TYPE_IDS; |
| 18 | + private static final Map<Integer, EntityType> ENTITY_TYPES; |
| 19 | + |
| 20 | + static { |
| 21 | + { |
| 22 | + final int majorVersion; |
| 23 | + if ((majorVersion = VersionUtil.getMajorVersion()) != 1) throw new Error( |
| 24 | + "Major Minecraft version " + majorVersion + " is unsupported" |
| 25 | + ); |
| 26 | + } |
| 27 | + MINOR_VERSION = VersionUtil.getMinorVersion(); |
| 28 | + |
| 29 | + if (MINOR_VERSION >= 13) { |
| 30 | + final Collection<EntityType> entityTypes = EnumSet.allOf(EntityType.class); |
| 31 | + entityTypes.remove(EntityType.UNKNOWN); |
| 32 | + entityTypes.remove(EntityType.PLAYER); |
| 33 | + entityTypes.remove(EntityType.FISHING_HOOK); |
| 34 | + |
| 35 | + final int[] id = {0}; |
| 36 | + ENTITY_TYPE_IDS = entityTypes |
| 37 | + .stream() |
| 38 | + .map(Enum::name) |
| 39 | + .sorted() |
| 40 | + .collect(Collectors.toMap(EntityType::valueOf, name -> id[0]++, (left, right) -> { |
| 41 | + throw new AssertionError("Enums cannot have duplicate names"); |
| 42 | + }, () -> new EnumMap<>(EntityType.class))); |
| 43 | + ENTITY_TYPE_IDS.put(EntityType.PLAYER, id[0]++); |
| 44 | + ENTITY_TYPE_IDS.put(EntityType.FISHING_HOOK, id[0]++); |
| 45 | + ENTITY_TYPE_IDS.put(EntityType.UNKNOWN, -1); |
| 46 | + } else ENTITY_TYPE_IDS = Arrays.stream(EntityType.values()) |
| 47 | + .collect(Collectors.toMap(Function.identity(), type -> (int) type.getTypeId(), (left, right) -> { |
| 48 | + throw new AssertionError("Enums cannot have duplicate names"); |
| 49 | + }, () -> new EnumMap<>(EntityType.class))); |
| 50 | + |
| 51 | + ENTITY_TYPES = ENTITY_TYPE_IDS.entrySet().stream() |
| 52 | + .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); |
| 53 | + |
| 54 | + ENTITY_TYPE_IDS.put(null, -1); |
| 55 | + } |
| 56 | + |
| 57 | + private EntityUtil() { |
| 58 | + throw new AssertionError("EntityUtil cannot be instantiated"); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets the type ID of the given entity. |
| 63 | + * Note that {@link EntityType#getTypeId()} is incorrect since 1.13 as it represents legacy IDs. |
| 64 | + * It seems that sine 1.13 IDs are ordered alphabetically with some minor exceptions. |
| 65 | + * |
| 66 | + * @param entityType type for which to get the ID |
| 67 | + * @return type ID of the given entity type |
| 68 | + */ |
| 69 | + public static int getTypeId(final EntityType entityType) { |
| 70 | + return ENTITY_TYPE_IDS.get(entityType); // the value is always present |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Gets the entity type by the given ID. |
| 75 | + * Note that {@link EntityType#fromId(int)} is incorrect since 1.13 as it is based on legacy IDs. |
| 76 | + * It seems that sine 1.13 IDs are ordered alphabetically with some minor exceptions. |
| 77 | + * |
| 78 | + * @param id entity type ID |
| 79 | + * @return entity with the given type ID or {@link EntityType#UNKNOWN} if there is none |
| 80 | + */ |
| 81 | + public static @NotNull EntityType getEntityTypeById(final int id) { |
| 82 | + return ENTITY_TYPES.getOrDefault(id, EntityType.UNKNOWN); |
| 83 | + } |
| 84 | +} |
0 commit comments