-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
GlowingBlocks.java
278 lines (232 loc) · 8.22 KB
/
GlowingBlocks.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package fr.skytasul.glowingentities;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import fr.skytasul.glowingentities.GlowingEntities.Packets;
import io.papermc.paper.event.packet.PlayerChunkLoadEvent;
/**
* An extension of {@link GlowingEntities} to make blocks glow as well!
* <p>
* <i>Can only be used on Paper-based servers.</i>
*
* @author SkytAsul
*/
public class GlowingBlocks implements Listener {
private final @NotNull GlowingEntities entities;
private Map<Player, PlayerData> glowing;
private boolean enabled = false;
/**
* Initializes the Glowing blocks API.
*
* @param plugin plugin that will be used to register the events.
*/
public GlowingBlocks(@NotNull Plugin plugin) {
testForPaper();
this.entities = new GlowingEntities(plugin);
enable();
}
/**
* Enables the Glowing blocks API.
*
* @see #disable()
*/
public void enable() {
if (enabled)
throw new IllegalStateException("The Glowing Blocks API has already been enabled.");
entities.plugin.getServer().getPluginManager().registerEvents(this, entities.plugin);
if (!entities.enabled)
entities.enable();
glowing = new HashMap<>();
enabled = true;
}
/**
* Disables the API.
* <p>
* Methods such as {@link #setGlowing(Location, Player, ChatColor)} and
* {@link #unsetGlowing(Location, Player)} will no longer be usable.
*
* @see #enable()
*/
public void disable() {
if (!enabled)
return;
HandlerList.unregisterAll(this);
glowing.values().forEach(playerData -> {
playerData.datas.values().forEach(glowingData -> {
try {
glowingData.remove();
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
});
});
entities.disable();
glowing = null;
enabled = false;
}
private void ensureEnabled() {
if (!enabled)
throw new IllegalStateException("The Glowing Blocks API is not enabled.");
}
private void testForPaper() {
try {
Class.forName("io.papermc.paper.event.packet.PlayerChunkLoadEvent"); // NOSONAR
} catch (ClassNotFoundException ex) {
throw new UnsupportedOperationException("The GlowingBlocks util can only be used on a Paper server.");
}
}
/**
* Makes the {@link Block} passed as a parameter glow with the specified color.
*
* @param block block to make glow
* @param receiver player which will see the block glowing
* @param color color of the glowing effect
* @throws ReflectiveOperationException
*/
public void setGlowing(@NotNull Block block, @NotNull Player receiver, @NotNull ChatColor color)
throws ReflectiveOperationException {
setGlowing(block.getLocation(), receiver, color);
}
/**
* Makes the block at the location passed as a parameter glow with the specified color.
*
* @param block location of the block to make glow
* @param receiver player which will see the block glowing
* @param color color of the glowing effect
* @throws ReflectiveOperationException
*/
public void setGlowing(@NotNull Location block, @NotNull Player receiver, @NotNull ChatColor color)
throws ReflectiveOperationException {
ensureEnabled();
block = normalizeLocation(block);
if (!color.isColor())
throw new IllegalArgumentException("ChatColor must be a color format");
PlayerData playerData = glowing.computeIfAbsent(Objects.requireNonNull(receiver), PlayerData::new);
GlowingBlockData blockData = playerData.datas.get(block);
if (blockData == null) {
blockData = new GlowingBlockData(receiver, block, color);
playerData.datas.put(block, blockData);
if (canSee(receiver, block))
blockData.spawn();
} else {
blockData.setColor(color);
}
}
/**
* Makes the {@link Block} passed as a parameter loose its glowing effect.
*
* @param block block to remove glowing effect from
* @param receiver player which will no longer see the glowing effect
* @throws ReflectiveOperationException
*/
public void unsetGlowing(@NotNull Block block, @NotNull Player receiver) throws ReflectiveOperationException {
unsetGlowing(block.getLocation(), receiver);
}
/**
* Makes the block at the location passed as a parameter loose its glowing effect.
*
* @param block location of the block to remove glowing effect from
* @param receiver player which will no longer see the glowing effect
* @throws ReflectiveOperationException
*/
public void unsetGlowing(@NotNull Location block, @NotNull Player receiver) throws ReflectiveOperationException {
ensureEnabled();
block = normalizeLocation(block);
PlayerData playerData = glowing.get(receiver);
if (playerData == null)
return;
GlowingBlockData blockData = playerData.datas.remove(block);
if (blockData == null)
return; // the player did not have this block glowing
blockData.remove();
if (playerData.datas.isEmpty())
glowing.remove(receiver);
}
private @NotNull Location normalizeLocation(@NotNull Location location) {
// we normalize here all locations to be on the corner of the block
location.checkFinite();
return new Location(location.getWorld(), location.getBlockX(), location.getBlockY(), location.getBlockZ());
}
private boolean canSee(Player player, Location location) {
// little Pythagorean theorem with 1 chunk as the unit distance
int viewDistance = Math.min(player.getViewDistance(), Bukkit.getViewDistance());
int deltaChunkX = (player.getLocation().getBlockX() >> 4) - (location.getBlockX() >> 4);
int deltaChunkZ = (player.getLocation().getBlockZ() >> 4) - (location.getBlockZ() >> 4);
int chunkDistanceSquared = deltaChunkX * deltaChunkX + deltaChunkZ * deltaChunkZ;
return chunkDistanceSquared <= viewDistance * viewDistance;
}
@EventHandler
public void onPlayerChunkLoad(PlayerChunkLoadEvent event) {
PlayerData playerData = glowing.get(event.getPlayer());
if (playerData == null)
return;
playerData.datas.forEach((location, blockData) -> {
if (Objects.equals(location.getWorld(), event.getWorld())
&& location.getBlockX() >> 4 == event.getChunk().getX()
&& location.getBlockZ() >> 4 == event.getChunk().getZ()) {
try {
blockData.spawn();
} catch (ReflectiveOperationException ex) {
ex.printStackTrace();
}
}
});
}
private record PlayerData(@NotNull Player player, @NotNull Map<Location, GlowingBlockData> datas) {
public PlayerData(@NotNull Player player) {
this(player, new HashMap<>());
}
}
private class GlowingBlockData {
private static final byte FLAGS = 1 << 5;
private static final AtomicInteger ENTITY_ID_COUNTER =
new AtomicInteger(ThreadLocalRandom.current().nextInt(1_000_000, 2_000_000_000));
private final @NotNull Player player;
private final @NotNull Location location;
private @NotNull ChatColor color;
private int entityId;
private UUID entityUuid;
public GlowingBlockData(@NotNull Player player, @NotNull Location location, @NotNull ChatColor color) {
this.player = player;
this.location = location;
this.color = color;
}
public void setColor(@NotNull ChatColor color) throws ReflectiveOperationException {
this.color = color;
if (entityUuid != null)
entities.setGlowing(entityId, entityUuid.toString(), player, color, FLAGS);
}
public void spawn() throws ReflectiveOperationException {
init();
Packets.createEntity(player, entityId, entityUuid, Packets.shulkerEntityType, location);
Packets.setMetadata(player, entityId, FLAGS, false);
// this will take care of refreshing the color thanks to the packet handler in GlowingEntities
}
public void remove() throws ReflectiveOperationException {
if (entityUuid == null)
return;
Packets.removeEntities(player, entityId);
entities.unsetGlowing(entityId, player);
}
private void init() throws ReflectiveOperationException {
if (entityUuid == null) {
entityId = ENTITY_ID_COUNTER.getAndIncrement();
entityUuid = UUID.randomUUID();
setColor(color);
}
}
}
}