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();
}
}
}
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;
}
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;
}
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() + "]");
}
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);
}
Aggregations