/
BlockLightImpl.java
263 lines (242 loc) · 11.7 KB
/
BlockLightImpl.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
package com.denizenscript.denizen.nms.v1_19.impl.blocks;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.BlockLight;
import com.denizenscript.denizen.nms.v1_19.ReflectionMappingsInfo;
import com.denizenscript.denizen.utilities.blocks.ChunkCoordinate;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.minecraft.core.BlockPos;
import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundLightUpdatePacket;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ThreadedLevelLightEngine;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.chunk.DataLayer;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.lighting.LayerLightEventListener;
import net.minecraft.world.level.lighting.LevelLightEngine;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_19_R3.CraftChunk;
import org.bukkit.craftbukkit.v1_19_R3.block.CraftBlock;
import org.bukkit.util.Vector;
import java.lang.invoke.MethodHandle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
public class BlockLightImpl extends BlockLight {
public static final Class LIGHTENGINETHREADED_TASKTYPE = Arrays.stream(ThreadedLevelLightEngine.class.getDeclaredClasses()).filter(c -> c.isEnum()).findFirst().get(); // TaskType
public static final Object LIGHTENGINETHREADED_TASKTYPE_PRE;
static {
Object preObj = null;
try {
preObj = ReflectionHelper.getFields(LIGHTENGINETHREADED_TASKTYPE).get(ReflectionMappingsInfo.ThreadedLevelLightEngineTaskType_PRE_UPDATE).get(null);
}
catch (Throwable ex) {
ex.printStackTrace();
}
LIGHTENGINETHREADED_TASKTYPE_PRE = preObj;
}
public static final MethodHandle LIGHTENGINETHREADED_QUEUERUNNABLE = ReflectionHelper.getMethodHandle(ThreadedLevelLightEngine.class, ReflectionMappingsInfo.ThreadedLevelLightEngine_addTask_method,
int.class, int.class, LIGHTENGINETHREADED_TASKTYPE, Runnable.class);
public static void enqueueRunnable(LevelChunk chunk, Runnable runnable) {
LevelLightEngine lightEngine = chunk.getLevel().getChunkSource().getLightEngine();
if (lightEngine instanceof ThreadedLevelLightEngine) {
ChunkPos coord = chunk.getPos();
try {
LIGHTENGINETHREADED_QUEUERUNNABLE.invoke(lightEngine, coord.x, coord.z, LIGHTENGINETHREADED_TASKTYPE_PRE, runnable);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
else {
runnable.run();
}
}
private BlockLightImpl(Location location, long ticks) {
super(location, ticks);
}
public static BlockLight createLight(Location location, int lightLevel, long ticks) {
location = location.getBlock().getLocation();
BlockLight blockLight;
if (lightsByLocation.containsKey(location)) {
blockLight = lightsByLocation.get(location);
if (blockLight.removeTask != null) {
blockLight.removeTask.cancel();
blockLight.removeTask = null;
}
if (blockLight.updateTask != null) {
blockLight.updateTask.cancel();
blockLight.updateTask = null;
}
blockLight.removeLater(ticks);
}
else {
blockLight = new BlockLightImpl(location, ticks);
lightsByLocation.put(location, blockLight);
if (!lightsByChunk.containsKey(blockLight.chunkCoord)) {
lightsByChunk.put(blockLight.chunkCoord, new ArrayList<>());
}
lightsByChunk.get(blockLight.chunkCoord).add(blockLight);
}
blockLight.intendedLevel = lightLevel;
blockLight.update(lightLevel, true);
return blockLight;
}
public static void checkIfLightsBrokenByPacket(ClientboundBlockUpdatePacket packet, Level world) {
try {
BlockPos pos = packet.getPos();
int chunkX = pos.getX() >> 4;
int chunkZ = pos.getZ() >> 4;
Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> {
LevelChunk chunk = world.getChunk(chunkX, chunkZ);
boolean any = false;
for (Vector vec : RELATIVE_CHUNKS) {
ChunkAccess other = world.getChunk(chunkX + vec.getBlockX(), chunkZ + vec.getBlockZ(), ChunkStatus.FULL, false);
if (other instanceof LevelChunk) {
List<BlockLight> lights = lightsByChunk.get(new ChunkCoordinate(((LevelChunk) other).bukkitChunk));
if (lights != null) {
any = true;
for (BlockLight light : lights) {
Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> light.update(light.intendedLevel, false), 1);
}
}
}
}
if (any) {
Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> sendNearbyChunkUpdates(chunk), 3);
}
}, 1);
}
catch (Exception ex) {
Debug.echoError(ex);
}
}
public static void checkIfLightsBrokenByPacket(ClientboundLightUpdatePacket packet, Level world) {
if (doNotCheck) {
return;
}
try {
int cX = packet.getX();
int cZ = packet.getZ();
BitSet bitMask = packet.getLightData().getBlockYMask();
List<byte[]> blockData = packet.getLightData().getBlockUpdates();
Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> {
ChunkAccess chk = world.getChunk(cX, cZ, ChunkStatus.FULL, false);
if (!(chk instanceof LevelChunk)) {
return;
}
List<BlockLight> lights = lightsByChunk.get(new ChunkCoordinate(((LevelChunk) chk).bukkitChunk));
if (lights == null) {
return;
}
boolean any = false;
for (BlockLight light : lights) {
if (((BlockLightImpl) light).checkIfChangedBy(bitMask, blockData)) {
Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> light.update(light.intendedLevel, false), 1);
any = true;
}
}
if (any) {
Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> sendNearbyChunkUpdates((LevelChunk) chk), 3);
}
}, 1);
}
catch (Exception ex) {
Debug.echoError(ex);
}
}
public static boolean doNotCheck = false;
public boolean checkIfChangedBy(BitSet bitmask, List<byte[]> data) {
Location blockLoc = block.getLocation();
int layer = (blockLoc.getBlockY() >> 4) + 1;
if (!bitmask.get(layer)) {
return false;
}
int found = 0;
for (int i = 0; i < 16; i++) {
if (bitmask.get(i)) {
if (i == layer) {
byte[] blocks = data.get(found);
DataLayer arr = new DataLayer(blocks);
int x = blockLoc.getBlockX() - (chunkCoord.x << 4);
int y = blockLoc.getBlockY() % 16;
int z = blockLoc.getBlockZ() - (chunkCoord.z << 4);
int level = arr.get(x, y, z);
return intendedLevel != level;
}
found++;
}
}
return false;
}
public static void runResetFor(final LevelChunk chunk, final BlockPos pos) {
Runnable runnable = () -> {
LevelLightEngine lightEngine = chunk.getLevel().getChunkSource().getLightEngine();
LayerLightEventListener engineBlock = lightEngine.getLayerListener(LightLayer.BLOCK);
engineBlock.checkBlock(pos);
};
enqueueRunnable(chunk, runnable);
}
public static void runSetFor(final LevelChunk chunk, final BlockPos pos, final int level) {
Runnable runnable = () -> {
LevelLightEngine lightEngine = chunk.getLevel().getChunkSource().getLightEngine();
LayerLightEventListener engineBlock = lightEngine.getLayerListener(LightLayer.BLOCK);
engineBlock.onBlockEmissionIncrease(pos, level);
};
enqueueRunnable(chunk, runnable);
}
@Override
public void reset(boolean updateChunk) {
runResetFor(((CraftChunk) getChunk()).getHandle(), ((CraftBlock) block).getPosition());
if (updateChunk) {
// This runnable cast is necessary despite what your IDE may claim
updateTask = Bukkit.getScheduler().runTaskLater(NMSHandler.getJavaPlugin(), (Runnable) this::sendNearbyChunkUpdates, 1);
}
}
@Override
public void update(int lightLevel, boolean updateChunk) {
runResetFor(((CraftChunk) getChunk()).getHandle(), ((CraftBlock) block).getPosition());
updateTask = Bukkit.getScheduler().runTaskLater(NMSHandler.getJavaPlugin(), () -> {
updateTask = null;
runSetFor(((CraftChunk) chunk).getHandle(), ((CraftBlock) block).getPosition(), lightLevel);
if (updateChunk) {
// This runnable cast is necessary despite what your IDE may claim
updateTask = Bukkit.getScheduler().runTaskLater(NMSHandler.getJavaPlugin(), (Runnable) this::sendNearbyChunkUpdates, 1);
}
}, 1);
}
public static final Vector[] RELATIVE_CHUNKS = new Vector[] {
new Vector(0, 0, 0),
new Vector(-1, 0, 0), new Vector(1, 0, 0), new Vector(0, 0, -1), new Vector(0, 0, 1),
new Vector(-1, 0, -1), new Vector(-1, 0, 1), new Vector(1, 0, -1), new Vector(1, 0, 1)
};
public void sendNearbyChunkUpdates() {
sendNearbyChunkUpdates(((CraftChunk) getChunk()).getHandle());
}
public static void sendNearbyChunkUpdates(LevelChunk chunk) {
ChunkPos pos = chunk.getPos();
for (Vector vec : RELATIVE_CHUNKS) {
ChunkAccess other = chunk.getLevel().getChunk(pos.x + vec.getBlockX(), pos.z + vec.getBlockZ(), ChunkStatus.FULL, false);
if (other instanceof LevelChunk) {
sendSingleChunkUpdate((LevelChunk) other);
}
}
}
public static void sendSingleChunkUpdate(LevelChunk chunk) {
doNotCheck = true;
LevelLightEngine lightEngine = chunk.getLevel().getChunkSource().getLightEngine();
ChunkPos pos = chunk.getPos();
ClientboundLightUpdatePacket packet = new ClientboundLightUpdatePacket(pos, lightEngine, null, null, true); // TODO: 1.16: should 'trust edges' be true here?
((ServerChunkCache) chunk.getLevel().getChunkSource()).chunkMap.getPlayers(pos, false).forEach((player) -> {
player.connection.send(packet);
});
doNotCheck = false;
}
}