/
SpigotUtil.java
72 lines (63 loc) · 2.29 KB
/
SpigotUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package net.citizensnpcs.api.util;
import java.time.Duration;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import com.google.common.primitives.Ints;
public class SpigotUtil {
public static boolean checkYSafe(double y, World world) {
if (!SUPPORT_WORLD_HEIGHT || world == null) {
return y >= 0 && y <= 255;
}
try {
return y >= world.getMinHeight() && y <= world.getMaxHeight();
} catch (Throwable t) {
SUPPORT_WORLD_HEIGHT = false;
return y >= 0 && y <= 255;
}
}
public static int getMaxNameLength(EntityType type) {
return isUsing1_13API() ? 256 : 64;
}
private static int[] getVersion() {
if (BUKKIT_VERSION == null) {
String version = Bukkit.getVersion();
if (version == null || version.isEmpty()) {
return new int[] { 1, 8 };
}
String[] parts = version.split("_");
return BUKKIT_VERSION = new int[] { Integer.parseInt(parts[0]), Integer.parseInt(parts[1]) };
}
return BUKKIT_VERSION;
}
public static boolean isUsing1_13API() {
if (using1_13API == null) {
try {
Enchantment.getByKey(Enchantment.ARROW_DAMAGE.getKey());
using1_13API = true;
} catch (Exception ex) {
using1_13API = false;
} catch (NoSuchMethodError ex) {
using1_13API = false;
}
}
return using1_13API;
}
public static Duration parseDuration(String raw) {
Integer ticks = Ints.tryParse(raw.endsWith("t") ? raw.substring(0, raw.length() - 1) : raw);
if (ticks != null) {
return Duration.ofMillis(ticks * 50);
}
raw = NUMBER_MATCHER.matcher(raw).replaceFirst("P$1T").replace("min", "m").replace("hr", "h");
if (raw.charAt(0) != 'P') {
raw = "PT" + raw;
}
return Duration.parse(raw);
}
private static int[] BUKKIT_VERSION = null;
private static Pattern NUMBER_MATCHER = Pattern.compile("(\\d+d)");
private static boolean SUPPORT_WORLD_HEIGHT = true;
private static Boolean using1_13API;
}