com.sk89q.worldedit.math.BlockVector2 Java Examples

The following examples show how to use com.sk89q.worldedit.math.BlockVector2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Spigot_v1_16_R1.java    From worldedit-adapters with GNU Lesser General Public License v3.0 6 votes vote down vote up
private List<CompletableFuture<IChunkAccess>> submitChunkLoadTasks(Region region, WorldServer serverWorld) {
    ChunkProviderServer chunkManager = serverWorld.getChunkProvider();
    List<CompletableFuture<IChunkAccess>> chunkLoadings = new ArrayList<>();
    // Pre-gen all the chunks
    for (BlockVector2 chunk : region.getChunks()) {
        try {
            //noinspection unchecked
            chunkLoadings.add(
                ((CompletableFuture<Either<IChunkAccess, PlayerChunk.Failure>>)
                    getChunkFutureMethod.invoke(chunkManager, chunk.getX(), chunk.getZ(), ChunkStatus.FEATURES, true))
                        .thenApply(either -> either.left().orElse(null))
            );
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new IllegalStateException("Couldn't load chunk for regen.", e);
        }
    }
    return chunkLoadings;
}
 
Example #2
Source File: WorldEditHandler.java    From uSkyBlock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns all the chunks that are fully contained within the region.
 */
public static Set<BlockVector2> getInnerChunks(Region region) {
    Set<BlockVector2> chunks = new HashSet<>();
    int minX = region.getMinimumPoint().getBlockX();
    int minZ = region.getMinimumPoint().getBlockZ();
    int maxX = region.getMaximumPoint().getBlockX();
    int maxZ = region.getMaximumPoint().getBlockZ();
    int cx = minX & 0xF;
    int cz = minZ & 0xF;
    minX = cx != 0 ? minX - cx + 16 : minX;
    minZ = cz != 0 ? minZ - cz + 16 : minZ;
    cx = maxX & 0xF;
    cz = maxZ & 0xF;
    maxX = cx != 15 ? maxX - cx : maxX;
    maxZ = cz != 15 ? maxZ - cz : maxZ;
    for (int x = minX; x < maxX; x += 16) {
        for (int z = minZ; z < maxZ; z += 16) {
            chunks.add(BlockVector2.at(x >> 4, z >> 4));
        }
    }
    return chunks;
}
 
Example #3
Source File: WorldEditHandler.java    From uSkyBlock with GNU General Public License v3.0 6 votes vote down vote up
public static Set<BlockVector2> getOuterChunks(Region region) {
    Set<BlockVector2> chunks = new HashSet<>();
    int minX = region.getMinimumPoint().getBlockX();
    int minZ = region.getMinimumPoint().getBlockZ();
    int maxX = region.getMaximumPoint().getBlockX();
    int maxZ = region.getMaximumPoint().getBlockZ();
    int cx = minX & 0xF;
    int cz = minZ & 0xF;
    minX = minX - cx;
    minZ = minZ - cz;
    cx = maxX & 0xF;
    cz = maxZ & 0xF;
    maxX = cx != 15 ? maxX - cx + 16 : maxX;
    maxZ = cz != 15 ? maxZ - cz + 16 : maxZ;
    for (int x = minX; x < maxX; x += 16) {
        for (int z = minZ; z < maxZ; z += 16) {
            chunks.add(BlockVector2.at(x >> 4, z >> 4));
        }
    }
    return chunks;
}
 
Example #4
Source File: WorldEditHandler.java    From uSkyBlock with GNU General Public License v3.0 6 votes vote down vote up
public static Set<BlockVector2> getChunks(Region region) {
    Set<BlockVector2> chunks = new HashSet<>();
    int minX = region.getMinimumPoint().getBlockX();
    int minZ = region.getMinimumPoint().getBlockZ();
    int maxX = region.getMaximumPoint().getBlockX();
    int maxZ = region.getMaximumPoint().getBlockZ();
    int cx = minX & 0xF;
    int cz = minZ & 0xF;
    minX = (minX - cx) >> 4;
    minZ = (minZ - cz) >> 4;
    maxX = (maxX - (maxX & 0xF) + 16) >> 4;
    maxZ = (maxZ - (maxZ & 0xF) + 16) >> 4;
    for (int x = minX; x <= maxX; x++) {
        for (int z = minZ; z <= maxZ; z++) {
            chunks.add(BlockVector2.at(x, z));
        }
    }
    return chunks;
}
 
