-
-
Notifications
You must be signed in to change notification settings - Fork 315
/
Util.java
142 lines (119 loc) · 4.88 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
package net.citizensnpcs.util;
import java.util.Random;
import net.citizensnpcs.api.event.NPCCollisionEvent;
import net.citizensnpcs.api.event.NPCPushEvent;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
public class Util {
// Static class for small (emphasis small) utility methods
private Util() {
}
public static void assumePose(LivingEntity 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 NPCPushEvent callPushEvent(NPC npc, Vector vector) {
NPCPushEvent event = new NPCPushEvent(npc, vector);
event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
Bukkit.getPluginManager().callEvent(event);
return event;
}
public static void faceEntity(LivingEntity from, LivingEntity at) {
if (from.getWorld() != at.getWorld())
return;
faceLocation(from, at.getLocation(AT_LOCATION));
}
public static void faceLocation(LivingEntity from, Location to) {
if (from.getWorld() != to.getWorld())
return;
Location fromLocation = from.getLocation(FROM_LOCATION);
double xDiff, yDiff, zDiff;
xDiff = to.getX() - fromLocation.getX();
yDiff = to.getY() - fromLocation.getY();
zDiff = to.getZ() - fromLocation.getZ();
double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);
double yaw = Math.toDegrees(Math.acos(xDiff / distanceXZ));
double pitch = Math.toDegrees(Math.acos(yDiff / distanceY)) - 90;
if (zDiff < 0.0)
yaw += Math.abs(180 - yaw) * 2;
NMS.look(from, (float) yaw - 90, (float) pitch);
}
public static Random getFastRandom() {
return new XORShiftRNG();
}
public static String getMinecraftVersion() {
String raw = Bukkit.getVersion();
int start = raw.indexOf("MC:");
if (start == -1)
return raw;
start += 4;
int end = raw.indexOf(')', start);
return raw.substring(start, end);
}
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 String listValuesPretty(Enum<?>[] values) {
return Joiner.on(", ").join(values).toLowerCase().replace('_', ' ');
}
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) {
EntityType type = EntityType.fromName(toMatch);
if (type != null)
return type;
return matchEnum(EntityType.values(), toMatch);
}
public static <T extends Enum<?>> T matchEnum(T[] values, String toMatch) {
T type = null;
toMatch = toMatch.toLowerCase();
for (T check : values) {
String name = check.name().toLowerCase();
if (name.matches(toMatch) || name.equals(toMatch) || name.replace("_", "").equals(toMatch)
|| name.replace('_', ' ').equals(toMatch) || name.replace('_', '-').equals(toMatch)
|| name.replace('_', ' ').equals(toMatch) || name.startsWith(toMatch)) {
type = check;
break;
}
}
return type;
}
public static boolean matchesItemInHand(Player player, String setting) {
String parts = setting;
if (parts.contains("*"))
return true;
for (String part : Splitter.on(',').split(parts)) {
if (Material.matchMaterial(part) == player.getItemInHand().getType()) {
return true;
}
}
return false;
}
public static String prettyEnum(Enum<?> e) {
return e.name().toLowerCase().replace('_', ' ');
}
private static final Location AT_LOCATION = new Location(null, 0, 0, 0);
private static final Location FROM_LOCATION = new Location(null, 0, 0, 0);
}