Search in sources :

Example 1 with ChunkLocation

use of dev.frankheijden.insights.api.objects.chunk.ChunkLocation in project Insights by InsightsPlugin.

the class PlayerTrackerTask method run.

@Override
public void run() {
    var worldStorage = plugin.getWorldStorage();
    Set<ChunkLocation> locations = new HashSet<>();
    for (Map.Entry<UUID, Player> entry : plugin.getPlayerList()) {
        var location = entry.getValue().getLocation();
        var world = location.getWorld();
        Set<Long> loadedChunks = worldStorage.getWorld(world.getUID()).getChunks();
        int chunkX = location.getBlockX() >> 4;
        int chunkZ = location.getBlockZ() >> 4;
        for (int x = -1; x <= 1; x++) {
            for (int z = -1; z <= 1; z++) {
                var loc = new ChunkLocation(world, chunkX + x, chunkZ + z);
                if (!loadedChunks.contains(loc.getKey()) && !this.scanLocations.containsKey(loc)) {
                    locations.add(loc);
                }
            }
        }
    }
    if (locations.isEmpty()) {
        return;
    }
    plugin.getServer().getScheduler().runTask(plugin, () -> {
        long now = System.nanoTime();
        for (ChunkLocation loc : locations) {
            var world = loc.getWorld();
            if (world.isChunkLoaded(loc.getX(), loc.getZ())) {
                this.scanLocations.put(loc, now);
                var chunk = world.getChunkAt(loc.getX(), loc.getZ());
                plugin.getChunkContainerExecutor().submit(chunk, ScanOptions.all()).whenComplete((s, e) -> {
                    if (s == null) {
                        plugin.getLogger().warning("Error occurred while scanning " + loc);
                    }
                    this.scanLocations.remove(loc);
                });
            }
        }
    });
}
Also used : Player(org.bukkit.entity.Player) ChunkLocation(dev.frankheijden.insights.api.objects.chunk.ChunkLocation) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet)

Example 2 with ChunkLocation

use of dev.frankheijden.insights.api.objects.chunk.ChunkLocation in project Insights by InsightsPlugin.

the class Cuboid method toChunkParts.

/**
 * Converts this cuboid into a List of ChunkParts.
 */
public List<ChunkPart> toChunkParts() {
    ChunkVector minV = ChunkVector.from(this.min);
    int minX = this.min.x >> 4;
    int minZ = this.min.z >> 4;
    ChunkVector maxV = ChunkVector.from(this.max);
    int maxX = this.max.x >> 4;
    int maxZ = this.max.z >> 4;
    List<ChunkPart> parts = new ArrayList<>(maxX - minX + 1 + maxZ - minZ + 1);
    for (int x = minX; x <= maxX; x++) {
        int xmin = (x == minX) ? minV.getX() : 0;
        int ymin = minV.getY();
        int xmax = (x == maxX) ? maxV.getX() : 15;
        for (int z = minZ; z <= maxZ; z++) {
            int zmin = (z == minZ) ? minV.getZ() : 0;
            int ymax = maxV.getY();
            int zmax = (z == maxZ) ? maxV.getZ() : 15;
            ChunkLocation loc = new ChunkLocation(world, x, z);
            ChunkVector vmin = new ChunkVector(xmin, ymin, zmin);
            ChunkVector vmax = new ChunkVector(xmax, ymax, zmax);
            parts.add(new ChunkPart(loc, new ChunkCuboid(vmin, vmax)));
        }
    }
    return parts;
}
Also used : ChunkCuboid(dev.frankheijden.insights.api.objects.chunk.ChunkCuboid) ChunkVector(dev.frankheijden.insights.api.objects.chunk.ChunkVector) ArrayList(java.util.ArrayList) ChunkPart(dev.frankheijden.insights.api.objects.chunk.ChunkPart) ChunkLocation(dev.frankheijden.insights.api.objects.chunk.ChunkLocation)

Example 3 with ChunkLocation

use of dev.frankheijden.insights.api.objects.chunk.ChunkLocation in project Insights by InsightsPlugin.

the class LazyChunkPartRadiusIteratorTest method determineChunkParts.

@ParameterizedTest(name = "Chunk[{0}, {1}], Radius = {2}")
@MethodSource("radiusGenerator")
void determineChunkParts(int chunkX, int chunkZ, int radius) {
    World world = Mockito.mock(World.class);
    int edge = (2 * radius) + 1;
    int chunkCount = edge * edge;
    List<ChunkPart> expectedChunkParts = new ArrayList<>(chunkCount);
    for (int x = chunkX - radius; x <= chunkX + radius; x++) {
        for (int z = chunkZ - radius; z <= chunkZ + radius; z++) {
            expectedChunkParts.add(new ChunkLocation(world, x, z).toPart());
        }
    }
    List<ChunkPart> actualChunkParts = new ArrayList<>(chunkCount);
    LazyChunkPartRadiusIterator it = new LazyChunkPartRadiusIterator(world, chunkX, chunkZ, radius);
    while (it.hasNext()) {
        actualChunkParts.add(it.next());
        if (actualChunkParts.size() > expectedChunkParts.size()) {
            fail("Expected ChunkPart count exceeded of " + expectedChunkParts.size());
        }
    }
    assertThat(actualChunkParts).containsExactlyInAnyOrderElementsOf(expectedChunkParts);
}
Also used : ArrayList(java.util.ArrayList) ChunkPart(dev.frankheijden.insights.api.objects.chunk.ChunkPart) World(org.bukkit.World) ChunkLocation(dev.frankheijden.insights.api.objects.chunk.ChunkLocation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ChunkLocation (dev.frankheijden.insights.api.objects.chunk.ChunkLocation)3 ChunkPart (dev.frankheijden.insights.api.objects.chunk.ChunkPart)2 ArrayList (java.util.ArrayList)2 ChunkCuboid (dev.frankheijden.insights.api.objects.chunk.ChunkCuboid)1 ChunkVector (dev.frankheijden.insights.api.objects.chunk.ChunkVector)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 World (org.bukkit.World)1 Player (org.bukkit.entity.Player)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1