Example #5
Source File: RegionCommand.java    From uSkyBlock with GNU General Public License v3.0 6 votes vote down vote up
private void showChunk(Player player, BlockVector2 chunk) {
    World world = player.getWorld();
    int y = player.getLocation().getBlockY();
    List<Location> points = new ArrayList<>();
    int px = chunk.getBlockX() << 4;
    int pz = chunk.getBlockZ() << 4;
    for (int x = 0; x <= 15; x+=dash) {
        points.add(new Location(world, px+x+0.5d, y, pz+0.5d));
    }
    for (int z = 0; z <= 15; z+=dash) {
        points.add(new Location(world, px+15+0.5d, y, pz+z+0.5d));
    }
    for (int x = 15; x >= 0; x-=dash) {
        points.add(new Location(world, px+x+0.5d, y, pz+15+0.5d));
    }
    for (int z = 15; z >= 0; z-=dash) {
        points.add(new Location(world, px+0.5d, y, pz+z+0.5d));
    }
    addAnimation(player, points);
}
 
Example #6
Source File: ChunkSnapShotTask.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected boolean execute() {
    while (!chunks.isEmpty()) {
        BlockVector2 chunkVector = chunks.remove(0);
        Chunk chunk = location.getWorld().getChunkAt(chunkVector.getBlockX(), chunkVector.getBlockZ());
        if (!chunk.isLoaded()) {
            chunk.load();
        }
        snapshots.add(chunk.getChunkSnapshot(false, false, false));
        if (!tick()) {
            break;
        }
    }
    return chunks.isEmpty();
}
 
Example #7
Source File: WorldGuardHandler7_beta_2.java    From AreaShop with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<Vector> getRegionPoints(ProtectedRegion region) {
	List<Vector> result = new ArrayList<>();
	for (BlockVector2 point : region.getPoints()) {
		result.add(new Vector(point.getX(), 0,point.getZ()));
	}
	return result;
}
 
Example #8
Source File: WorldEditHandlerTest.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * <pre>
 *     +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
 *     |     |     |     |     |     |     |     |     |     |     |
 *     |     |     |     |     |     |     |     |     |     |     |
 *     |     |     |x=======================x B  |     |     |     |
 *     +-----+-----+|----+-----+-----+-----+|----+-----+-----+-----+
 *     |     |     ||    |     |     |     ||    |     |     |     |
 *     |     |     ||    |     |     |     ||    |     |     |     |
 *     +-----+-----+|----+-----+-----+-----+|----+-----+-----+-----+
 *     |     |     ||    |     |     |     ||    |     |     |     |
 *     |     |     ||    |     | 0,0 |     ||    |     |     |     |
 *     +-----+-----+|----+-----0-----+-----+|----+-----+-----+-----+
 *     |     |     ||    |     |     |     ||    |     |     |     |
 *     |     |     ||    |     |     |     ||    |     |     |     |
 *     +-----+-----+|----+-----+-----+-----+|----+-----+-----+-----+
 *     |     |     || A  |     |     |     ||    |     |     |     |
 *     |     |     |x=======================x    |     |     |     |
 *     +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
 *     A = -31, -31
 *     B =  32,  32
 * </pre>
 * @throws Exception
 */
@Test
public void testGetBorderRegionsUnaligned4Quadrants() throws Exception {
    Region region = new CuboidRegion(BlockVector3.at(-31,0,-31), BlockVector3.at(32, 15, 32));
    Set<Region> borderRegions = WorldEditHandler.getBorderRegions(region);
    Set<Region> expectedBorder = new HashSet<>(Arrays.<Region>asList(
            new CuboidRegion(BlockVector3.at(-16,0,32), BlockVector3.at(31,15,32)),
            new CuboidRegion(BlockVector3.at(-16,0,-31), BlockVector3.at(31,15,-17)),
            new CuboidRegion(BlockVector3.at(-31,0,-31), BlockVector3.at(-17,15,32)),
            new CuboidRegion(BlockVector3.at(32,0,-31), BlockVector3.at(32,15,32))
            ));
    Set<BlockVector2> expectedInner = new HashSet<>();
    for (int x = -1; x <= 1; x++) {
        for (int z = -1; z <= 1; z++) {
            expectedInner.add(BlockVector2.at(x, z));
        }
    }
    Set<BlockVector2> expectedOuter = new HashSet<>();
    for (int x = -2; x <= 2; x++) {
        for (int z = -2; z <= 2; z++) {
            expectedOuter.add(BlockVector2.at(x, z));
        }
    }

    verifySame(borderRegions, expectedBorder);
    assertThat(WorldEditHandler.getInnerChunks(region), is(expectedInner));
    assertThat(WorldEditHandler.getOuterChunks(region), is(expectedOuter));
}
 
