Compare commits

...
3 Commits
Author SHA1 Message Date
Cubicake e1f70a296c WIP, still broken. Don't fork based off of this branch 2025-07-18 11:55:05 +09:30
Cubicake 3eccd69669 Tidy up 2025-07-15 16:30:33 +09:30
Cubicake 9f27f1c58f Store entities 2025-07-15 14:48:53 +09:30
6 changed files with 214 additions and 23 deletions

No files matched your search

@@ -5,14 +5,20 @@ import games.cubi.raycastedEntityOcclusion.Snapshot.ChunkSnapshotManager;
import games.cubi.raycastedEntityOcclusion.ConfigManager;
import games.cubi.raycastedEntityOcclusion.RaycastedEntityOcclusion;
import games.cubi.raycastedEntityOcclusion.Snapshot.EntitySnapshotManager;
import io.papermc.paper.threadedregions.scheduler.AsyncScheduler;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
public class Engine {
@@ -43,10 +49,30 @@ public class Engine {
visible = v;
}
}
private static class PlayerInfo {
final Player player;
final Location location;
final Location predLoc;
public static void runEngine(ConfigManager cfg, ChunkSnapshotManager snapMgr, MovementTracker tracker, RaycastedEntityOcclusion plugin) {
PlayerInfo(Player p, Location loc, Location pred) {
player = p;
location = loc;
predLoc = pred;
}
}
private static Consumer<ScheduledTask> raycastEntitiesAroundPlayer(Player player, Location eye, Location predEye){
return task -> { /* This is needed for it to return, idk why*/ };
}
Map<UUID, RayResult> results = new ConcurrentHashMap<>();
public static void runEngine(ConfigManager cfg, ChunkSnapshotManager snapMgr, MovementTracker tracker, EntitySnapshotManager entitySnapshotManager, RaycastedEntityOcclusion plugin) {
// ----- PHASE 1: SYNC GATHER -----
List<RayJob> jobs = new ArrayList<>();
List<PlayerInfo> info = new ArrayList<>();
// TODO: Store players in EntitySnapshotManager and use that to avoid iterating through all players every tick
AsyncScheduler asyncScheduler = plugin.getServer().getAsyncScheduler();
for (Player p : Bukkit.getOnlinePlayers()) {
if (p.hasPermission("raycastedentityocclusions.bypass")) continue;
Location eye = p.getEyeLocation().clone();
@@ -55,12 +81,20 @@ public class Engine {
// getPredictedLocation returns null if insufficient data or too slow
predEye = tracker.getPredictedLocation(p);
}
asyncScheduler.runNow(plugin, raycastEntitiesAroundPlayer(p, eye, predEye));
}
for (Entity e : p.getNearbyEntities(cfg.searchRadius, cfg.searchRadius, cfg.searchRadius)) {
// ----- PHASE 2: ASYNC RAYCASTS -----
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
List<RayResult> results = new ArrayList<>();
for (Entity e : entitySnapshotManager.getBukkitEntitiesNear(eye, cfg.searchRadius, cfg.searchRadius, cfg.searchRadius)) {
if (e == p) continue;
// Cull-players logic
if (e instanceof Player pl && (!cfg.cullPlayers || (cfg.onlyCullSneakingPlayers && !pl.isSneaking()))) {
p.showEntity(plugin, e);
//TODO: Add to result that player should be shown
continue;
}
@@ -77,11 +111,15 @@ public class Engine {
jobs.add(new RayJob(p, e, eye, predEye, target));
}
}
}
// ----- PHASE 2: ASYNC RAYCASTS -----
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
List<RayResult> results = new ArrayList<>(jobs.size());
for (RayJob job : jobs) {
// if the player is not in the same world as the target, skip
if (!job.player.getWorld().equals(job.target.getWorld())) {
@@ -210,7 +248,4 @@ public class Engine {
}
});
}
}
/*
*/
}
@@ -36,6 +36,7 @@ public class MovementTracker {
* Predicts location 5 ticks ahead based on last 5 ticks. Returns null if insufficient or too slow.
*/
public Location getPredictedLocation(Player p) {
//TODO: Make the predicted location incorporate ping
Deque<Location> dq = history.get(p);
if (dq == null || dq.size() < 5) return null;
Location old = dq.peekFirst();
@@ -7,13 +7,10 @@ import org.bukkit.Particle;
import org.bukkit.util.Vector;
public class RaycastUtil {
private static final Particle.DustOptions DUST_RED = new Particle.DustOptions(org.bukkit.Color.RED, 1f);
private static final Particle.DustOptions DUST_GREEN = new Particle.DustOptions(org.bukkit.Color.GREEN, 1f);;
public static boolean raycast(Location start, Location end, int maxOccluding, boolean debug, ChunkSnapshotManager snap) {
Particle.DustOptions dustRed = null;
Particle.DustOptions dustGreen = null;
if (debug) {
dustRed = new Particle.DustOptions(org.bukkit.Color.RED, 1f);
dustGreen = new Particle.DustOptions(org.bukkit.Color.GREEN, 1f);
}
double total = start.distance(end);
double traveled = 0;
Location curr = start.clone();
@@ -28,12 +25,12 @@ public class RaycastUtil {
if (mat.isOccluding()) {
maxOccluding--;
if (debug) {
start.getWorld().spawnParticle(Particle.DUST, curr, 1, dustRed);
start.getWorld().spawnParticle(Particle.DUST, curr, 1, DUST_RED);
}
if (maxOccluding < 1) return false;
}
else if (debug) {
start.getWorld().spawnParticle(Particle.DUST, curr, 1, dustGreen);
start.getWorld().spawnParticle(Particle.DUST, curr, 1, DUST_GREEN);
}
}
return true;
@@ -7,6 +7,7 @@ import games.cubi.raycastedEntityOcclusion.Packets.Registrar;
import games.cubi.raycastedEntityOcclusion.Raycast.Engine;
import games.cubi.raycastedEntityOcclusion.Raycast.MovementTracker;
import games.cubi.raycastedEntityOcclusion.Snapshot.ChunkSnapshotManager;
import games.cubi.raycastedEntityOcclusion.Snapshot.EntitySnapshotManager;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
@@ -20,10 +21,12 @@ import games.cubi.raycastedEntityOcclusion.bStats.MetricsCollector;
public class RaycastedEntityOcclusion extends JavaPlugin implements CommandExecutor {
private ConfigManager cfg;
private ChunkSnapshotManager snapMgr;
private EntitySnapshotManager entitySnapshotManager;
private MovementTracker tracker;
private CommandsManager commands;
private boolean packetEventsPresent = false;
private PacketProcessor packetProcessor = null;
private UpdateChecker updateChecker;
public int tick = 0;
@@ -44,8 +47,10 @@ public class RaycastedEntityOcclusion extends JavaPlugin implements CommandExecu
public void onEnable() {
cfg = new ConfigManager(this);
snapMgr = new ChunkSnapshotManager(this);
tracker = new MovementTracker(this);
tracker = new MovementTracker(this, cfg);
commands = new CommandsManager(this, cfg);
updateChecker = new UpdateChecker(this);
entitySnapshotManager = new EntitySnapshotManager();
getServer().getPluginManager().registerEvents(new EventListener(this, snapMgr, cfg), this);
//Brigadier API
LiteralCommandNode<CommandSourceStack> buildCommand = commands.registerCommand();
@@ -71,7 +76,7 @@ public class RaycastedEntityOcclusion extends JavaPlugin implements CommandExecu
@Override
public void run() {
tick++;
Engine.runEngine(cfg, snapMgr, tracker, RaycastedEntityOcclusion.this);
Engine.runEngine(cfg, snapMgr, tracker, entitySnapshotManager, RaycastedEntityOcclusion.this);
Engine.runTileEngine(cfg, snapMgr, tracker, RaycastedEntityOcclusion.this);
}
}.runTaskTimer(this, 0L, 1L);
@@ -0,0 +1,148 @@
package games.cubi.raycastedEntityOcclusion.Snapshot;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import javax.annotation.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class EntitySnapshotManager {
public static class EntityData {
//Entity data for a single entity in a world.
private volatile Location location;
/** Player UUID → can-see flag (true/false). Absence = NOT_SET. */
private final Map<UUID, Boolean> visibility = new ConcurrentHashMap<>();
public EntityData(Location initial) {
this.location = initial.clone();
}
/* ---- location ---- */
public Location getLocation() { return location.clone(); }
public void setLocation(Location loc) { this.location = loc.clone(); }
/* ---- visibility ---- */
public void setVisibleFor(UUID playerId, boolean canSee) {
visibility.put(playerId, canSee);
}
public void clearVisibility(UUID playerId) { visibility.remove(playerId); }
public Boolean getVisibility(UUID playerId) {
return visibility.get(playerId); // may be null
}
}
// Cache: world-name → (entity-UUID → data)
private final Map<String, Map<UUID, EntityData>> cache = new ConcurrentHashMap<>();
public void addEntity(Entity entity) {
Objects.requireNonNull(entity, "EntitySnapshotManager: Cannot add null entity");
cache.computeIfAbsent(entity.getWorld().getName(),
w -> new ConcurrentHashMap<>())
.putIfAbsent(entity.getUniqueId(),
new EntityData(entity.getLocation()));
}
public void removeEntity(Entity entity) {
Map<UUID, EntityData> entitiesInWorld = cache.get(entity.getWorld().getName());
if (entitiesInWorld != null) {
entitiesInWorld.remove(entity.getUniqueId());
if (entitiesInWorld.isEmpty()) cache.remove(entity.getWorld().getName());
}
}
/** Updates (or inserts) the latest location for the entity. */
public void updateLocation(Entity entity) {
cache.computeIfAbsent(entity.getWorld().getName(),
w -> new ConcurrentHashMap<>())
.compute(entity.getUniqueId(), (id, data) -> {
if (data == null) return new EntityData(entity.getLocation());
data.setLocation(entity.getLocation());
return data;
});
}
// put anywhere inside EntitySnapshotManager
public @Nullable Location getLocation(Entity entity) {
Map<UUID, EntityData> entitiesInWorld = cache.get(entity.getWorld().getName());
if (entitiesInWorld == null) return null;
EntityData data = entitiesInWorld.get(entity.getUniqueId());
return (data == null) ? null : data.getLocation();
}
/* ---- visibility helpers ---- */
public void setVisibility(Entity entity, Player player, boolean canSee) {
Map<UUID, EntityData> entitiesInWorld = cache.get(entity.getWorld().getName());
if (entitiesInWorld != null) {
EntityData d = entitiesInWorld.get(entity.getUniqueId());
if (d != null) d.setVisibleFor(player.getUniqueId(), canSee);
}
}
public void clearVisibility(Entity entity, Player player) {
Map<UUID, EntityData> entitiesInWorld = cache.get(entity.getWorld().getName());
if (entitiesInWorld != null) {
EntityData d = entitiesInWorld.get(entity.getUniqueId());
if (d != null) d.clearVisibility(player.getUniqueId());
}
}
public Boolean getVisibility(Entity entity, Player player) {
Map<UUID, EntityData> entitiesInWorld = cache.get(entity.getWorld().getName());
if (entitiesInWorld == null) return null;
EntityData d = entitiesInWorld.get(entity.getUniqueId());
return (d == null) ? null : d.getVisibility(player.getUniqueId());
}
/* ---- miscellaneous ---- */
public Collection<EntityData> getEntitiesInWorld(World world) {
Map<UUID, EntityData> entitiesInWorld = cache.get(world.getName());
return (entitiesInWorld == null) ? Collections.emptyList() : entitiesInWorld.values();
}
public List<Entity> getBukkitEntitiesNear(Location origin, int radiusX, int radiusY, int radiusZ) {
World world = origin.getWorld();
Map<UUID, EntityData> entitiesInWorld = cache.get(world.getName());
if (entitiesInWorld == null || entitiesInWorld.isEmpty()) {
return Collections.emptyList();
}
double ox = origin.getX(), oy = origin.getY(), oz = origin.getZ();
double minX = ox - radiusX, maxX = ox + radiusX;
double minY = oy - radiusY, maxY = oy + radiusY;
double minZ = oz - radiusZ, maxZ = oz + radiusZ;
List<Entity> result = new ArrayList<>();
for (Map.Entry<UUID, EntityData> entry : entitiesInWorld.entrySet()) {
EntityData data = entry.getValue();
Location loc = data.getLocation();
double x = loc.getX(), y = loc.getY(), z = loc.getZ();
if (x < minX || x > maxX || y < minY || y > maxY || z < minZ || z > maxZ) {
Entity live = world.getEntity(entry.getKey());
if (live != null) {
if (!live.isDead()) {
result.add(live);
}
else removeEntity(live);
}
}
}
return result;
}
public int size() {
return cache.values().stream().mapToInt(Map::size).sum();
}
}
@@ -4,9 +4,9 @@ package games.cubi.raycastedEntityOcclusion;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.kyori.adventure.audience.Audience;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -15,6 +15,7 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
public class UpdateChecker {
@@ -22,6 +23,7 @@ public class UpdateChecker {
public UpdateChecker(RaycastedEntityOcclusion plugin) {
this.plugin = plugin;
plugin.getLogger().info("Checking for updates...");
checkForUpdates(plugin, Bukkit.getConsoleSender());
}
@@ -55,9 +57,12 @@ public class UpdateChecker {
}
public static void checkForUpdates(RaycastedEntityOcclusion plugin, CommandSender audience) {
//plugin.getLogger().info("Fetching latest version from Modrinth...");
fetchFeaturedVersion(plugin).thenAccept(version -> {
//plugin.getLogger().info("Latest version fetched: " + version);
// This runs synchronously when the version is fetched
Bukkit.getScheduler().runTask(plugin, () -> {
//plugin.getLogger().info("Checking if current version matches the latest version...");
if (plugin.getDescription().getVersion().equals(version)) {
audience.sendRichMessage("<green>You are using the latest version of Raycasted Entity Occlusions.");
} else {