-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
SpigotUtil.java
108 lines (96 loc) · 3.45 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package net.citizensnpcs.api.util;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;
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;
import com.google.common.primitives.Longs;
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;
}
public 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 void main(String[] args) {
System.out.println(parseDuration("1234", null));
}
public static Duration parseDuration(String raw, TimeUnit defaultUnits) {
if (defaultUnits == null) {
Integer ticks = Ints.tryParse(raw);
if (ticks != null) {
return Duration.ofMillis(ticks * 50);
}
} else if (NUMBER_MATCHER.matcher(raw).matches()) {
return Duration.of(Longs.tryParse(raw), toChronoUnit(defaultUnits));
}
if (raw.endsWith("t")) {
return Duration.ofMillis(Integer.parseInt(raw.substring(0, raw.length() - 1)) * 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 ChronoUnit toChronoUnit(TimeUnit tu) {
switch (tu) {
case NANOSECONDS:
return ChronoUnit.NANOS;
case MICROSECONDS:
return ChronoUnit.MICROS;
case MILLISECONDS:
return ChronoUnit.MILLIS;
case SECONDS:
return ChronoUnit.SECONDS;
case MINUTES:
return ChronoUnit.MINUTES;
case HOURS:
return ChronoUnit.HOURS;
case DAYS:
return ChronoUnit.DAYS;
default:
throw new AssertionError();
}
}
private static int[] BUKKIT_VERSION = null;
private static Pattern NUMBER_MATCHER = Pattern.compile("(\\d+)");
private static boolean SUPPORT_WORLD_HEIGHT = true;
private static Boolean using1_13API;
}