Example #9
Source File: WorldEditHandlerTest.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testGetBorderRegionsAligned4Quadrants() throws Exception {
    Region region = new CuboidRegion(BlockVector3.at(-64,0,-64), BlockVector3.at(63, 15, 63));
    Set<Region> borderRegions = WorldEditHandler.getBorderRegions(region);
    Set<Region> expectedBorder = new HashSet<>(Arrays.<Region>asList());
    Set<BlockVector2> expectedInner = new HashSet<>();
    for (int x = -4; x <= 3; x++) {
        for (int z = -4; z <= 3; z++) {
            expectedInner.add(BlockVector2.at(x, z));
        }
    }
    verifySame(borderRegions, expectedBorder);
    assertThat(WorldEditHandler.getInnerChunks(region), is(expectedInner));
    assertThat(WorldEditHandler.getOuterChunks(region), is(expectedInner));
}
 
Example #10
Source File: WorldEditHandler.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
public static void refreshRegion(Location location) {
    ProtectedRegion region = WorldGuardHandler.getIslandRegionAt(location);
    World world = location.getWorld();
    Region cube = getRegion(world, region);
    for (BlockVector2 chunk : cube.getChunks()) {
        world.refreshChunk(chunk.getBlockX(), chunk.getBlockZ());
    }
}
 
Example #11
Source File: WorldEditHandler.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
public static void unloadRegion(Location location) {
    ProtectedRegion region = WorldGuardHandler.getIslandRegionAt(location);
    World world = location.getWorld();
    Region cube = getRegion(world, region);
    for (BlockVector2 chunk : cube.getChunks()) {
        world.unloadChunk(chunk.getBlockX(), chunk.getBlockZ(), true);
    }
}
 
Example #12
Source File: WorldEditHandler.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
public static void loadRegion(Location location) {
    ProtectedRegion region = WorldGuardHandler.getIslandRegionAt(location);
    World world = location.getWorld();
    Region cube = getRegion(world, region);
    for (BlockVector2 chunk : cube.getChunks()) {
        world.unloadChunk(chunk.getBlockX(), chunk.getBlockZ(), true);
        world.loadChunk(chunk.getBlockX(), chunk.getBlockZ(), false);
    }
}
 
Example #13
Source File: WorldEditHandler.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
public static void clearIsland(@NotNull final World islandWorld, @NotNull final ProtectedRegion region,
                               @Nullable final Runnable afterDeletion) {
    Validate.notNull(islandWorld, "IslandWorld cannot be null");
    Validate.notNull(region, "Region cannot be null");

    log.finer("Clearing island " + region);
    uSkyBlock plugin = uSkyBlock.getInstance();
    final long t = System.currentTimeMillis();
    final Region cube = getRegion(islandWorld, region);
    Runnable onCompletion = () -> {
        long diff = System.currentTimeMillis() - t;
        LogUtil.log(Level.INFO, String.format("Cleared island on %s in %d.%03d seconds",
                islandWorld.getName(), (diff / 1000), (diff % 1000)));
        if (afterDeletion != null) {
            afterDeletion.run();
        }
    };
    Set<BlockVector2> innerChunks;
    Set<Region> borderRegions = new HashSet<>();
    if (isOuterPossible()) {
        if (Settings.island_protectionRange == Settings.island_distance) {
            innerChunks = getInnerChunks(cube);
        } else {
            innerChunks = getOuterChunks(cube);
        }
    } else {
        innerChunks = getInnerChunks(cube);
        borderRegions = getBorderRegions(cube);
    }
    List<Chunk> chunkList = new ArrayList<>();
    for (BlockVector2 vector : innerChunks) {
        chunkList.add(islandWorld.getChunkAt(vector.getBlockX(), vector.getBlockZ()));
    }
    WorldEditClear weClear = new WorldEditClear(plugin, islandWorld, borderRegions, onCompletion);
    plugin.getWorldManager().getChunkRegenerator(islandWorld).regenerateChunks(chunkList, weClear);
}
 
Example #14
Source File: ChunkComparator.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int compare(BlockVector2 o1, BlockVector2 o2) {
    int cmp = (int) Math.round(origin.distanceSq(o1) -  origin.distanceSq(o2));
    if (cmp == 0) {
        cmp = o1.getBlockX() - o2.getBlockX();
    }
    if (cmp == 0) {
        cmp = o1.getBlockZ() - o2.getBlockZ();
    }
    return cmp;
}
 
