-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Handler.java
260 lines (236 loc) · 10.7 KB
/
Handler.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
package com.denizenscript.denizen.nms.v1_16;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.*;
import com.denizenscript.denizen.nms.v1_16.helpers.*;
import com.denizenscript.denizen.nms.v1_16.impl.BiomeNMSImpl;
import com.denizenscript.denizen.nms.v1_16.impl.ProfileEditorImpl;
import com.denizenscript.denizen.nms.v1_16.impl.SidebarImpl;
import com.denizenscript.denizen.nms.v1_16.impl.blocks.BlockLightImpl;
import com.denizenscript.denizen.nms.v1_16.impl.jnbt.CompoundTagImpl;
import com.denizenscript.denizen.nms.v1_16.impl.network.handlers.DenizenPacketListenerImpl;
import com.denizenscript.denizen.nms.util.jnbt.Tag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.packets.DenizenPacketHandler;
import com.google.common.collect.Iterables;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.denizenscript.denizen.nms.util.PlayerProfile;
import com.denizenscript.denizen.nms.util.ReflectionHelper;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.hover.content.Content;
import net.md_5.bungee.api.chat.hover.content.Item;
import net.md_5.bungee.api.chat.hover.content.Text;
import net.md_5.bungee.chat.ComponentSerializer;
import net.minecraft.server.v1_16_R1.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Biome;
import org.bukkit.craftbukkit.v1_16_R1.CraftServer;
import org.bukkit.craftbukkit.v1_16_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftInventory;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftInventoryCustom;
import org.bukkit.craftbukkit.v1_16_R1.util.CraftChatMessage;
import org.bukkit.craftbukkit.v1_16_R1.util.CraftMagicNumbers;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.spigotmc.AsyncCatcher;
import java.lang.reflect.Field;
import java.util.Map;
public class Handler extends NMSHandler {
public Handler() {
advancementHelper = new AdvancementHelperImpl();
animationHelper = new AnimationHelperImpl();
blockHelper = new BlockHelperImpl();
chunkHelper = new ChunkHelperImpl();
customEntityHelper = new CustomEntityHelperImpl();
entityHelper = new EntityHelperImpl();
fishingHelper = new FishingHelperImpl();
itemHelper = new ItemHelperImpl();
soundHelper = new SoundHelperImpl();
packetHelper = new PacketHelperImpl();
particleHelper = new ParticleHelperImpl();
playerHelper = new PlayerHelperImpl();
worldHelper = new WorldHelperImpl();
}
private final ProfileEditor profileEditor = new ProfileEditorImpl();
private boolean wasAsyncCatcherEnabled;
@Override
public void disableAsyncCatcher() {
wasAsyncCatcherEnabled = AsyncCatcher.enabled;
AsyncCatcher.enabled = false;
}
@Override
public void undisableAsyncCatcher() {
AsyncCatcher.enabled = wasAsyncCatcherEnabled;
}
@Override
public boolean isCorrectMappingsCode() {
return ((CraftMagicNumbers) CraftMagicNumbers.INSTANCE).getMappingsVersion().equals("25afc67716a170ea965092c1067ff439");
}
@Override
public double[] getRecentTps() {
return ((CraftServer) Bukkit.getServer()).getServer().recentTps;
}
@Override
public void enablePacketInterception(DenizenPacketHandler packetHandler) {
DenizenPacketListenerImpl.enable(packetHandler);
}
@Override
public CompoundTag createCompoundTag(Map<String, Tag> value) {
return new CompoundTagImpl(value);
}
@Override
public Sidebar createSidebar(Player player) {
return new SidebarImpl(player);
}
@Override
public BlockLight createBlockLight(Location location, int lightLevel, long ticks) {
return BlockLightImpl.createLight(location, lightLevel, ticks);
}
@Override
public PlayerProfile fillPlayerProfile(PlayerProfile playerProfile) {
try {
if (playerProfile != null) {
GameProfile gameProfile = new GameProfile(playerProfile.getUniqueId(), playerProfile.getName());
gameProfile.getProperties().get("textures").clear();
if (playerProfile.getTextureSignature() != null) {
gameProfile.getProperties().put("textures", new Property("textures", playerProfile.getTexture(), playerProfile.getTextureSignature()));
}
else {
gameProfile.getProperties().put("textures", new Property("textures", playerProfile.getTexture()));
}
MinecraftServer minecraftServer = ((CraftServer) Bukkit.getServer()).getServer();
GameProfile gameProfile1 = null;
if (gameProfile.getId() != null) {
gameProfile1 = minecraftServer.getUserCache().getProfile(gameProfile.getId());
}
if (gameProfile1 == null && gameProfile.getName() != null) {
gameProfile1 = minecraftServer.getUserCache().getProfile(gameProfile.getName());
}
if (gameProfile1 == null) {
gameProfile1 = gameProfile;
}
if (playerProfile.hasTexture()) {
gameProfile1.getProperties().get("textures").clear();
if (playerProfile.getTextureSignature() != null) {
gameProfile1.getProperties().put("textures", new Property("textures", playerProfile.getTexture(), playerProfile.getTextureSignature()));
}
else {
gameProfile1.getProperties().put("textures", new Property("textures", playerProfile.getTexture()));
}
}
if (Iterables.getFirst(gameProfile1.getProperties().get("textures"), null) == null) {
gameProfile1 = minecraftServer.getMinecraftSessionService().fillProfileProperties(gameProfile1, true);
}
Property property = Iterables.getFirst(gameProfile1.getProperties().get("textures"), null);
return new PlayerProfile(gameProfile1.getName(), gameProfile1.getId(),
property != null ? property.getValue() : null,
property != null ? property.getSignature() : null);
}
}
catch (Exception e) {
if (Debug.verbose) {
Debug.echoError(e);
}
}
return null;
}
@Override
public int getPort() {
return ((CraftServer) Bukkit.getServer()).getServer().getPort();
}
@Override
public String getTitle(Inventory inventory) {
IInventory nms = ((CraftInventory) inventory).getInventory();
if (nms instanceof INamableTileEntity) {
return CraftChatMessage.fromComponent(((INamableTileEntity) nms).getDisplayName());
}
else if (MINECRAFT_INVENTORY.isInstance(nms)) {
try {
return (String) INVENTORY_TITLE.get(nms);
}
catch (IllegalAccessException e) {
Debug.echoError(e);
}
}
return "Chest";
}
public static final Class MINECRAFT_INVENTORY;
public static final Field INVENTORY_TITLE;
public static final Field ENTITY_BUKKITYENTITY = ReflectionHelper.getFields(Entity.class).get("bukkitEntity");
static {
Class minecraftInv = null;
Field title = null;
try {
for (Class clzz : CraftInventoryCustom.class.getDeclaredClasses()) {
if (CoreUtilities.toLowerCase(clzz.getName()).contains("minecraftinventory")) { // MinecraftInventory.
minecraftInv = clzz;
title = clzz.getDeclaredField("title");
title.setAccessible(true);
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
MINECRAFT_INVENTORY = minecraftInv;
INVENTORY_TITLE = title;
}
@Override
public PlayerProfile getPlayerProfile(Player player) {
GameProfile gameProfile = ((CraftPlayer) player).getProfile();
Property property = Iterables.getFirst(gameProfile.getProperties().get("textures"), null);
return new PlayerProfile(gameProfile.getName(), gameProfile.getId(),
property != null ? property.getValue() : null,
property != null ? property.getSignature() : null);
}
@Override
public ProfileEditor getProfileEditor() {
return profileEditor;
}
@Override
public BiomeNMS getBiomeNMS(Biome biome) {
return new BiomeNMSImpl(biome);
}
@Override
public String stringForHover(HoverEvent hover) {
if (hover.getContents().isEmpty()) {
return "";
}
Content contentObject = hover.getContents().get(0);
if (contentObject instanceof Text) {
Object value = ((Text) contentObject).getValue();
if (value instanceof BaseComponent[]) {
return FormattedTextHelper.stringify((BaseComponent[]) value);
}
else {
return value.toString();
}
}
else if (contentObject instanceof Item) {
Item item = (Item) contentObject;
ItemStack itemStack = new ItemStack(org.bukkit.Material.getMaterial(item.getId()), item.getCount());
// TODO: Apply tag somehow
return new ItemTag(itemStack).identify();
}
else if (contentObject instanceof net.md_5.bungee.api.chat.hover.content.Entity) {
net.md_5.bungee.api.chat.hover.content.Entity entity = (net.md_5.bungee.api.chat.hover.content.Entity) contentObject;
// TODO: Maybe a stabler way of doing this?
return "e@" + entity.getId();
}
else {
throw new UnsupportedOperationException();
}
}
public static IChatMutableComponent componentToNMS(BaseComponent[] spigot) {
String json = ComponentSerializer.toString(spigot);
return IChatBaseComponent.ChatSerializer.b(json);
}
}