Skip to content

Commit 7502272

Browse files
committed
feat: add VersionUtil and EntityUtil
1 parent 5617f1d commit 7502272

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.comphenix.packetwrapper.util;
2+
3+
import org.bukkit.Bukkit;
4+
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
public final class VersionUtil {
9+
10+
private static final String OVERRIDDEN_VERSION_SYSTEM_PROPERTY_NAME = VersionUtil.class.getName() + ".version";
11+
12+
private static final Pattern VERSION_PATTERN = Pattern.compile("v(\\d+)_(\\d+)_R(\\d+)");
13+
14+
private static final int MAJOR_VERSION, MINOR_VERSION, BUILD_VERSION;
15+
16+
static {
17+
String version;
18+
if ((version = System.getProperty(OVERRIDDEN_VERSION_SYSTEM_PROPERTY_NAME)) == null) {
19+
version = Bukkit.getServer().getClass().getPackage().getName();
20+
version = version.substring(version.lastIndexOf('.') + 1);
21+
}
22+
23+
// version is in format like `v1_12_R1`
24+
final Matcher matcher;
25+
if (!(matcher = Pattern.compile("v(\\d+)_(\\d+)_R(\\d+)").matcher(version))
26+
.matches()) throw new RuntimeException("Unknown version: \"" + version + "\"");
27+
28+
MAJOR_VERSION = Integer.parseInt(matcher.group(1));
29+
MINOR_VERSION = Integer.parseInt(matcher.group(1));
30+
BUILD_VERSION = Integer.parseInt(matcher.group(1));
31+
}
32+
33+
private VersionUtil() {
34+
throw new AssertionError("VersionUtil cannot be instantiated");
35+
}
36+
37+
public static int getMajorVersion() {
38+
return MAJOR_VERSION;
39+
}
40+
41+
public static int getMinorVersion() {
42+
return MINOR_VERSION;
43+
}
44+
45+
public static int getBuildVersion() {
46+
return BUILD_VERSION;
47+
}
48+
}

0 commit comments

Comments
 (0)