Example #15
Source File: RadiationCommandHandler.java    From CraftserveRadiation with Apache License 2.0 5 votes vote down vote up
private boolean onSafe(Player sender, String label, String[] args) {
    String usage = ChatColor.RED + "/" + label + " safe <radius>";
    if (args.length == 1) {
        sender.sendMessage(ChatColor.RED + "Provide safe-from-radiation zone radius in the first argument. Radius will be relative to your current position.");
        sender.sendMessage(usage);
        return true;
    }

    int radius;
    try {
        radius = Integer.parseInt(args[1]);
    } catch (NumberFormatException e) {
        sender.sendMessage(ChatColor.RED + "Number was expected, but " + args[1] + " was provided.");
        sender.sendMessage(ChatColor.RED + usage);
        return true;
    }

    if (radius <= 0) {
        sender.sendMessage(ChatColor.RED + "Radius must be positive.");
        sender.sendMessage(ChatColor.RED + usage);
        return true;
    }

    RegionContainer container = this.worldGuardMatcher.getRegionContainer();
    if (container == null) {
        sender.sendMessage(ChatColor.RED + "Sorry, region container is not currently accessible.");
        return true;
    }

    if (this.define(sender, container, REGION_ID, radius)) {
        BlockVector2 origin = BukkitAdapter.asBlockVector(sender.getLocation()).toBlockVector2();
        sender.sendMessage(ChatColor.GREEN + "A new safe-from-radiation zone has been created in radius " +
                radius + " at the origin at " + origin + " in world " + sender.getWorld().getName() + ".");
    }
    return true;
}
 
Example #16
Source File: WorldEditHandlerTest.java    From uSkyBlock with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Tests that single blocks overlapping regions gives out 1-wide borders.
 * <pre>
 *     ^
 *     |           +---+---+---+---+---+---+
 *     |           |   |   |   |   |   |   |
 *     |           |   |   |   |   |   |   |
 *     |           +---+---+---+---+---+---+
 *     |         l |   | D=======C |   |   |
 *     |           |   | I |   | I |   |   |
 *     |         k +---+-I-R---Q-I-+---+---+
 *     |           |   | I |   | I |   |   |
 *     |           |   | I |   | I |   |   |
 *     |         j +---+-I-O---P-I-+---+---+
 *     |           |   | I |   | I |   |   |
 *     |         i |   | A=======B |   |   |
 *     |           +---+---+---+---+---+---+
 *     |                 a b   c d
 *     +----------------------------------------->
 *
 * Points:
 *     A = (a,i)
 *     B = (d,i)
 *     C = (d,l)
 *     D = (a,l)
 *
 *     M(x) = X mod 16, i.e. Mc = C mod 16.
 *
 * Borders:
 *     O = A + 16 - Ma   | A > 0
 *       = A - Ma        | A <= 0
 *
 *     Q = C - Mc - 1    | C > 0 && Mc != 15
 *       = C + Mc - 16   | C < 0 && Mc != -1
 * </pre>
 *
 * <pre>
 *     +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
 *     |     |     |     |     |     |     |     |     |     |     |
 *     |     |     |     |     |     |     |     |     |     |     |
 *     |     |     |     |     |     |     |     |     |     |     |
 *     +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
 *     |     |     |     |     |     |     |     |     |     |     |
 *     |     |     |     |     |     |     |     |     |     |     |
 *     |     |     |x===========x B  |     |     |     |     |     |
 *     +-----+-----+|----+-----+|----+-----+-----+-----+-----+-----+
 *     |     |     ||    |     ||    |     |     |     |     |     |
 *     |     |     ||    |     ||    |     |     |     |     |     |
 *     |     |     ||    |     ||    |     |     |     |     |     |
 *     +-----+-----+|----+-----||----+-----+-----+-----+-----+-----+
 *     |     |     ||    |     ||    |     |     |     |     |     |
 *     |     |     || A  |     ||    |     |     |     |     |     |
 *     |     |     |x===========x    |     |     |     |     |     |
 *     +-----+-----0-----+-----+-----+-----+-----+-----+-----+-----+
 *     |     |     |     |     |     |     |     |     |     |     |
 *     |     |     |     |     |     |     |     |     |     |     |
 *     |     |     |     |     |     |     |     |     |     |     |
 *     +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
 *     A =   1,   1
 *     B =  32,  32
 * </pre>
 * @throws Exception
 */
