-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
NMSHandler.java
178 lines (138 loc) · 6.23 KB
/
NMSHandler.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
package net.aufdemrand.denizen.nms;
import net.aufdemrand.denizen.nms.abstracts.AnimationHelper;
import net.aufdemrand.denizen.nms.abstracts.BiomeNMS;
import net.aufdemrand.denizen.nms.abstracts.BlockLight;
import net.aufdemrand.denizen.nms.abstracts.ParticleHelper;
import net.aufdemrand.denizen.nms.abstracts.ProfileEditor;
import net.aufdemrand.denizen.nms.abstracts.Sidebar;
import net.aufdemrand.denizen.nms.interfaces.*;
import net.aufdemrand.denizen.nms.interfaces.packets.PacketHandler;
import net.aufdemrand.denizen.nms.util.PlayerProfile;
import net.aufdemrand.denizen.nms.util.jnbt.CompoundTag;
import net.aufdemrand.denizen.nms.util.jnbt.Tag;
import org.bukkit.Location;
import org.bukkit.block.Biome;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.UUID;
public abstract class NMSHandler {
private static NMSHandler instance;
private static NMSVersion version;
private static JavaPlugin javaPlugin;
public static boolean initialize(JavaPlugin plugin) {
javaPlugin = plugin;
String packageName = javaPlugin.getServer().getClass().getPackage().getName();
try {
// Check if we support this MC version
version = NMSVersion.valueOf(packageName.substring(packageName.lastIndexOf('.') + 1));
}
catch (Exception e) {
version = NMSVersion.NOT_SUPPORTED;
instance = null;
return false;
}
try {
// Get the class of our handler for this version
final Class<?> clazz = Class.forName("net.aufdemrand.denizen.nms.Handler_" + version.name());
if (NMSHandler.class.isAssignableFrom(clazz)) {
// Found and loaded - good to go!
instance = (NMSHandler) clazz.newInstance();
return true;
}
}
catch (Exception e) {
e.printStackTrace();
}
// Someone made an oopsie and didn't implement this version properly :(
version = NMSVersion.NOT_SUPPORTED;
instance = null;
return false;
}
public static NMSHandler getInstance() {
return instance;
}
public static NMSVersion getVersion() {
return version;
}
public static JavaPlugin getJavaPlugin() {
return javaPlugin;
}
public boolean isCorrectMappingsCode() {
return true;
}
public abstract void disableAsyncCatcher();
public abstract void undisableAsyncCatcher();
public abstract Sidebar createSidebar(Player player);
public abstract BlockLight createBlockLight(Location location, int lightLevel, long ticks);
public abstract PlayerProfile fillPlayerProfile(PlayerProfile playerProfile);
public abstract PlayerProfile getPlayerProfile(Player player);
public abstract ProfileEditor getProfileEditor();
public abstract BiomeNMS getBiomeNMS(Biome biome);
public abstract Thread getMainThread();
public abstract double[] getRecentTps();
public abstract AdvancementHelper getAdvancementHelper();
public abstract AnimationHelper getAnimationHelper();
public abstract BlockHelper getBlockHelper();
public abstract ChunkHelper getChunkHelper();
public abstract CustomEntityHelper getCustomEntityHelper();
public abstract EntityHelper getEntityHelper();
public abstract FishingHelper getFishingHelper();
public abstract ItemHelper getItemHelper();
public abstract SoundHelper getSoundHelper();
public abstract PacketHelper getPacketHelper();
public abstract ParticleHelper getParticleHelper();
public abstract PlayerHelper getPlayerHelper();
public abstract WorldHelper getWorldHelper();
public abstract void enablePacketInterception(PacketHandler packetHandler);
public abstract CompoundTag createCompoundTag(Map<String, Tag> value);
public abstract int getPort();
public abstract String getTitle(Inventory inventory);
public static Vector fixOffset(Vector offset, double yaw, double pitch) {
yaw = Math.toRadians(yaw);
pitch = Math.toRadians(pitch);
Vector offsetPatched = offset.clone();
// x rotation
double cosPitch = Math.cos(pitch);
double sinPitch = Math.sin(pitch);
double y1 = (offsetPatched.getY() * cosPitch) - (offsetPatched.getZ() * sinPitch);
double z1 = (offsetPatched.getY() * sinPitch) + (offsetPatched.getZ() * cosPitch);
offsetPatched.setY(y1);
offsetPatched.setZ(z1);
// y rotation
double cosYaw = Math.cos(yaw);
double sinYaw = Math.sin(yaw);
double x2 = (offsetPatched.getX() * cosYaw) + (offsetPatched.getZ() * sinYaw);
double z2 = (offsetPatched.getX() * -sinYaw) + (offsetPatched.getZ() * cosYaw);
offsetPatched.setX(x2);
offsetPatched.setZ(z2);
return offsetPatched;
}
public HashMap<UUID, UUID> attachmentsA = new HashMap<>(); // Key follows value
public HashMap<UUID, UUID> attachments2 = new HashMap<>(); // Value follows key
public HashMap<UUID, Vector> attachmentOffsets = new HashMap<>();
public HashSet<UUID> attachmentRotations = new HashSet<>();
public HashMap<UUID, Vector> visiblePositions = new HashMap<>();
public void forceAttachMove(Entity a, Entity b, Vector offset, boolean matchRotation) {
if (attachmentsA.containsKey(a.getUniqueId())) {
attachments2.remove(attachmentsA.get(a.getUniqueId()));
attachmentsA.remove(a.getUniqueId());
attachmentOffsets.remove(a.getUniqueId());
attachmentRotations.remove(a.getUniqueId());
}
if (b == null) {
return;
}
attachmentsA.put(a.getUniqueId(), b.getUniqueId());
attachments2.put(b.getUniqueId(), a.getUniqueId());
attachmentOffsets.put(a.getUniqueId(), offset);
if (matchRotation) {
attachmentRotations.add(a.getUniqueId());
}
}
}