-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathDependencyProtocolLib.java
More file actions
152 lines (138 loc) · 5.29 KB
/
DependencyProtocolLib.java
File metadata and controls
152 lines (138 loc) · 5.29 KB
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
package me.crafter.mc.lockettepro;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
public class DependencyProtocolLib {
public static List<Player> signqueue = new ArrayList<Player>();
public static void setUpProtocolLib(Plugin plugin){
addOpenSignEditorListener(plugin);
switch (LockettePro.getBukkitVersion()){
case v1_8_R1:
case v1_8_R2:
case v1_8_R3:
addUpdateSignListener(plugin);
break;
case v1_9_R1:
addUpdateSignListener(plugin);
addTileEntityDataListener(plugin);
break;
case v1_9_R2:
addTileEntityDataListener(plugin);
addMapChunkListener(plugin);
break;
case v1_10_R1:
addTileEntityDataListener(plugin);
addMapChunkListener(plugin);
break;
case v1_11_R1:
addTileEntityDataListener(plugin);
addMapChunkListener(plugin);
break;
case v1_12_R1:
addTileEntityDataListener(plugin);
addMapChunkListener(plugin);
break;
case UNKNOWN:
default:
addUpdateSignListener(plugin);
addTileEntityDataListener(plugin);
addMapChunkListener(plugin);
break;
}
}
public static void cleanUpProtocolLib(Plugin plugin){
try {
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null){
ProtocolLibrary.getProtocolManager().removePacketListeners(plugin);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void addOpenSignEditorListener(Plugin plugin){
ProtocolLibrary.getProtocolManager().getAsynchronousManager().registerAsyncHandler(new PacketAdapter(plugin, ListenerPriority.LOW, PacketType.Play.Server.OPEN_SIGN_EDITOR) {
@Override
public void onPacketSending(PacketEvent event) {
Player player = event.getPlayer();
signqueue.add(player);
try {Thread.sleep(25);} catch (Exception e) {e.printStackTrace();}
if (signqueue.remove(player) == false) event.setCancelled(true);
}
}).start();
}
public static void addUpdateSignListener(Plugin plugin){
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, ListenerPriority.LOW, PacketType.Play.Server.UPDATE_SIGN) {
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
WrappedChatComponent[] lines = packet.getChatComponentArrays().read(0);
String[] liness = new String[4];
for (int i = 0; i < 4; i++){
liness[i] = lines[i].getJson();
}
SignSendEvent signsendevent = new SignSendEvent(event.getPlayer(), liness);
Bukkit.getPluginManager().callEvent(signsendevent);
if (signsendevent.isModified()){
packet.getChatComponentArrays().write(0, signsendevent.getLinesWrappedChatComponent());
}
}
});
}
public static void addTileEntityDataListener(Plugin plugin){
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, ListenerPriority.LOW, PacketType.Play.Server.TILE_ENTITY_DATA) {
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
if (packet.getIntegers().read(0) != 9) return;
NbtCompound nbtcompound = (NbtCompound) packet.getNbtModifier().read(0);
String[] liness = new String[4];
for (int i = 0; i < 4; i++){
liness[i] = nbtcompound.getString("Text" + (i+1));
}
SignSendEvent signsendevent = new SignSendEvent(event.getPlayer(), liness);
Bukkit.getPluginManager().callEvent(signsendevent);
if (signsendevent.isModified()){
for (int i = 0; i < 4; i++){
nbtcompound.put("Text" + (i+1), signsendevent.getLine(i));
}
}
}
});
}
public static void addMapChunkListener(Plugin plugin){
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, ListenerPriority.LOW, PacketType.Play.Server.MAP_CHUNK) {
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
List<?> tileentitydatas = packet.getSpecificModifier(List.class).read(0);
for (Object tileentitydata : tileentitydatas) {
NbtCompound nbtcompound = NbtFactory.fromNMSCompound(tileentitydata);
if (nbtcompound == null || nbtcompound.getString("id") == null) continue;
if (!(nbtcompound.getString("id").equals("Sign") || nbtcompound.getString("id").equals("minecraft:sign"))) continue;
String[] liness = new String[4];
for (int i = 0; i < 4; i++){
liness[i] = nbtcompound.getString("Text" + (i+1));
}
SignSendEvent signsendevent = new SignSendEvent(event.getPlayer(), liness);
Bukkit.getPluginManager().callEvent(signsendevent);
if (signsendevent.isModified()){
for (int i = 0; i < 4; i++){
nbtcompound.put("Text" + (i+1), signsendevent.getLine(i));
}
}
}
}
});
}
}