Skip to content

Commit

Permalink
Separate lighting task
Browse files Browse the repository at this point in the history
  • Loading branch information
Realizedd committed Apr 5, 2022
1 parent c22dfc7 commit caee255
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ public void setBlockFast(final Block block, final Material material, final int d
} catch (IllegalAccessException | InvocationTargetException ignored) {}
}
}

@Override
public void updateLighting(Block bukkitBlock) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.realized.de.arenaregen.util;

import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;

public final class BlockUtil {

public static boolean isSurrounded(final Block block) {
final Block east = block.getRelative(BlockFace.EAST);
final Block west = block.getRelative(BlockFace.WEST);
final Block south = block.getRelative(BlockFace.SOUTH);
final Block north = block.getRelative(BlockFace.NORTH);
final Block up = block.getRelative(BlockFace.UP);
final Block down = block.getRelative(BlockFace.DOWN);
return !east.getType().isTransparent()
&& !west.getType().isTransparent()
&& !up.getType().isTransparent()
&& !down.getType().isTransparent()
&& !south.getType().isTransparent()
&& !north.getType().isTransparent();
}

private BlockUtil() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import me.realized.de.arenaregen.ArenaRegen;
import me.realized.de.arenaregen.config.Config;
import me.realized.de.arenaregen.nms.NMS;
import me.realized.de.arenaregen.nms.fallback.NMSHandler;
import me.realized.de.arenaregen.util.BlockInfo;
import me.realized.de.arenaregen.util.BlockUtil;
import me.realized.de.arenaregen.util.Callback;
import me.realized.de.arenaregen.util.Pair;
import me.realized.de.arenaregen.util.Position;
Expand Down Expand Up @@ -292,21 +294,22 @@ public class IndexTask extends BukkitRunnable {

private final Callback onDone;
private final Queue<Pair<Block, BlockInfo>> changed = new LinkedList<>();
private int y = max.getBlockY();
private int x = min.getBlockX();

public IndexTask(final Callback onDone) {
this.onDone = onDone;
}

@Override
public void run() {
for (int x = min.getBlockX(); x <= max.getBlockX(); x++) {
for (int y = min.getBlockY(); y <= max.getBlockY(); y++) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) {
final Block block = min.getWorld().getBlockAt(x, y, z);
final Position position = new Position(block);
final BlockInfo info = blocks.get(position);

chunks.add(new ChunkLoc(block.getChunk()));

if (info == null) {
// If no stored information is available (= air) but block is not air, set to air
if (block.getType() != Material.AIR) {
Expand All @@ -322,9 +325,9 @@ public void run() {
}
}

y--;
x++;

if (y < min.getBlockY()) {
if (x > max.getBlockX()) {
cancel();
task = new ResetTask(onDone, changed);
task.runTaskTimer(api, 1L, 1L);
Expand Down Expand Up @@ -359,11 +362,46 @@ public void run() {
}

cancel();
task = new ChunkRefreshTask(onDone);

// Skip relighting if using fallback handler
task = handler instanceof NMSHandler ? new ChunkRefreshTask(onDone) : new RelightTask(onDone);
task.runTaskTimer(api, 1L, 1L);
}
}

public class RelightTask extends BukkitRunnable {

private final Callback onDone;
private int x = min.getBlockX();

public RelightTask(final Callback onDone) {
this.onDone = onDone;
}

@Override
public void run() {
for (int y = min.getBlockY(); y <= max.getBlockY(); y++) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) {
final Block block = min.getWorld().getBlockAt(x, y, z);

if (block.getType() == Material.AIR || BlockUtil.isSurrounded(block)) {
continue;
}

handler.updateLighting(block);
}
}

x++;

if (x > max.getBlockX()) {
cancel();
task = new ChunkRefreshTask(onDone);
task.runTaskTimer(api, 1L, 1L);
}
}
}


public class ChunkRefreshTask extends BukkitRunnable {

Expand Down
2 changes: 1 addition & 1 deletion arenaregen/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
selecting-tool: IRON_HOE

# Blocks to reset per tick.
blocks-per-tick: 25
blocks-per-tick: 1000

# Allow breaking blocks in the reset zone that is not placed by a player in match.
# default: false
Expand Down
2 changes: 2 additions & 0 deletions nms/src/main/java/me/realized/de/arenaregen/nms/NMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface NMS {

void setBlockFast(final Block bukkitBlock, final Material material, final int data);

void updateLighting(final Block bukkitBlock);

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,13 @@ public void setBlockFast(final Block bukkitBlock, final Material material, final
final net.minecraft.server.v1_12_R1.Block block = CraftMagicNumbers.getBlock(material);
final IBlockData blockData = block.fromLegacyData(data);
chunk.a(position, blockData);
}

if (bukkitBlock.getType() == Material.AIR || isSurrounded(bukkitBlock)) {
return;
}

@Override
public void updateLighting(Block bukkitBlock) {
final int x = bukkitBlock.getX(), y = bukkitBlock.getY(), z = bukkitBlock.getZ();
final BlockPosition position = new BlockPosition(x, y, z);
final World world = ((CraftWorld) bukkitBlock.getWorld()).getHandle();
world.c(EnumSkyBlock.BLOCK, position);
}

private boolean isSurrounded(final Block block) {
final Block east = block.getRelative(BlockFace.EAST);
final Block west = block.getRelative(BlockFace.WEST);
final Block south = block.getRelative(BlockFace.SOUTH);
final Block north = block.getRelative(BlockFace.NORTH);
final Block up = block.getRelative(BlockFace.UP);
final Block down = block.getRelative(BlockFace.DOWN);
return !east.getType().isTransparent()
&& !west.getType().isTransparent()
&& !up.getType().isTransparent()
&& !down.getType().isTransparent()
&& !south.getType().isTransparent()
&& !north.getType().isTransparent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,13 @@ public void setBlockFast(final Block bukkitBlock, final Material material, final
final net.minecraft.server.v1_14_R1.Block block = CraftMagicNumbers.getBlock(material);
final IBlockData blockData = block.getBlockData();
chunk.setType(position, blockData, true);
}

if (bukkitBlock.getType() == Material.AIR || isSurrounded(bukkitBlock)) {
return;
}

@Override
public void updateLighting(Block bukkitBlock) {
final int x = bukkitBlock.getX(), y = bukkitBlock.getY(), z = bukkitBlock.getZ();
final BlockPosition position = new BlockPosition(x, y, z);
final World world = ((CraftWorld) bukkitBlock.getWorld()).getHandle();
world.getChunkProvider().getLightEngine().a(position);
}

private boolean isSurrounded(final Block block) {
final Block east = block.getRelative(BlockFace.EAST);
final Block west = block.getRelative(BlockFace.WEST);
final Block south = block.getRelative(BlockFace.SOUTH);
final Block north = block.getRelative(BlockFace.NORTH);
final Block up = block.getRelative(BlockFace.UP);
final Block down = block.getRelative(BlockFace.DOWN);
return !east.getType().isTransparent()
&& !west.getType().isTransparent()
&& !up.getType().isTransparent()
&& !down.getType().isTransparent()
&& !south.getType().isTransparent()
&& !north.getType().isTransparent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,13 @@ public void setBlockFast(final Block bukkitBlock, final Material material, final
final net.minecraft.server.v1_15_R1.Block block = CraftMagicNumbers.getBlock(material);
final IBlockData blockData = block.getBlockData();
chunk.setType(position, blockData, true);
}

if (bukkitBlock.getType() == Material.AIR || isSurrounded(bukkitBlock)) {
return;
}

@Override
public void updateLighting(Block bukkitBlock) {
final int x = bukkitBlock.getX(), y = bukkitBlock.getY(), z = bukkitBlock.getZ();
final BlockPosition position = new BlockPosition(x, y, z);
final World world = ((CraftWorld) bukkitBlock.getWorld()).getHandle();
world.getChunkProvider().getLightEngine().a(position);
}

private boolean isSurrounded(final Block block) {
final Block east = block.getRelative(BlockFace.EAST);
final Block west = block.getRelative(BlockFace.WEST);
final Block south = block.getRelative(BlockFace.SOUTH);
final Block north = block.getRelative(BlockFace.NORTH);
final Block up = block.getRelative(BlockFace.UP);
final Block down = block.getRelative(BlockFace.DOWN);
return !east.getType().isTransparent()
&& !west.getType().isTransparent()
&& !up.getType().isTransparent()
&& !down.getType().isTransparent()
&& !south.getType().isTransparent()
&& !north.getType().isTransparent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,13 @@ public void setBlockFast(final Block bukkitBlock, final Material material, final
final net.minecraft.server.v1_16_R3.Block block = CraftMagicNumbers.getBlock(material);
final IBlockData blockData = block.getBlockData();
chunk.setType(position, blockData, true);
}

if (bukkitBlock.getType() == Material.AIR || isSurrounded(bukkitBlock)) {
return;
}

@Override
public void updateLighting(Block bukkitBlock) {
final int x = bukkitBlock.getX(), y = bukkitBlock.getY(), z = bukkitBlock.getZ();
final BlockPosition position = new BlockPosition(x, y, z);
final World world = ((CraftWorld) bukkitBlock.getWorld()).getHandle();
world.getChunkProvider().getLightEngine().a(position);
}

private boolean isSurrounded(final Block block) {
final Block east = block.getRelative(BlockFace.EAST);
final Block west = block.getRelative(BlockFace.WEST);
final Block south = block.getRelative(BlockFace.SOUTH);
final Block north = block.getRelative(BlockFace.NORTH);
final Block up = block.getRelative(BlockFace.UP);
final Block down = block.getRelative(BlockFace.DOWN);
return !east.getType().isTransparent()
&& !west.getType().isTransparent()
&& !up.getType().isTransparent()
&& !down.getType().isTransparent()
&& !south.getType().isTransparent()
&& !north.getType().isTransparent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,13 @@ public void setBlockFast(final Block bukkitBlock, final Material material, final
final net.minecraft.world.level.block.Block block = CraftMagicNumbers.getBlock(material);
final IBlockData blockData = block.getBlockData();
chunk.setType(position, blockData, true);
}

if (bukkitBlock.getType() == Material.AIR || isSurrounded(bukkitBlock)) {
return;
}

@Override
public void updateLighting(Block bukkitBlock) {
final int x = bukkitBlock.getX(), y = bukkitBlock.getY(), z = bukkitBlock.getZ();
final BlockPosition position = new BlockPosition(x, y, z);
final World world = ((CraftWorld) bukkitBlock.getWorld()).getHandle();
world.getChunkProvider().getLightEngine().a(position);
}

private boolean isSurrounded(final Block block) {
final Block east = block.getRelative(BlockFace.EAST);
final Block west = block.getRelative(BlockFace.WEST);
final Block south = block.getRelative(BlockFace.SOUTH);
final Block north = block.getRelative(BlockFace.NORTH);
final Block up = block.getRelative(BlockFace.UP);
final Block down = block.getRelative(BlockFace.DOWN);
return !east.getType().isTransparent()
&& !west.getType().isTransparent()
&& !up.getType().isTransparent()
&& !down.getType().isTransparent()
&& !south.getType().isTransparent()
&& !north.getType().isTransparent();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package me.realized.de.arenaregen.nms.v1_18_R1;

import me.realized.de.arenaregen.nms.NMS;
import net.minecraft.core.BlockPosition;
import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
import net.minecraft.world.level.World;
import net.minecraft.world.level.block.state.IBlockData;
import net.minecraft.world.level.chunk.Chunk;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_18_R1.CraftChunk;
import org.bukkit.craftbukkit.v1_18_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_18_R1.util.CraftMagicNumbers;
import org.bukkit.entity.Player;

import me.realized.de.arenaregen.nms.NMS;
import net.minecraft.core.BlockPosition;
import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
import net.minecraft.world.level.block.state.IBlockData;

public class NMSHandler implements NMS {

@Override
Expand All @@ -31,27 +31,13 @@ public void setBlockFast(final Block bukkitBlock, final Material material, final
final net.minecraft.world.level.block.Block block = CraftMagicNumbers.getBlock(material);
final IBlockData blockData = block.n();
chunk.a(position, blockData, true);
}

if (bukkitBlock.getType() == Material.AIR || isSurrounded(bukkitBlock)) {
return;
}

@Override
public void updateLighting(Block bukkitBlock) {
final int x = bukkitBlock.getX(), y = bukkitBlock.getY(), z = bukkitBlock.getZ();
final BlockPosition position = new BlockPosition(x, y, z);
final World world = ((CraftWorld) bukkitBlock.getWorld()).getHandle();
world.L().m().a(position);
}

private boolean isSurrounded(final Block block) {
final Block east = block.getRelative(BlockFace.EAST);
final Block west = block.getRelative(BlockFace.WEST);
final Block south = block.getRelative(BlockFace.SOUTH);
final Block north = block.getRelative(BlockFace.NORTH);
final Block up = block.getRelative(BlockFace.UP);
final Block down = block.getRelative(BlockFace.DOWN);
return !east.getType().isTransparent()
&& !west.getType().isTransparent()
&& !up.getType().isTransparent()
&& !down.getType().isTransparent()
&& !south.getType().isTransparent()
&& !north.getType().isTransparent();
}
}
Loading

0 comments on commit caee255

Please sign in to comment.