-
-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathExample.java
More file actions
157 lines (126 loc) · 4.63 KB
/
Example.java
File metadata and controls
157 lines (126 loc) · 4.63 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
153
154
155
156
157
package dev.protocollib;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import dev.protocollib.api.ProtocolLib;
import dev.protocollib.api.listener.PacketListenerBundleBehavior;
import dev.protocollib.api.listener.PacketListenerPriority;
import dev.protocollib.api.packet.MutablePacketContainer;
import dev.protocollib.api.packet.PacketContainer;
import dev.protocollib.api.packet.PacketTypes;
import dev.protocollib.api.reflect.MutableGenericAccessor;
public class Example {
private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
private static ProtocolLib protocolLib;
private static Plugin plugin;
public static class ParticleData {
public String s;
public int i;
}
public static class ParticlePacket {
public int duration;
public String name;
public ParticleData data;
}
static {
MutablePacketContainer packet = null;
Class<?> clazz = MutableGenericAccessor.class;
packet.accessor().update(a -> {
a.update(ParticleData.class, 0, b -> {
b.set(int.class, 0, -1);
});
});
packet.accessor().update(accessor -> {
accessor.setObject(clazz, 1, "");
accessor.set(Integer.class, 1, 1235);
accessor.set(String.class, 1, "world");
accessor.update(Object.class, 1, a -> {
a.update(Object.class, 1, b -> {
b.set(int.class, 2, -1);
});
});
accessor.update(Object.class, 1, mutableAccessor -> {
mutableAccessor.set(int.class, 1, -1);
});
accessor.update(ParticleData.class, 1, (pd) -> {
pd.i++;
return pd;
});
accessor.update(ParticleData.class, 1, (pd) -> {
pd.set(int.class, 1, pd.get(int.class, 1) + 1);
});
});
}
// ========================
// Packet Listeners
// ========================
static void registerListeners() {
// sync read-only listener
protocolLib
.createListener(plugin)
.types(PacketTypes.Game.LEVEL_CHUNK)
.priority(PacketListenerPriority.LOW)
.includeCanceledPackets()
.bundleBehavior(PacketListenerBundleBehavior.SKIP_OUTSIDE_BUNDLE)
.registerSync((packet, context) -> {
Chunk chunk = Chunk.from(packet);
// Sync processing
});
// async modify packet on netty thread
protocolLib
.createListener(plugin)
.types(PacketTypes.Game.LEVEL_CHUNK)
.mutable()
.registerAsync((packet, context) -> {
Chunk chunk = Chunk.from(packet);
// do processing here ...
// write changes to packet ...
context.addTransmissionListener(chunk::markSent);
context.resumeProcessing();
});
// async modify packet on app thread-pool
protocolLib
.createListener(plugin)
.types(PacketTypes.Game.LEVEL_CHUNK)
.mutable()
.registerAsync((packet, context) -> {
Chunk chunk = Chunk.from(packet);
EXECUTOR.execute(() -> {
// do heavy processing here ...
// write changes to packet ...
context.addTransmissionListener(chunk::markSent);
context.resumeProcessing();
});
});
}
// ========================
// Packet Sending
// ========================
static void sendPackets(Player player, Chunk chunk) {
// full packet operation
protocolLib
.connection(player)
.packetOperation()
.skipProcessing()
.postTransmission(chunk::markSent)
.send(chunk.packet());
// connection shortcut
protocolLib
.connection(player)
.sendPacket(chunk.packet());
// direct API shortcut
protocolLib
.sendPacket(player, chunk.packet());
}
// ========================
// Chunk Implementation
// ========================
static class Chunk {
static Chunk from(PacketContainer packet) {
return new Chunk();
}
PacketContainer packet() { return null; }
void markSent() {}
}
}