Files
src
build.gradle
Code
3 241
src/main/java/com/kodari/blockveil/BlockVeilPlugin.java
package com.kodari.blockveil;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public final class BlockVeilPlugin extends JavaPlugin implements Listener {
private static final double HIDE_DISTANCE = 30.0;
private static final double HIDE_DISTANCE_SQUARED = HIDE_DISTANCE * HIDE_DISTANCE;
private final Map<ChunkKey, Set<BlockKey>> blocksByChunk = new HashMap<>();
private final Map<UUID, Set<BlockKey>> concealedByPlayer = new HashMap<>();
private BlockData air;
@Override
public void onEnable() {
air = Bukkit.createBlockData("minecraft:air");
getServer().getPluginManager().registerEvents(this, this);
for (org.bukkit.World world : Bukkit.getWorlds()) {
for (Chunk chunk : world.getLoadedChunks()) {
indexChunk(chunk);
}
}
getServer().getScheduler().runTaskTimer(this, this::refreshPlayers, 1L, 10L);
}
@EventHandler
public void onChunkLoad(ChunkLoadEvent event) {
indexChunk(event.getChunk());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent event) {
ChunkKey chunkKey = new ChunkKey(event.getWorld().getUID(), event.getChunk().getX(), event.getChunk().getZ());
Set<BlockKey> removed = blocksByChunk.remove(chunkKey);
if (removed != null) {
for (Set<BlockKey> concealed : concealedByPlayer.values()) {
concealed.removeAll(removed);
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
scheduleBlockRefresh(event.getBlockPlaced());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
scheduleBlockRefresh(event.getBlock());
}
@EventHandler
public void onQuit(PlayerQuitEvent event) {
concealedByPlayer.remove(event.getPlayer().getUniqueId());
}
private void indexChunk(Chunk chunk) {
ChunkKey chunkKey = new ChunkKey(chunk.getWorld().getUID(), chunk.getX(), chunk.getZ());
Set<BlockKey> positions = new HashSet<>();
for (BlockState state : chunk.getTileEntities()) {
if (isConcealable(state.getBlock())) {
positions.add(key(state.getBlock()));
}
}
if (positions.isEmpty()) {
blocksByChunk.remove(chunkKey);
} else {
blocksByChunk.put(chunkKey, positions);
}
}
private void scheduleBlockRefresh(Block block) {
BlockKey blockKey = key(block);
getServer().getScheduler().runTask(this, () -> {
org.bukkit.World world = Bukkit.getWorld(blockKey.worldId());
if (world == null || !world.isChunkLoaded(blockKey.x() >> 4, blockKey.z() >> 4)) {
return;
}
Block current = world.getBlockAt(blockKey.x(), blockKey.y(), blockKey.z());
ChunkKey chunkKey = new ChunkKey(blockKey.worldId(), blockKey.x() >> 4, blockKey.z() >> 4);
Set<BlockKey> positions = blocksByChunk.computeIfAbsent(chunkKey, ignored -> new HashSet<>());
if (isConcealable(current)) {
positions.add(blockKey);
} else {
positions.remove(blockKey);
if (positions.isEmpty()) {
blocksByChunk.remove(chunkKey);
}
}
});
}
private void refreshPlayers() {
int viewDistance = getServer().getViewDistance() + 1;
for (Player player : Bukkit.getOnlinePlayers()) {
refreshPlayer(player, viewDistance);
}
}
private void refreshPlayer(Player player, int viewDistance) {
UUID worldId = player.getWorld().getUID();
int playerChunkX = player.getLocation().getBlockX() >> 4;
int playerChunkZ = player.getLocation().getBlockZ() >> 4;
Set<BlockKey> candidates = new HashSet<>();
Set<BlockKey> stale = new HashSet<>();
for (int chunkX = playerChunkX - viewDistance; chunkX <= playerChunkX + viewDistance; chunkX++) {
for (int chunkZ = playerChunkZ - viewDistance; chunkZ <= playerChunkZ + viewDistance; chunkZ++) {
Set<BlockKey> positions = blocksByChunk.get(new ChunkKey(worldId, chunkX, chunkZ));
if (positions == null) {
continue;
}
for (BlockKey position : positions) {
Block block = player.getWorld().getBlockAt(position.x(), position.y(), position.z());
if (isConcealable(block)) {
candidates.add(position);
} else {
stale.add(position);
}
}
}
}
for (BlockKey position : stale) {
removeTrackedBlock(position);
}
Set<BlockKey> concealed = concealedByPlayer.computeIfAbsent(player.getUniqueId(), ignored -> new HashSet<>());
Iterator<BlockKey> iterator = concealed.iterator();
while (iterator.hasNext()) {
BlockKey position = iterator.next();
int chunkDistance = Math.max(Math.abs((position.x() >> 4) - playerChunkX),
Math.abs((position.z() >> 4) - playerChunkZ));
if (!position.worldId().equals(worldId) || chunkDistance > viewDistance) {
iterator.remove();
} else if (!candidates.contains(position)) {
player.sendBlockChange(location(position), blockData(position));
iterator.remove();
}
}
Location playerLocation = player.getLocation();
for (BlockKey position : candidates) {
Location blockLocation = location(position).add(0.5, 0.5, 0.5);
if (playerLocation.distanceSquared(blockLocation) <= HIDE_DISTANCE_SQUARED) {
if (concealed.remove(position)) {
player.sendBlockChange(location(position), blockData(position));
}
} else if (concealed.add(position)) {
player.sendBlockChange(location(position), air);
}
}
}
private void removeTrackedBlock(BlockKey position) {
ChunkKey chunkKey = new ChunkKey(position.worldId(), position.x() >> 4, position.z() >> 4);
Set<BlockKey> positions = blocksByChunk.get(chunkKey);
if (positions != null) {
positions.remove(position);
if (positions.isEmpty()) {
blocksByChunk.remove(chunkKey);
}
}
}
private Location location(BlockKey position) {
return new Location(Bukkit.getWorld(position.worldId()), position.x(), position.y(), position.z());
}
private BlockData blockData(BlockKey position) {
return Bukkit.getWorld(position.worldId()).getBlockAt(position.x(), position.y(), position.z()).getBlockData();
}
private BlockKey key(Block block) {
return new BlockKey(block.getWorld().getUID(), block.getX(), block.getY(), block.getZ());
}
private boolean isConcealable(Block block) {
String type = block.getType().name();