Search in sources :

Example 1 with AABBTicket

use of net.minecraftforge.common.ticket.AABBTicket in project MinecraftForge by MinecraftForge.

the class FarmlandWaterManager method addAABBTicket.

/**
 * Convenience method to add a ticket that is backed by an AABB.
 * <br>
 * If you don't want to water the region anymore, call {@link SimpleTicket#invalidate()}. Also call this
 * when the region this is unloaded (e.g. your TE is unloaded or the block is removed), and validate once it is loaded
 * <br>
 * The AABB in the ticket is immutable
 * @param world The world where the region should be marked. Only server-side worlds are allowed
 * @param aabb The region where blocks should be watered
 * @return The ticket for your requested region.
 */
public static AABBTicket addAABBTicket(Level world, AABB aabb) {
    if (DEBUG)
        LOGGER.info("FarmlandWaterManager: New AABBTicket, aabb={}", aabb);
    // First calculate all chunks the aabb is in
    ChunkPos leftUp = new ChunkPos(((int) aabb.minX) >> 4, ((int) aabb.minZ) >> 4);
    ChunkPos rightDown = new ChunkPos(((int) aabb.maxX) >> 4, ((int) aabb.maxZ) >> 4);
    Set<ChunkPos> posSet = new HashSet<>();
    for (int x = leftUp.x; x <= rightDown.x; x++) {
        for (int z = leftUp.z; z <= rightDown.z; z++) {
            posSet.add(new ChunkPos(x, z));
        }
    }
    ChunkPos masterPos = null;
    double masterDistance = Double.MAX_VALUE;
    for (// Find the chunkPos with the lowest distance to the center and choose it as the master pos
    ChunkPos pos : // Find the chunkPos with the lowest distance to the center and choose it as the master pos
    posSet) {
        double distToCenter = getDistanceSq(pos, aabb.getCenter());
        if (distToCenter < masterDistance) {
            if (DEBUG)
                LOGGER.info("FarmlandWaterManager: New better pos then {}: {}, prev dist {}, new dist {}", masterPos, pos, masterDistance, distToCenter);
            masterPos = pos;
            masterDistance = distToCenter;
        }
    }
    posSet.remove(masterPos);
    if (DEBUG)
        LOGGER.info("FarmlandWaterManager: {} center pos, {} dummy posses. Dist to center {}", masterPos, posSet.toArray(new ChunkPos[0]), masterDistance);
    return addCustomTicket(world, new AABBTicket(aabb), masterPos, posSet.toArray(new ChunkPos[0]));
}
Also used : AABBTicket(net.minecraftforge.common.ticket.AABBTicket) ChunkPos(net.minecraft.world.level.ChunkPos) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 ChunkPos (net.minecraft.world.level.ChunkPos)1 AABBTicket (net.minecraftforge.common.ticket.AABBTicket)1