Search in sources :

Example 6 with IntVector2

use of com.bergerkiller.bukkit.common.bases.IntVector2 in project BKCommonLib by bergerhealer.

the class CommonMapController method onChunkEntitiesLoaded.

private void onChunkEntitiesLoaded(Chunk chunk) {
    World world = chunk.getWorld();
    Set<IntVector2> dependingChunks;
    {
        Map<IntVector2, Set<IntVector2>> dependencies = this.itemFrameClusterDependencies.get(world);
        if (dependencies == null || (dependingChunks = dependencies.remove(new IntVector2(chunk))) == null) {
            return;
        }
    }
    boolean wasClustersByWorldCacheEnabled = this.itemFrameClustersByWorldEnabled;
    try {
        this.itemFrameClustersByWorldEnabled = true;
        for (IntVector2 depending : dependingChunks) {
            // Check this depending chunk is still loaded with all entities inside
            // If not, then when it loads the cluster will be revived then
            Chunk dependingChunk = WorldUtil.getChunk(world, depending.x, depending.z);
            if (dependingChunk == null || !WorldUtil.isChunkEntitiesLoaded(dependingChunk)) {
                continue;
            }
            // Quicker than iterating all item frames on the world
            for (Entity entity : ChunkUtil.getEntities(dependingChunk)) {
                if (!(entity instanceof ItemFrame)) {
                    continue;
                }
                // Recalculate UUID, this will re-discover the cluster
                // May also revive other item frames that were part of the same cluster
                // Note that if this chunk being loaded contained item frames part of the cluster,
                // the cluster is already revived. Entity add handling occurs prior.
                ItemFrameInfo frameInfo = this.itemFrames.get(entity.getEntityId());
                if (frameInfo != null) {
                    frameInfo.onChunkDependencyLoaded();
                }
            }
        }
    } finally {
        this.itemFrameClustersByWorldEnabled = wasClustersByWorldCacheEnabled;
        if (!wasClustersByWorldCacheEnabled) {
            itemFrameClustersByWorld.clear();
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) ItemFrame(org.bukkit.entity.ItemFrame) World(org.bukkit.World) OfflineWorld(com.bergerkiller.bukkit.common.offline.OfflineWorld) IntVector2(com.bergerkiller.bukkit.common.bases.IntVector2) Chunk(org.bukkit.Chunk) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) IntHashMap(com.bergerkiller.bukkit.common.wrappers.IntHashMap) HashMap(java.util.HashMap) OutputTypeMap(com.bergerkiller.mountiplex.reflection.util.OutputTypeMap) ItemFrameInfo(com.bergerkiller.bukkit.common.map.binding.ItemFrameInfo)

Example 7 with IntVector2

use of com.bergerkiller.bukkit.common.bases.IntVector2 in project BKCommonLib by bergerhealer.

the class RegionHandlerVanilla method getRegions3ForXZ.

@Override
public Set<IntVector3> getRegions3ForXZ(World world, Set<IntVector2> regionXZCoordinates) {
    // Figure out the minimum/maximum region y coordinate
    // Since Minecraft 1.17 there can be more than one region (32 chunks) vertically
    WorldHandle worldHandle = WorldHandle.fromBukkit(world);
    int minRegionY = worldHandle.getMinBuildHeight() >> 9;
    int maxRegionY = (worldHandle.getMaxBuildHeight() - 1) >> 9;
    Set<IntVector3> result = new HashSet<IntVector3>(regionXZCoordinates.size());
    for (IntVector2 coord : regionXZCoordinates) {
        for (int ry = minRegionY; ry <= maxRegionY; ry++) {
            result.add(coord.toIntVector3(ry));
        }
    }
    return result;
}
Also used : WorldHandle(com.bergerkiller.generated.net.minecraft.world.level.WorldHandle) IntVector2(com.bergerkiller.bukkit.common.bases.IntVector2) IntVector3(com.bergerkiller.bukkit.common.bases.IntVector3) HashSet(java.util.HashSet)

Example 8 with IntVector2

use of com.bergerkiller.bukkit.common.bases.IntVector2 in project BKCommonLib by bergerhealer.

the class RegionHandler method getRegions.

/**
 * Gets all region indices for loadable regions of a world.<br>
 * <br>
 * <b>Deprecated: use {@link #getRegions3(World)} instead
 * to support servers with infinite Y-coordinate generation</b>
 *
 * @param world
 * @return region indices
 */
@Deprecated
public final Set<IntVector2> getRegions(World world) {
    Set<IntVector3> coords_3d = getRegions3(world);
    Set<IntVector2> coords_2d = new HashSet<IntVector2>(coords_3d.size());
    for (IntVector3 coord : coords_3d) {
        coords_2d.add(coord.toIntVector2());
    }
    return coords_2d;
}
Also used : IntVector2(com.bergerkiller.bukkit.common.bases.IntVector2) IntVector3(com.bergerkiller.bukkit.common.bases.IntVector3) HashSet(java.util.HashSet)

Example 9 with IntVector2

use of com.bergerkiller.bukkit.common.bases.IntVector2 in project BKCommonLib by bergerhealer.

the class MCSDWebbingCodec method processBest.

/**
 * Tries out many different orders of drawing the webbing information.
 * The more iterations, the better the results, the slower the process.
 * Using max_iterations higher than 100000 is generally not useful.
 *
 * @param points to start encoding from
 * @param max_iterations maximum number of iterations until giving up
 */
public void processBest(List<IntVector2> points, int max_iterations) {
    // Get all starting coordinates
    List<StartPoint> best_coords = new ArrayList<StartPoint>(points.size());
    for (IntVector2 point : points) {
        if (this.strands[point.x | (point.z << 8)]) {
            StartPoint startPoint = new StartPoint();
            startPoint.x = point.x;
            startPoint.y = point.z;
            startPoint.active = true;
            best_coords.add(startPoint);
        }
    }
    if (best_coords.isEmpty()) {
        // nothing to do
        return;
    }
    // This one is a stored starting point
    MCSDWebbingCodec codec_start = new MCSDWebbingCodec();
    codec_start.reset(this);
    // Try many random iterations
    Random random = new Random();
    MCSDWebbingCodec tempCodec = new MCSDWebbingCodec();
    int start_size = 0;
    int start_count = 0;
    double start_cost = Double.MAX_VALUE;
    int best_size = 0;
    int best_count = 0;
    double best_cost = Double.MAX_VALUE;
    int iterations = 0;
    int index_a, index_b;
    while (++iterations < max_iterations) {
        // This saves us some cycles that have the same outcome
        do {
            index_a = random.nextInt(best_coords.size());
            index_b = random.nextInt(best_coords.size());
        } while (index_a == index_b || (!best_coords.get(index_a).active && !best_coords.get(index_b).active));
        // Perform another encoding run using a temporary codec
        Collections.swap(best_coords, index_a, index_b);
        tempCodec.reset(codec_start);
        for (StartPoint startPoint : best_coords) {
            startPoint.active = tempCodec.writeFrom(startPoint.x, startPoint.y);
        }
        // Check if the cost per pixel has decreased
        // When that happens, continue with the new coordinates
        // When that does not happen, undo the swap and try again
        double cost = tempCodec.calculateCost();
        if (cost < best_cost) {
            this.reset(tempCodec);
            iterations = 0;
            best_cost = cost;
            best_count = tempCodec.calculateWritten();
            best_size = tempCodec.calculateSize();
            if (start_cost == Double.MAX_VALUE) {
                start_cost = best_cost;
                start_count = best_count;
                start_size = best_size;
            }
        } else {
            Collections.swap(best_coords, index_a, index_b);
        }
    }
    Logging.LOGGER_MAPDISPLAY.info("[" + MathUtil.round(start_cost, 3) + " c/p, " + start_count + " pixels, " + start_size + " bits] => " + "[" + MathUtil.round(best_cost, 3) + " c/p, " + best_count + " pixels, " + best_size + " bits] " + "[compressed=" + this.calculateCompressedSize() + "]");
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) IntVector2(com.bergerkiller.bukkit.common.bases.IntVector2)

Example 10 with IntVector2

use of com.bergerkiller.bukkit.common.bases.IntVector2 in project BKCommonLib by bergerhealer.

the class MCSDBubbleFormat method spread.

private void spread(Bubble cell) {
    boolean hasChanges;
    do {
        hasChanges = false;
        for (int i = 0; i < cell.pixels.size(); i++) {
            IntVector2 pixel = cell.pixels.get(i);
            for (int dx = -1; dx <= 1; dx += 2) {
                for (int dy = -1; dy <= 1; dy += 2) {
                    int x = pixel.x + dx;
                    int y = pixel.z + dy;
                    // Bounds check
                    if (x < 0 || y < 0 || x >= 256 || y >= 256) {
                        continue;
                    }
                    // Only spread when it is the same color, and the pixel is not sitting on a cell boundary
                    if (get(x, y, cell.z_min) != cell.color || getStrand(x, y, cell.z_min)) {
                        continue;
                    }
                    set(x, y, cell.z_min, (byte) 0);
                    cell.pixels.add(new IntVector2(x, y));
                    hasChanges = true;
                }
            }
        }
    } while (hasChanges);
}
Also used : IntVector2(com.bergerkiller.bukkit.common.bases.IntVector2)

Aggregations

IntVector2 (com.bergerkiller.bukkit.common.bases.IntVector2)13 IntVector3 (com.bergerkiller.bukkit.common.bases.IntVector3)6 HashSet (java.util.HashSet)6 Chunk (org.bukkit.Chunk)5 ArrayList (java.util.ArrayList)4 WorldHandle (com.bergerkiller.generated.net.minecraft.world.level.WorldHandle)2 File (java.io.File)2 BitSet (java.util.BitSet)2 Set (java.util.Set)2 BitOutputStream (com.bergerkiller.bukkit.common.io.BitOutputStream)1 BitPacket (com.bergerkiller.bukkit.common.io.BitPacket)1 ItemFrameInfo (com.bergerkiller.bukkit.common.map.binding.ItemFrameInfo)1 OfflineWorld (com.bergerkiller.bukkit.common.offline.OfflineWorld)1 IntHashMap (com.bergerkiller.bukkit.common.wrappers.IntHashMap)1 OutputTypeMap (com.bergerkiller.mountiplex.reflection.util.OutputTypeMap)1 Color (java.awt.Color)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1