Skip to content

Commit

Permalink
[3.4.0] Added LightType support to enable both SKY and BLOCK light types
Browse files Browse the repository at this point in the history
- Now all methods (createLight,deleteLight,collectChunks,updateChunk) support parameter LightType
- Fixed minor bugs
  • Loading branch information
Qveshn committed Sep 14, 2019
1 parent 03afc52 commit f179ba9
Show file tree
Hide file tree
Showing 20 changed files with 500 additions and 235 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Vladimir Mikhailov <[email protected]>
* Copyright (c) 2016 Vladimir Mikhailov <[email protected]>
* Copyright (c) 2016-2017 The ImplexDevOne Project
* Copyright (c) 2019 Qveshn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,27 +23,9 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package ru.beykerykt.lightapi.server.nms.craftbukkit;
package ru.beykerykt.lightapi;

public enum LightType {

/**
* Light emanates from the block (torch, glowstone, etc.)
*/
BLOCK(0),

/**
* N/A
*/
SKY(1);

private final int id;

private LightType(int id) {
this.id = id;
}

public int getId() {
return id;
}
SKY,
BLOCK
}
11 changes: 8 additions & 3 deletions common/src/main/java/ru/beykerykt/lightapi/chunks/ChunkInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ChunkInfo(World world, int chunkX, int chunkZ, Collection<? extends Playe
public ChunkInfo(World world, int chunkX, int chunk_y_height, int chunkZ, Collection<? extends Player> players) {
this.world = world;
this.x = chunkX;
this.y = chunk_y_height;
this.y = chunk_y_height >> 4;
this.z = chunkZ;
this.receivers = players;
}
Expand All @@ -58,15 +58,20 @@ public int getChunkX() {
return x;
}

public int getChunkY() {
return y;
}

public int getChunkZ() {
return z;
}

@Deprecated
public int getChunkYHeight() {
return y;
return y << 4;
}

@SuppressWarnings("unused")
@Deprecated
public void setChunkYHeight(int y) {
this.y = y;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,40 @@
import org.bukkit.World;
import org.bukkit.entity.Player;

import ru.beykerykt.lightapi.LightType;
import ru.beykerykt.lightapi.chunks.ChunkInfo;

public interface INMSHandler {

// Lights...
@Deprecated
void createLight(World world, int x, int y, int z, int light);

void createLight(World world, int x, int y, int z, LightType lightType, int light);

@Deprecated
void deleteLight(World world, int x, int y, int z);

void deleteLight(World world, int x, int y, int z, LightType lightType);

@Deprecated
void recalculateLight(World world, int x, int y, int z);

// Chunks...
List<ChunkInfo> collectChunks(World world, int blockX, int blockY, int blockZ, int lightLevel);

@Deprecated
List<ChunkInfo> collectChunks(World world, int x, int y, int z);

@Deprecated
List<ChunkInfo> collectChunks(World world, int blockX, int blockY, int blockZ, int lightLevel);

List<ChunkInfo> collectChunks(World world, int blockX, int blockY, int blockZ, LightType lightType, int lightLevel);

@Deprecated
void sendChunkSectionsUpdate(
World world, int chunkX, int chunkZ, int sectionsMask, Collection<? extends Player> players
);

@Deprecated
void sendChunkSectionsUpdate(World world, int chunkX, int chunkZ, int sectionsMask, Player player);

@Deprecated
Expand All @@ -66,6 +78,24 @@ void sendChunkSectionsUpdate(
@Deprecated
void sendChunkUpdate(World world, int chunkX, int y, int chunkZ, Player player);

void sendChunkSectionsUpdate(
World world,
int chunkX,
int chunkZ,
int sectionsMaskSky,
int sectionsMaskBlock,
Collection<? extends Player> players
);

void sendChunkSectionsUpdate(
World world,
int chunkX,
int chunkZ,
int sectionsMaskSky,
int sectionsMaskBlock,
Player player
);

// Utils...
boolean isValidSectionY(int sectionY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import ru.beykerykt.lightapi.LightType;
import ru.beykerykt.lightapi.chunks.ChunkInfo;

import java.util.ArrayList;
Expand Down Expand Up @@ -70,13 +71,30 @@ public Collection<? extends Player> filterVisiblePlayers(
return result;
}

private int getDeltaLight(int x, int dx) {
return (((x ^ ((-dx >> 4) & 15)) + 1) & (-(dx & 1)));
@Deprecated
@Override
public void createLight(World world, int x, int y, int z, int light) {
createLight(world, x, y, z, LightType.BLOCK, light);
}

@Deprecated
@Override
public void deleteLight(World world, int x, int y, int z) {
deleteLight(world, x, y, z, LightType.BLOCK);
}

@Override
public List<ChunkInfo> collectChunks(World world, int blockX, int blockY, int blockZ, int lightLevel) {
public List<ChunkInfo> collectChunks(World world, int blockX, int blockY, int blockZ, int lightLevel
) {
return collectChunks(world, blockX, blockY, blockZ, LightType.BLOCK, lightLevel);
}

@Override
public List<ChunkInfo> collectChunks(
World world, int blockX, int blockY, int blockZ, LightType lightType, int lightLevel
) {
List<ChunkInfo> list = new ArrayList<ChunkInfo>();
Collection<Player> players = null;
if (lightLevel > 0) {
for (int dx = -1; dx <= 1; dx++) {
int lightLevelX = lightLevel - getDeltaLight(blockX & 15, dx);
Expand All @@ -85,18 +103,17 @@ public List<ChunkInfo> collectChunks(World world, int blockX, int blockY, int bl
int lightLevelZ = lightLevelX - getDeltaLight(blockZ & 15, dz);
if (lightLevelZ > 0) {
for (int dy = -1; dy <= 1; dy++) {
int lightLevelY = lightLevelZ - getDeltaLight(blockY & 15, dy);
if (lightLevelY > 0) {
int sectionY = blockY >> 4;
if (lightLevelZ > getDeltaLight(blockY & 15, dy)) {
int sectionY = (blockY >> 4) + dy;
if (isValidSectionY(sectionY)) {
int chunkX = blockX >> 4;
int chunkZ = blockZ >> 4;
ChunkInfo cCoord = new ChunkInfo(
world,
chunkX + dx,
((sectionY + dy) << 4) + 15,
sectionY << 4,
chunkZ + dz,
world.getPlayers());
players != null ? players : (players = world.getPlayers()));
list.add(cCoord);
}
}
Expand All @@ -109,58 +126,81 @@ public List<ChunkInfo> collectChunks(World world, int blockX, int blockY, int bl
return list;
}

private int getDeltaLight(int x, int dx) {
return (((x ^ ((-dx >> 4) & 15)) + 1) & (-(dx & 1)));
}

@Deprecated
@Override
public void sendChunkSectionsUpdate(
World world, int chunkX, int chunkZ, int sectionsMask, Collection<? extends Player> players
) {
sendChunkSectionsUpdate(world, chunkX, chunkZ, 0, sectionsMask, players);
}

@Override
public void sendChunkSectionsUpdate(
World world, int chunkX, int chunkZ,
int sectionsMaskSky, int sectionsMaskBlock, Collection<? extends Player> players
) {
for (Player player : players) {
sendChunkSectionsUpdate(world, chunkX, chunkZ, sectionsMask, player);
sendChunkSectionsUpdate(world, chunkX, chunkZ, sectionsMaskSky, sectionsMaskBlock, player);
}
}

protected void recalculateLighting(World world, int x, int y, int z) {
throw new UnsupportedOperationException("recalculateLighting not implemented");
@Deprecated
@Override
public void sendChunkSectionsUpdate(World world, int chunkX, int chunkZ, int sectionsMask, Player player) {
sendChunkSectionsUpdate(world, chunkX, chunkZ, 0, sectionsMask, player);
}

protected void recalculateNeighbour(World world, int x, int y, int z) {
recalculateLighting(world, x - 1, y, z);
recalculateLighting(world, x + 1, y, z);
recalculateLighting(world, x, y - 1, z);
recalculateLighting(world, x, y + 1, z);
recalculateLighting(world, x, y, z - 1);
recalculateLighting(world, x, y, z + 1);
@Deprecated
@Override
public void recalculateLight(World world, int x, int y, int z) {
recalculateLighting(world, x, y, z, LightType.BLOCK);
}

protected abstract void recalculateLighting(World world, int x, int y, int z, LightType lightType);

protected void recalculateNeighbours(World world, int x, int y, int z, LightType lightType) {
recalculateLighting(world, x - 1, y, z, lightType);
recalculateLighting(world, x + 1, y, z, lightType);
recalculateLighting(world, x, y - 1, z, lightType);
recalculateLighting(world, x, y + 1, z, lightType);
recalculateLighting(world, x, y, z - 1, lightType);
recalculateLighting(world, x, y, z + 1, lightType);
}

@Deprecated
@Override
public List<ChunkInfo> collectChunks(World world, int x, int y, int z) {
return collectChunks(world, x, y, z, 15);
return collectChunks(world, x, y, z, LightType.BLOCK, 15);
}

@Deprecated
@Override
public void sendChunkUpdate(World world, int chunkX, int chunkZ, Collection<? extends Player> players) {
sendChunkUpdate(world, chunkX, chunkZ, isValidSectionY(-1) ? 0x3ffff : 0xffff, players);
sendChunkSectionsUpdate(world, chunkX, chunkZ, 0, isValidSectionY(-1) ? 0x3ffff : 0xffff, players);
}

@Deprecated
@Override
public void sendChunkUpdate(World world, int chunkX, int chunkZ, Player player) {
sendChunkSectionsUpdate(world, chunkX, chunkZ, isValidSectionY(-1) ? 0x3ffff : 0xffff, player);
sendChunkSectionsUpdate(world, chunkX, chunkZ, 0, isValidSectionY(-1) ? 0x3ffff : 0xffff, player);
}

@Deprecated
@Override
public void sendChunkUpdate(World world, int chunkX, int y, int chunkZ, Collection<? extends Player> players) {
int mask = getThreeSectionsMask(y);
if (mask != 0) sendChunkUpdate(world, chunkX, chunkZ, mask, players);
if (mask != 0) sendChunkSectionsUpdate(world, chunkX, chunkZ, 0, mask, players);
}

@Deprecated
@Override
public void sendChunkUpdate(World world, int chunkX, int y, int chunkZ, Player player) {
int mask = getThreeSectionsMask(y);
if (mask != 0) sendChunkSectionsUpdate(world, chunkX, chunkZ, mask, player);
if (mask != 0) sendChunkSectionsUpdate(world, chunkX, chunkZ, 0, mask, player);
}

private int getThreeSectionsMask(int y) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,36 @@
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_10_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import ru.beykerykt.lightapi.LightType;
import ru.beykerykt.lightapi.server.nms.NmsHandlerBase;

public class CraftBukkit_v1_10_R1 extends NmsHandlerBase {

@Override
public void createLight(World world, int x, int y, int z, int light) {
public void createLight(World world, int x, int y, int z, LightType lightType, int light) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
worldServer.a(EnumSkyBlock.BLOCK, new BlockPosition(x, y, z), light);
recalculateNeighbour(world, x, y, z);
worldServer.a(lightType == LightType.SKY ? EnumSkyBlock.SKY : EnumSkyBlock.BLOCK,
new BlockPosition(x, y, z), light);
recalculateNeighbours(world, x, y, z, lightType);
}

@Override
public void deleteLight(World world, int x, int y, int z) {
recalculateLighting(world, x, y, z);
public void deleteLight(World world, int x, int y, int z, LightType lightType) {
recalculateLighting(world, x, y, z, lightType);
}

@Deprecated
@Override
public void recalculateLight(World world, int x, int y, int z) {
recalculateLighting(world, x, y, z);
}

protected void recalculateLighting(World world, int x, int y, int z) {
protected void recalculateLighting(World world, int x, int y, int z, LightType lightType) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
BlockPosition position = new BlockPosition(x, y, z);
worldServer.c(EnumSkyBlock.BLOCK, position);
worldServer.c(lightType == LightType.SKY ? EnumSkyBlock.SKY : EnumSkyBlock.BLOCK, position);
}

@Override
public void sendChunkSectionsUpdate(World world, int chunkX, int chunkZ, int sectionsMask, Player player) {
public void sendChunkSectionsUpdate(
World world, int chunkX, int chunkZ, int sectionsMaskSky, int sectionsMaskBlock, Player player
) {
int sectionsMask = sectionsMaskSky | sectionsMaskBlock;
Chunk chunk = ((CraftWorld) world).getHandle().getChunkAt(chunkX, chunkZ);
// The last argument is bit-mask what chunk sections to update. Mask containing
// 16 bits, with the lowest bit corresponding to chunk section 0 (y=0 to y=15)
Expand Down
Loading

0 comments on commit f179ba9

Please sign in to comment.