-
Notifications
You must be signed in to change notification settings - Fork 1
/
DebugMarker.java
181 lines (170 loc) · 7.54 KB
/
DebugMarker.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
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.MinecraftKey;
import io.netty.buffer.Unpooled;
import net.minecraft.core.BlockPosition;
import net.minecraft.network.PacketDataSerializer;
import org.bukkit.entity.Player;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class DebugMarker {
private static final PacketContainer STOP_ALL_MARKERS = new PacketContainer(PacketType.Play.Server.CUSTOM_PAYLOAD);
private PacketDataSerializer data;
private final PacketContainer marker;
private org.bukkit.Location location;
private Color color;
private String name;
private int duration;
private int distanceSquared;
private final List<Player> seen;
private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
public DebugMarker(org.bukkit.Location location, Color color, String name, int duration, List<Player> showTo) throws InvocationTargetException {
this(location, color, name, duration);
for (Player player : showTo) {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, marker);
}
}
public DebugMarker(org.bukkit.Location location, Color color, String name, int duration) {
this.location = location;
this.color = color;
this.name = name;
this.duration = duration;
seen = new ArrayList<>();
marker = new PacketContainer(PacketType.Play.Server.CUSTOM_PAYLOAD);
data = new PacketDataSerializer(Unpooled.buffer());
data.a(new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ())); // location
data.writeInt(color.getRGB()); // color
data.a(name); // name
data.writeInt(duration); // lifetime of marker
marker.getMinecraftKeys().write(0, new MinecraftKey("debug/game_test_add_marker"));
marker.getSpecificModifier(PacketDataSerializer.class).write(0, data);
}
public void start(int distance) {
start(distance, () -> {
}); // do-nothing runnable
}
public void start(int distance, Runnable callback) {
if (!executorService.isShutdown()) {
stop();
}
distanceSquared = distance < 0 ? -1 : distance * distance;
long endTime = System.currentTimeMillis() + duration;
executorService = Executors.newScheduledThreadPool(1);
// probably not the most efficient way of doing this
Runnable run = () -> {
if (System.currentTimeMillis() > endTime) {
seen.clear();
callback.run();
executorService.shutdown();
return;
}
for (Player p : location.getWorld().getPlayers()) {
if (isCloseEnough(p.getLocation()) && !seen.contains(p)) {
setData(location, color, name, (int) (endTime - System.currentTimeMillis())); // make sure death time is the same for all players
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(p, marker);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
seen.add(p);
} else if (!isCloseEnough(p.getLocation()) && seen.contains(p)) {
setData(location, new Color(0, 0, 0, 0), "", 0);
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(p, marker);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
seen.remove(p);
}
}
};
executorService.scheduleAtFixedRate(run, 0, 200, TimeUnit.MILLISECONDS);
}
public void stop() {
setData(location, new Color(0, 0, 0, 0), "", 0);
for (Player p : location.getWorld().getPlayers()) {
if (distanceSquared == -1 || this.location.distanceSquared(p.getLocation()) <= distanceSquared) {
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(p, marker);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
seen.clear();
executorService.shutdownNow();
}
public void stopAll(int distance) throws InvocationTargetException {
int distanceSquared = distance < 0 ? -1 : distance * distance;
// probably not the most efficient way of doing this
for (Player p : location.getWorld().getPlayers()) {
if (distanceSquared == -1 || this.location.distanceSquared(p.getLocation()) <= distanceSquared) {
ProtocolLibrary.getProtocolManager().sendServerPacket(p, STOP_ALL_MARKERS);
}
}
}
public static void stopAll(List<Player> stopTo) throws InvocationTargetException {
for (Player player : stopTo) {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, STOP_ALL_MARKERS);
}
}
public static void stopAll(org.bukkit.Location location, int distance) throws InvocationTargetException {
int distanceSquared = distance < 0 ? -1 : distance * distance;
// probably not the most efficient way of doing this
for (Player p : location.getWorld().getPlayers()) {
if (distanceSquared == -1 || location.distanceSquared(p.getLocation()) <= distanceSquared) {
ProtocolLibrary.getProtocolManager().sendServerPacket(p, STOP_ALL_MARKERS);
}
}
}
private void setData(org.bukkit.Location location, Color color, String name, int duration) {
data = new PacketDataSerializer(Unpooled.buffer());
data.a(new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
data.writeInt(color.getRGB());
data.a(name);
data.writeInt(duration);
marker.getSpecificModifier(PacketDataSerializer.class).write(0, data);
}
public void setLocation(org.bukkit.Location location) {
this.location = location;
setData(this.location, this.color, this.name, this.duration);
}
public void setColor(Color color) {
this.color = color;
setData(this.location, this.color, this.name, this.duration);
}
public void setName(String name) {
this.name = name;
setData(this.location, this.color, this.name, this.duration);
}
public void setDuration(int duration) {
this.duration = duration;
setData(this.location, this.color, this.name, this.duration);
}
public org.bukkit.Location getLocation() {
return location;
}
public Color getColor() {
return color;
}
public String getName() {
return name;
}
public int getDuration() {
return duration;
}
private boolean isCloseEnough(org.bukkit.Location location) {
return distanceSquared == -1 ||
this.location.distanceSquared(location) <= distanceSquared;
}
static {
STOP_ALL_MARKERS.getMinecraftKeys().write(0, new MinecraftKey("debug/game_test_clear"));
STOP_ALL_MARKERS.getSpecificModifier(PacketDataSerializer.class).write(0, new PacketDataSerializer(Unpooled.buffer()));
}
}