@Test
public void testGetBorderRegionsUnalignedPos() throws Exception {
    Region region = new CuboidRegion(BlockVector3.at(1,0,1), BlockVector3.at(32, 15, 32));
    Set<Region> borderRegions = WorldEditHandler.getBorderRegions(region);
    Set<Region> expectedBorder = new HashSet<>(Arrays.<Region>asList(
            new CuboidRegion(BlockVector3.at(1,0,1), BlockVector3.at(15,15,32)),
            new CuboidRegion(BlockVector3.at(32,0,1), BlockVector3.at(32,15,32)),
            new CuboidRegion(BlockVector3.at(16,0,1), BlockVector3.at(31,15,15)),
            new CuboidRegion(BlockVector3.at(16,0,32), BlockVector3.at(31,15,32))
    ));
    Set<BlockVector2> expectedInner = new HashSet<>(Arrays.asList(
            BlockVector2.at(1, 1)
    ));
    verifySame(borderRegions, expectedBorder);
    Set<BlockVector2> innerChunks = WorldEditHandler.getInnerChunks(region);
    assertThat(innerChunks, is(expectedInner));
}
 
Example #17
Source File: ChunkComparator.java    From uSkyBlock with GNU General Public License v3.0 4 votes vote down vote up
public ChunkComparator(BlockVector2 origin) {
    this.origin = origin;
}
 
Example #18
Source File: SetBiomeTask.java    From uSkyBlock with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected boolean execute() {
    if (minP == null || maxP == null) {
        return true;
    }
    Iterator<BlockVector2> it = chunks.iterator();
    while (it.hasNext()) {
        BlockVector2 chunk = it.next();
        it.remove();
        world.loadChunk(chunk.getBlockX(), chunk.getBlockZ());
        int cx = chunk.getBlockX() << 4;
        int cz = chunk.getBlockZ() << 4;
        int mx = cx + 15;
        int mz = cz + 15;
        if (cx < minP.getBlockX()) {
            cx = minP.getBlockX();
        }
        if (cz < minP.getBlockZ()) {
            cz = minP.getBlockZ();
        }
        if (mx > maxP.getBlockX()) {
            mx = maxP.getBlockX();
        }
        if (mz > maxP.getBlockZ()) {
            mz = maxP.getBlockZ();
        }
        for (int x = cx; x <= mx; x++) {
            for (int z = cz; z <= mz; z++) {
                for (int y = 0; y < world.getMaxHeight(); y++) {
                    world.setBiome(x, y, z, biome);
                }
            }
        }

        //noinspection deprecation
        world.refreshChunk(chunk.getBlockX(), chunk.getBlockZ());

        if (!tick()) {
            return isDone();
        }
    }
    return isDone();
}
 
Example #19
Source File: Spigot_v1_13_R2_2.java    From worldedit-adapters with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean regenerate(org.bukkit.World bukkitWorld, Region region, EditSession editSession) {
    WorldServer originalWorld = ((CraftWorld) bukkitWorld).getHandle();

    File saveFolder = Files.createTempDir();
    // register this just in case something goes wrong
    // normally it should be deleted at the end of this method
    saveFolder.deleteOnExit();
    saveFolder.deleteOnExit();
    try {
        Environment env = bukkitWorld.getEnvironment();
        ChunkGenerator gen = bukkitWorld.getGenerator();
        MinecraftServer server = originalWorld.getServer().getServer();

        WorldData newWorldData = new WorldData(originalWorld.worldData.a((NBTTagCompound) null),
                server.dataConverterManager, getDataVersion(), null);
        newWorldData.checkName("worldeditregentempworld");
        WorldNBTStorage saveHandler = new WorldNBTStorage(saveFolder,
                originalWorld.getDataManager().getDirectory().getName(), server, server.dataConverterManager);
        try (WorldServer freshWorld = new WorldServer(server, saveHandler, new PersistentCollection(saveHandler),
                newWorldData, originalWorld.worldProvider.getDimensionManager(),
                originalWorld.methodProfiler, env, gen)) {
            freshWorld.savingDisabled = true;

            // Pre-gen all the chunks
            // We need to also pull one more chunk in every direction
            CuboidRegion expandedPreGen = new CuboidRegion(region.getMinimumPoint().subtract(16, 0, 16),
                    region.getMaximumPoint().add(16, 0, 16));
            for (BlockVector2 chunk : expandedPreGen.getChunks()) {
                freshWorld.getChunkAt(chunk.getBlockX(), chunk.getBlockZ());
            }

            CraftWorld craftWorld = freshWorld.getWorld();
            BukkitWorld from = new BukkitWorld(craftWorld);
            for (BlockVector3 vec : region) {
                editSession.setBlock(vec, from.getFullBlock(vec));
            }
        }
    } catch (MaxChangedBlocksException e) {
        throw new RuntimeException(e);
    } finally {
        saveFolder.delete();
        try {
            Map<String, org.bukkit.World> map = (Map<String, org.bukkit.World>) serverWorldsField.get(Bukkit.getServer());
            map.remove("worldeditregentempworld");
        } catch (IllegalAccessException ignored) {
        }
    }
    return true;
}