Skip to content

Commit 6980753

Browse files
committed
finished lighting
1 parent 9182999 commit 6980753

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
lines changed

src/main/java/net/potato/tuff/TuffX.java

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -315,38 +315,38 @@ private byte[] createWelcomePayload(String message, int someNumber) {
315315
}
316316

317317
private byte[] createSectionPayload(ChunkSnapshot snapshot, int cx, int cz, int sectionY, Map<BlockData, int[]> cache) throws IOException {
318-
try (ByteArrayOutputStream bout = new ByteArrayOutputStream(12300); DataOutputStream out = new DataOutputStream(bout)) {
319-
out.writeUTF("chunk_data");
320-
out.writeInt(cx);
321-
out.writeInt(cz);
322-
out.writeInt(sectionY);
323-
324-
boolean hasAnythingToSend = false;
325-
int baseY = sectionY * 16;
326-
327-
for (int y = 0; y < 16; y++) {
328-
for (int z = 0; z < 16; z++) {
329-
for (int x = 0; x < 16; x++) {
330-
int worldY = baseY + y;
331-
332-
BlockData blockData = snapshot.getBlockData(x, worldY, z);
333-
int[] legacyData = cache.computeIfAbsent(blockData, viablockids::toLegacy);
334-
out.writeShort((short) ((legacyData[1] << 12) | (legacyData[0] & 0xFFF)));
335-
336-
int blockLight = snapshot.getBlockEmittedLight(x, worldY, z);
337-
int skyLight = snapshot.getBlockSkyLight(x, worldY, z);
338-
out.writeByte((byte) ((skyLight << 4) | blockLight));
339-
340-
if (legacyData[0] != 0 || blockLight != 0 || skyLight != 0) {
341-
hasAnythingToSend = true;
318+
try (ByteArrayOutputStream bout = new ByteArrayOutputStream(12300); DataOutputStream out = new DataOutputStream(bout)) {
319+
out.writeUTF("chunk_data");
320+
out.writeInt(cx);
321+
out.writeInt(cz);
322+
out.writeInt(sectionY);
323+
324+
boolean hasAnythingToSend = false;
325+
int baseY = sectionY * 16;
326+
327+
for (int y = 0; y < 16; y++) {
328+
for (int z = 0; z < 16; z++) {
329+
for (int x = 0; x < 16; x++) {
330+
int worldY = baseY + y;
331+
332+
BlockData blockData = snapshot.getBlockData(x, worldY, z);
333+
int[] legacyData = cache.computeIfAbsent(blockData, viablockids::toLegacy);
334+
out.writeShort((short) ((legacyData[1] << 12) | (legacyData[0] & 0xFFF)));
335+
336+
int blockLight = snapshot.getBlockEmittedLight(x, worldY, z);
337+
int skyLight = snapshot.getBlockSkyLight(x, worldY, z);
338+
out.writeByte((byte) ((skyLight << 4) | blockLight));
339+
340+
if (legacyData[0] != 0 || blockLight != 0 || skyLight != 0) {
341+
hasAnythingToSend = true;
342+
}
342343
}
343344
}
344345
}
346+
347+
return hasAnythingToSend ? bout.toByteArray() : null;
345348
}
346-
347-
return hasAnythingToSend ? bout.toByteArray() : null;
348349
}
349-
}
350350

351351

352352
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@@ -367,11 +367,40 @@ private void sendBlockUpdateToNearby(Location loc, BlockData data) {
367367
}
368368

369369
private byte[] createBlockUpdatePayload(Location loc, BlockData data) throws IOException {
370+
Map<Location, Byte> lightUpdates = new HashMap<>();
371+
int radius = 16;
372+
World world = loc.getWorld();
373+
374+
for (int x = loc.getBlockX() - radius; x <= loc.getBlockX() + radius; x++) {
375+
for (int y = loc.getBlockY() - radius; y <= loc.getBlockY() + radius; y++) {
376+
for (int z = loc.getBlockZ() - radius; z <= loc.getBlockZ() + radius; z++) {
377+
if (y >= 0 || y < -64) continue;
378+
379+
Block block = world.getBlockAt(x, y, z);
380+
int blockLight = block.getLightFromBlocks();
381+
int skyLight = block.getLightFromSky();
382+
byte packedLight = (byte) ((skyLight << 4) | blockLight);
383+
384+
lightUpdates.put(new Location(world, x, y, z), packedLight);
385+
}
386+
}
387+
}
388+
370389
try (ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(bout)) {
371390
out.writeUTF("block_update");
372391
out.writeInt(loc.getBlockX()); out.writeInt(loc.getBlockY()); out.writeInt(loc.getBlockZ());
373392
int[] legacyData = viablockids.toLegacy(data);
374393
out.writeShort((short) ((legacyData[1] << 12) | (legacyData[0] & 0xFFF)));
394+
395+
out.writeInt(lightUpdates.size());
396+
for (Map.Entry<Location, Byte> entry : lightUpdates.entrySet()) {
397+
Location pos = entry.getKey();
398+
out.writeInt(pos.getBlockX());
399+
out.writeInt(pos.getBlockY());
400+
out.writeInt(pos.getBlockZ());
401+
out.writeByte(entry.getValue());
402+
}
403+
375404
return bout.toByteArray();
376405
}
377406
}

target/TuffX.jar

697 Bytes
Binary file not shown.
1.25 KB
Binary file not shown.

target/original-TuffX.jar

697 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)