/
Util.java
344 lines (304 loc) · 12.9 KB
/
Util.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package net.citizensnpcs.util;
import java.util.EnumSet;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Vector;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import net.citizensnpcs.api.event.NPCCollisionEvent;
import net.citizensnpcs.api.event.NPCPushEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.SpigotUtil;
import net.citizensnpcs.npc.ai.NPCHolder;
public class Util {
// Static class for small (emphasis small) utility methods
private Util() {
}
public static void assumePose(Entity entity, float yaw, float pitch) {
NMS.look(entity, yaw, pitch);
}
public static void callCollisionEvent(NPC npc, Entity entity) {
if (NPCCollisionEvent.getHandlerList().getRegisteredListeners().length > 0) {
Bukkit.getPluginManager().callEvent(new NPCCollisionEvent(npc, entity));
}
}
public static Vector callPushEvent(NPC npc, double x, double y, double z) {
if (npc == null) {
return new Vector(x, y, z);
}
boolean allowed = !npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)
|| !npc.data().get(NPC.COLLIDABLE_METADATA, true);
if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0) {
return allowed ? new Vector(x, y, z) : null;
}
// when another entity collides, this method is called to push the NPC so we prevent it from
// doing anything if the event is cancelled.
Vector vector = new Vector(x, y, z);
NPCPushEvent event = new NPCPushEvent(npc, vector);
event.setCancelled(!allowed);
Bukkit.getPluginManager().callEvent(event);
return !event.isCancelled() ? event.getCollisionVector() : null;
}
public static float clampYaw(float yaw) {
while (yaw < -180.0F) {
yaw += 360.0F;
}
while (yaw >= 180.0F) {
yaw -= 360.0F;
}
return yaw;
}
public static void faceEntity(Entity entity, Entity at) {
if (at == null || entity == null || entity.getWorld() != at.getWorld())
return;
if (at instanceof LivingEntity) {
NMS.look(entity, at);
} else {
faceLocation(entity, at.getLocation(AT_LOCATION));
}
}
public static void faceLocation(Entity entity, Location to) {
faceLocation(entity, to, false);
}
public static void faceLocation(Entity entity, Location to, boolean headOnly) {
faceLocation(entity, to, headOnly, true);
}
public static void faceLocation(Entity entity, Location to, boolean headOnly, boolean immediate) {
if (to == null || entity.getWorld() != to.getWorld())
return;
NMS.look(entity, to, headOnly, immediate);
}
public static Scoreboard getDummyScoreboard() {
return DUMMY_SCOREBOARD;
}
public static Location getEyeLocation(Entity entity) {
return entity instanceof LivingEntity ? ((LivingEntity) entity).getEyeLocation() : entity.getLocation();
}
public static Material getFallbackMaterial(String first, String second) {
try {
return Material.valueOf(first);
} catch (IllegalArgumentException e) {
return Material.valueOf(second);
}
}
public static Random getFastRandom() {
return new XORShiftRNG();
}
public static String getMinecraftRevision() {
if (MINECRAFT_REVISION == null) {
MINECRAFT_REVISION = Bukkit.getServer().getClass().getPackage().getName();
}
return MINECRAFT_REVISION.substring(MINECRAFT_REVISION.lastIndexOf('.') + 2);
}
public static String getTeamName(UUID id) {
return "CIT-" + id.toString().replace("-", "").substring(0, 12);
}
public static boolean inBlock(Entity entity) {
// TODO: bounding box aware?
Location loc = entity.getLocation(AT_LOCATION);
if (!Util.isLoaded(loc)) {
return false;
}
Block in = loc.getBlock();
Block above = in.getRelative(BlockFace.UP);
return in.getType().isSolid() && above.getType().isSolid() && NMS.isSolid(in) && NMS.isSolid(above);
}
public static boolean isAlwaysFlyable(EntityType type) {
if (type.name().toLowerCase().equals("vex") || type.name().toLowerCase().equals("parrot")
|| type.name().toLowerCase().equals("bee") || type.name().toLowerCase().equals("phantom"))
// 1.8.8 compatibility
return true;
switch (type) {
case BAT:
case BLAZE:
case ENDER_DRAGON:
case GHAST:
case WITHER:
return true;
default:
return false;
}
}
public static boolean isHorse(EntityType type) {
String name = type.name();
return type == EntityType.HORSE || name.contains("_HORSE") || name.equals("DONKEY") || name.equals("MULE")
|| name.equals("LLAMA") || name.equals("TRADER_LLAMA");
}
public static boolean isLoaded(Location location) {
if (location.getWorld() == null)
return false;
int chunkX = location.getBlockX() >> 4;
int chunkZ = location.getBlockZ() >> 4;
return location.getWorld().isChunkLoaded(chunkX, chunkZ);
}
public static boolean isOffHand(PlayerInteractEntityEvent event) {
try {
return event.getHand() == org.bukkit.inventory.EquipmentSlot.OFF_HAND;
} catch (NoSuchMethodError e) {
return false;
} catch (NoSuchFieldError e) {
return false;
}
}
public static boolean isOffHand(PlayerInteractEvent event) {
try {
return event.getHand() == org.bukkit.inventory.EquipmentSlot.OFF_HAND;
} catch (NoSuchMethodError e) {
return false;
} catch (NoSuchFieldError e) {
return false;
}
}
public static String listValuesPretty(Enum<?>[] values) {
return "<e>" + Joiner.on("<a>, <e>").join(values).toLowerCase();
}
public static boolean locationWithinRange(Location current, Location target, double range) {
if (current == null || target == null)
return false;
if (current.getWorld() != target.getWorld())
return false;
return current.distanceSquared(target) <= Math.pow(range, 2);
}
public static EntityType matchEntityType(String toMatch) {
return matchEnum(EntityType.values(), toMatch);
}
public static <T extends Enum<?>> T matchEnum(T[] values, String toMatch) {
toMatch = toMatch.toLowerCase().replace('-', '_').replace(' ', '_');
for (T check : values) {
if (toMatch.equals(check.name().toLowerCase())
|| (toMatch.equals("item") && check == EntityType.DROPPED_ITEM)) {
return check; // check for an exact match first
}
}
for (T check : values) {
String name = check.name().toLowerCase();
if (name.replace("_", "").equals(toMatch) || name.startsWith(toMatch)) {
return check;
}
}
return null;
}
public static boolean matchesItemInHand(Player player, String setting) {
String parts = setting;
if (parts.contains("*") || parts.isEmpty())
return true;
for (String part : Splitter.on(',').split(parts)) {
Material matchMaterial = SpigotUtil.isUsing1_13API() ? Material.matchMaterial(part, false)
: Material.matchMaterial(part);
if (matchMaterial == null) {
if (part.equals("280")) {
matchMaterial = Material.STICK;
} else if (part.equals("340")) {
matchMaterial = Material.BOOK;
}
}
if (matchMaterial == player.getInventory().getItemInHand().getType()) {
return true;
}
}
return false;
}
public static Set<EntityType> optionalEntitySet(String... types) {
Set<EntityType> list = EnumSet.noneOf(EntityType.class);
for (String type : types) {
try {
list.add(EntityType.valueOf(type));
} catch (IllegalArgumentException e) {
}
}
return list;
}
public static String prettyEnum(Enum<?> e) {
return e.name().toLowerCase().replace('_', ' ');
}
public static String prettyPrintLocation(Location to) {
return String.format("%s at %d, %d, %d (%d, %d)", to.getWorld().getName(), to.getBlockX(), to.getBlockY(),
to.getBlockZ(), (int) to.getYaw(), (int) to.getPitch());
}
/**
* @param mode
* 0 for create, 1 for remove, 2 for update
*/
public static void sendTeamPacketToOnlinePlayers(Team team, int mode) {
for (Player player : Bukkit.getOnlinePlayers()) {
NMS.sendTeamPacket(player, team, mode);
}
}
public static String[] splitPlayerName(String coloredName) {
String name = coloredName, prefix = null, suffix = null;
if (coloredName.length() > 16) {
if (coloredName.length() >= 30) {
prefix = coloredName.substring(0, 16);
int len = 30;
name = coloredName.substring(16, 30);
String prefixColors = ChatColor.getLastColors(prefix);
if (prefixColors.isEmpty()) {
if (NON_ALPHABET_MATCHER.matcher(name).matches()) {
if (coloredName.length() >= 32) {
len = 32;
name = coloredName.substring(16, 32);
} else if (coloredName.length() == 31) {
len = 31;
name = coloredName.substring(16, 31);
}
} else {
prefixColors = ChatColor.RESET.toString();
}
} else if (prefixColors.length() > 2) {
prefixColors = prefixColors.substring(prefixColors.length() - 2);
}
name = prefixColors + name;
suffix = coloredName.substring(len);
} else {
prefix = coloredName.substring(0, coloredName.length() - 16);
name = coloredName.substring(prefix.length());
if (prefix.endsWith(String.valueOf(ChatColor.COLOR_CHAR))) {
prefix = prefix.substring(0, prefix.length() - 1);
name = ChatColor.COLOR_CHAR + name;
}
String prefixColors = ChatColor.getLastColors(prefix);
if (prefixColors.isEmpty() && !NON_ALPHABET_MATCHER.matcher(name).matches()) {
prefixColors = ChatColor.RESET.toString();
} else if (prefixColors.length() > 2) {
prefixColors = prefixColors.substring(prefixColors.length() - 2);
}
name = prefixColors + name;
if (name.length() > 16) {
suffix = name.substring(16);
name = name.substring(0, 16);
}
}
}
return new String[] { name, prefix, suffix };
}
public static void updateNPCTeams(Player toUpdate, int mode) {
for (Player player : PlayerUpdateTask.getRegisteredPlayerNPCs()) {
NPC npc = ((NPCHolder) player).getNPC();
String teamName = npc.data().get(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, "");
Team team = null;
if (teamName.length() == 0 || (team = Util.getDummyScoreboard().getTeam(teamName)) == null)
continue;
NMS.sendTeamPacket(toUpdate, team, mode);
}
}
private static final Location AT_LOCATION = new Location(null, 0, 0, 0);
private static final Scoreboard DUMMY_SCOREBOARD = Bukkit.getScoreboardManager().getNewScoreboard();
private static String MINECRAFT_REVISION;
private static final Pattern NON_ALPHABET_MATCHER = Pattern.compile(".*[^A-Za-z0-9_].*");
}