Search in sources :

Example 6 with RunnableVal

use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.

the class Trim method onCommand.

@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
    if (args.length == 0) {
        sendUsage(player);
        return false;
    }
    final String world = args[0];
    if (!this.worldUtil.isWorld(world) || !this.plotAreaManager.hasPlotArea(world)) {
        player.sendMessage(TranslatableCaption.of("errors.not_valid_world"));
        return false;
    }
    if (Trim.TASK) {
        player.sendMessage(TranslatableCaption.of("trim.trim_in_progress"));
        return false;
    }
    Trim.TASK = true;
    final boolean regen = args.length == 2 && Boolean.parseBoolean(args[1]);
    getTrimRegions(world, new RunnableVal2<>() {

        @Override
        public void run(Set<BlockVector2> viable, final Set<BlockVector2> nonViable) {
            Runnable regenTask;
            if (regen) {
                LOGGER.info("Starting regen task");
                LOGGER.info(" - This is a VERY slow command");
                LOGGER.info(" - It will say 'Trim done!' when complete");
                regenTask = new Runnable() {

                    @Override
                    public void run() {
                        if (nonViable.isEmpty()) {
                            Trim.TASK = false;
                            player.sendMessage(TranslatableCaption.of("trim.trim_done"));
                            LOGGER.info("Trim done!");
                            return;
                        }
                        Iterator<BlockVector2> iterator = nonViable.iterator();
                        BlockVector2 mcr = iterator.next();
                        iterator.remove();
                        int cbx = mcr.getX() << 5;
                        int cbz = mcr.getZ() << 5;
                        // get all 1024 chunks
                        HashSet<BlockVector2> chunks = new HashSet<>();
                        for (int x = cbx; x < cbx + 32; x++) {
                            for (int z = cbz; z < cbz + 32; z++) {
                                BlockVector2 loc = BlockVector2.at(x, z);
                                chunks.add(loc);
                            }
                        }
                        int bx = cbx << 4;
                        int bz = cbz << 4;
                        CuboidRegion region = RegionUtil.createRegion(bx, bx + 511, 0, 0, bz, bz + 511);
                        for (Plot plot : PlotQuery.newQuery().inWorld(world)) {
                            Location bot = plot.getBottomAbs();
                            Location top = plot.getExtendedTopAbs();
                            CuboidRegion plotReg = RegionUtil.createRegion(bot.getX(), top.getX(), 0, 0, bot.getZ(), top.getZ());
                            if (!RegionUtil.intersects(region, plotReg)) {
                                continue;
                            }
                            for (int x = plotReg.getMinimumPoint().getX() >> 4; x <= plotReg.getMaximumPoint().getX() >> 4; x++) {
                                for (int z = plotReg.getMinimumPoint().getZ() >> 4; z <= plotReg.getMaximumPoint().getZ() >> 4; z++) {
                                    BlockVector2 loc = BlockVector2.at(x, z);
                                    chunks.remove(loc);
                                }
                            }
                        }
                        final QueueCoordinator queue = blockQueue.getNewQueue(worldUtil.getWeWorld(world));
                        TaskManager.getPlatformImplementation().objectTask(chunks, new RunnableVal<>() {

                            @Override
                            public void run(BlockVector2 value) {
                                queue.regenChunk(value.getX(), value.getZ());
                            }
                        }).thenAccept(ignore -> TaskManager.getPlatformImplementation().taskLater(this, TaskTime.ticks(1L)));
                    }
                };
            } else {
                regenTask = () -> {
                    Trim.TASK = false;
                    player.sendMessage(TranslatableCaption.of("trim.trim_done"));
                    LOGGER.info("Trim done!");
                };
            }
            regionManager.deleteRegionFiles(world, viable, regenTask);
        }
    });
    return true;
}
Also used : Plot(com.plotsquared.core.plot.Plot) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BlockVector2(com.sk89q.worldedit.math.BlockVector2) RunnableVal(com.plotsquared.core.util.task.RunnableVal) QueueCoordinator(com.plotsquared.core.queue.QueueCoordinator) HashSet(java.util.HashSet) Location(com.plotsquared.core.location.Location)

Example 7 with RunnableVal

use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.

the class BukkitRegionManager method regenerateRegion.

@Override
public boolean regenerateRegion(@NonNull final Location pos1, @NonNull final Location pos2, final boolean ignoreAugment, @Nullable final Runnable whenDone) {
    final BukkitWorld world = (BukkitWorld) worldUtil.getWeWorld(pos1.getWorldName());
    final int p1x = pos1.getX();
    final int p1z = pos1.getZ();
    final int p2x = pos2.getX();
    final int p2z = pos2.getZ();
    final int bcx = p1x >> 4;
    final int bcz = p1z >> 4;
    final int tcx = p2x >> 4;
    final int tcz = p2z >> 4;
    final QueueCoordinator queue = blockQueue.getNewQueue(world);
    final QueueCoordinator regenQueue = blockQueue.getNewQueue(world);
    queue.addReadChunks(new CuboidRegion(pos1.getBlockVector3(), pos2.getBlockVector3()).getChunks());
    queue.setChunkConsumer(chunk -> {
        int x = chunk.getX();
        int z = chunk.getZ();
        int xxb = x << 4;
        int zzb = z << 4;
        int xxt = xxb + 15;
        int zzt = zzb + 15;
        if (xxb >= p1x && xxt <= p2x && zzb >= p1z && zzt <= p2z) {
            AugmentedUtils.bypass(ignoreAugment, () -> regenQueue.regenChunk(chunk.getX(), chunk.getZ()));
            return;
        }
        boolean checkX1 = false;
        int xxb2;
        if (x == bcx) {
            xxb2 = p1x - 1;
            checkX1 = true;
        } else {
            xxb2 = xxb;
        }
        boolean checkX2 = false;
        int xxt2;
        if (x == tcx) {
            xxt2 = p2x + 1;
            checkX2 = true;
        } else {
            xxt2 = xxt;
        }
        boolean checkZ1 = false;
        int zzb2;
        if (z == bcz) {
            zzb2 = p1z - 1;
            checkZ1 = true;
        } else {
            zzb2 = zzb;
        }
        boolean checkZ2 = false;
        int zzt2;
        if (z == tcz) {
            zzt2 = p2z + 1;
            checkZ2 = true;
        } else {
            zzt2 = zzt;
        }
        final ContentMap map = new ContentMap();
        if (checkX1) {
            // 
            map.saveRegion(world, xxb, xxb2, zzb2, zzt2);
        }
        if (checkX2) {
            // 
            map.saveRegion(world, xxt2, xxt, zzb2, zzt2);
        }
        if (checkZ1) {
            // 
            map.saveRegion(world, xxb2, xxt2, zzb, zzb2);
        }
        if (checkZ2) {
            // 
            map.saveRegion(world, xxb2, xxt2, zzt2, zzt);
        }
        if (checkX1 && checkZ1) {
            // 
            map.saveRegion(world, xxb, xxb2, zzb, zzb2);
        }
        if (checkX2 && checkZ1) {
            // ?
            map.saveRegion(world, xxt2, xxt, zzb, zzb2);
        }
        if (checkX1 && checkZ2) {
            // ?
            map.saveRegion(world, xxb, xxb2, zzt2, zzt);
        }
        if (checkX2 && checkZ2) {
            // 
            map.saveRegion(world, xxt2, xxt, zzt2, zzt);
        }
        CuboidRegion currentPlotClear = new CuboidRegion(pos1.getBlockVector3(), pos2.getBlockVector3());
        map.saveEntitiesOut(Bukkit.getWorld(world.getName()).getChunkAt(x, z), currentPlotClear);
        AugmentedUtils.bypass(ignoreAugment, () -> ChunkManager.setChunkInPlotArea(null, new RunnableVal<ScopedQueueCoordinator>() {

            @Override
            public void run(ScopedQueueCoordinator value) {
                Location min = value.getMin();
                int bx = min.getX();
                int bz = min.getZ();
                for (int x1 = 0; x1 < 16; x1++) {
                    for (int z1 = 0; z1 < 16; z1++) {
                        PlotLoc plotLoc = new PlotLoc(bx + x1, bz + z1);
                        BaseBlock[] ids = map.allBlocks.get(plotLoc);
                        if (ids != null) {
                            int minY = value.getMin().getY();
                            for (int yIndex = 0; yIndex < ids.length; yIndex++) {
                                int y = yIndex + minY;
                                BaseBlock id = ids[yIndex];
                                if (id != null) {
                                    value.setBlock(x1, y, z1, id);
                                } else {
                                    value.setBlock(x1, y, z1, BlockTypes.AIR.getDefaultState());
                                }
                            }
                        }
                    }
                }
            }
        }, world.getName(), chunk));
        // map.restoreBlocks(worldObj, 0, 0);
        map.restoreEntities(Bukkit.getWorld(world.getName()));
    });
    regenQueue.setCompleteTask(whenDone);
    queue.setCompleteTask(regenQueue::enqueue);
    queue.enqueue();
    return true;
}
Also used : BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) ScopedQueueCoordinator(com.plotsquared.core.queue.ScopedQueueCoordinator) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) PlotLoc(com.plotsquared.core.location.PlotLoc) RunnableVal(com.plotsquared.core.util.task.RunnableVal) ScopedQueueCoordinator(com.plotsquared.core.queue.ScopedQueueCoordinator) QueueCoordinator(com.plotsquared.core.queue.QueueCoordinator) Location(com.plotsquared.core.location.Location)

Example 8 with RunnableVal

use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.

the class Trim method getTrimRegions.

/**
 * Runs the result task with the parameters (viable, nonViable).
 *
 * @param world  The world
 * @param result (viable = .mcr to trim, nonViable = .mcr keep)
 * @return success or not
 */
public static boolean getTrimRegions(String world, final RunnableVal2<Set<BlockVector2>, Set<BlockVector2>> result) {
    if (result == null) {
        return false;
    }
    TranslatableCaption.of("trim.trim_starting");
    final List<Plot> plots = PlotQuery.newQuery().inWorld(world).asList();
    if (ExpireManager.IMP != null) {
        plots.removeAll(ExpireManager.IMP.getPendingExpired());
    }
    result.value1 = new HashSet<>(PlotSquared.platform().worldUtil().getChunkChunks(world));
    result.value2 = new HashSet<>();
    StaticCaption.of(" - MCA #: " + result.value1.size());
    StaticCaption.of(" - CHUNKS: " + (result.value1.size() * 1024) + " (max)");
    StaticCaption.of(" - TIME ESTIMATE: 12 Parsecs");
    TaskManager.getPlatformImplementation().objectTask(plots, new RunnableVal<>() {

        @Override
        public void run(Plot plot) {
            Location pos1 = plot.getCorners()[0];
            Location pos2 = plot.getCorners()[1];
            int ccx1 = pos1.getX() >> 9;
            int ccz1 = pos1.getZ() >> 9;
            int ccx2 = pos2.getX() >> 9;
            int ccz2 = pos2.getZ() >> 9;
            for (int x = ccx1; x <= ccx2; x++) {
                for (int z = ccz1; z <= ccz2; z++) {
                    BlockVector2 loc = BlockVector2.at(x, z);
                    if (result.value1.remove(loc)) {
                        result.value2.add(loc);
                    }
                }
            }
        }
    }).thenAccept(ignore -> TaskManager.getPlatformImplementation().taskLater(result, TaskTime.ticks(1L)));
    return true;
}
Also used : Plot(com.plotsquared.core.plot.Plot) RunnableVal(com.plotsquared.core.util.task.RunnableVal) BlockVector2(com.sk89q.worldedit.math.BlockVector2) Location(com.plotsquared.core.location.Location)

Example 9 with RunnableVal

use of com.plotsquared.core.util.task.RunnableVal in project PlotSquared by IntellectualSites.

the class Plot method claim.

/**
 * Claim the plot
 *
 * @param player    The player to set the owner to
 * @param teleport  If the player should be teleported
 * @param schematic The schematic name to paste on the plot
 * @param updateDB  If the database should be updated
 * @param auto      If the plot is being claimed by a /plot auto
 * @return success
 * @since 6.1.0
 */
public boolean claim(@NonNull final PlotPlayer<?> player, boolean teleport, String schematic, boolean updateDB, boolean auto) {
    this.eventDispatcher.callPlotClaimedNotify(this, auto);
    if (updateDB) {
        if (!this.getPlotModificationManager().create(player.getUUID(), true)) {
            LOGGER.error("Player {} attempted to claim plot {}, but the database failed to update", player.getName(), this.getId().toCommaSeparatedString());
            return false;
        }
    } else {
        area.addPlot(this);
        updateWorldBorder();
    }
    player.sendMessage(TranslatableCaption.of("working.claimed"), Template.of("plot", this.getId().toString()));
    if (teleport) {
        if (!auto && Settings.Teleport.ON_CLAIM) {
            teleportPlayer(player, TeleportCause.COMMAND_CLAIM, result -> {
            });
        } else if (auto && Settings.Teleport.ON_AUTO) {
            teleportPlayer(player, TeleportCause.COMMAND_AUTO, result -> {
            });
        }
    }
    PlotArea plotworld = getArea();
    if (plotworld.isSchematicOnClaim()) {
        Schematic sch;
        try {
            if (schematic == null || schematic.isEmpty()) {
                sch = schematicHandler.getSchematic(plotworld.getSchematicFile());
            } else {
                sch = schematicHandler.getSchematic(schematic);
                if (sch == null) {
                    sch = schematicHandler.getSchematic(plotworld.getSchematicFile());
                }
            }
        } catch (SchematicHandler.UnsupportedFormatException e) {
            e.printStackTrace();
            return true;
        }
        schematicHandler.paste(sch, this, 0, getArea().getMinBuildHeight(), 0, Settings.Schematics.PASTE_ON_TOP, player, new RunnableVal<>() {

            @Override
            public void run(Boolean value) {
                if (value) {
                    player.sendMessage(TranslatableCaption.of("schematics.schematic_paste_success"));
                } else {
                    player.sendMessage(TranslatableCaption.of("schematics.schematic_paste_failed"));
                }
            }
        });
    }
    plotworld.getPlotManager().claimPlot(this, null);
    this.getPlotModificationManager().setSign(player.getName());
    return true;
}
Also used : TeleportCause(com.plotsquared.core.events.TeleportCause) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Inject(com.google.inject.Inject) Like(com.plotsquared.core.command.Like) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Permission(com.plotsquared.core.permissions.Permission) StaticCaption(com.plotsquared.core.configuration.caption.StaticCaption) BlockLoc(com.plotsquared.core.location.BlockLoc) Schematic(com.plotsquared.core.plot.schematic.Schematic) Map(java.util.Map) PlotListener(com.plotsquared.core.listener.PlotListener) FlagContainer(com.plotsquared.core.plot.flag.FlagContainer) DescriptionFlag(com.plotsquared.core.plot.flag.implementations.DescriptionFlag) Template(net.kyori.adventure.text.minimessage.Template) EventDispatcher(com.plotsquared.core.util.EventDispatcher) Caption(com.plotsquared.core.configuration.caption.Caption) DBFunc(com.plotsquared.core.database.DBFunc) RegionManager(com.plotsquared.core.util.RegionManager) WorldUtil(com.plotsquared.core.util.WorldUtil) TextComponent(net.kyori.adventure.text.TextComponent) ImmutableSet(com.google.common.collect.ImmutableSet) RunnableVal(com.plotsquared.core.util.task.RunnableVal) TimeZone(java.util.TimeZone) ServerPlotFlag(com.plotsquared.core.plot.flag.implementations.ServerPlotFlag) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MiniMessage(net.kyori.adventure.text.minimessage.MiniMessage) Set(java.util.Set) UUID(java.util.UUID) CAP_MONSTER(com.plotsquared.core.util.entity.EntityCategories.CAP_MONSTER) Sets(com.google.common.collect.Sets) Result(com.plotsquared.core.events.Result) Objects(java.util.Objects) List(java.util.List) Logger(org.apache.logging.log4j.Logger) CAP_MOB(com.plotsquared.core.util.entity.EntityCategories.CAP_MOB) TranslatableCaption(com.plotsquared.core.configuration.caption.TranslatableCaption) Entry(java.util.Map.Entry) InternalFlag(com.plotsquared.core.plot.flag.InternalFlag) TaskTime(com.plotsquared.core.util.task.TaskTime) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) CAP_VEHICLE(com.plotsquared.core.util.entity.EntityCategories.CAP_VEHICLE) NonNull(org.checkerframework.checker.nullness.qual.NonNull) CAP_ENTITY(com.plotsquared.core.util.entity.EntityCategories.CAP_ENTITY) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CaptionUtility(com.plotsquared.core.configuration.caption.CaptionUtility) ClassicPlotWorld(com.plotsquared.core.generator.ClassicPlotWorld) PlotFlag(com.plotsquared.core.plot.flag.PlotFlag) Direction(com.plotsquared.core.location.Direction) PlotQuery(com.plotsquared.core.util.query.PlotQuery) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) SinglePlotArea(com.plotsquared.core.plot.world.SinglePlotArea) SchematicHandler(com.plotsquared.core.util.SchematicHandler) Component(net.kyori.adventure.text.Component) ConsolePlayer(com.plotsquared.core.player.ConsolePlayer) ExpireManager(com.plotsquared.core.plot.expiration.ExpireManager) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Location(com.plotsquared.core.location.Location) GlobalFlagContainer(com.plotsquared.core.plot.flag.GlobalFlagContainer) RegionUtil(com.plotsquared.core.util.RegionUtil) KeepFlag(com.plotsquared.core.plot.flag.implementations.KeepFlag) Permissions(com.plotsquared.core.util.Permissions) MathMan(com.plotsquared.core.util.MathMan) TaskManager(com.plotsquared.core.util.task.TaskManager) Cleaner(java.lang.ref.Cleaner) DecimalFormat(java.text.DecimalFormat) DoubleFlag(com.plotsquared.core.plot.flag.types.DoubleFlag) TimeUtil(com.plotsquared.core.util.TimeUtil) CAP_MISC(com.plotsquared.core.util.entity.EntityCategories.CAP_MISC) Consumer(java.util.function.Consumer) PlotAnalysis(com.plotsquared.core.plot.expiration.PlotAnalysis) QueueCoordinator(com.plotsquared.core.queue.QueueCoordinator) PlotSquared(com.plotsquared.core.PlotSquared) PlayerManager(com.plotsquared.core.util.PlayerManager) PlotPlayer(com.plotsquared.core.player.PlotPlayer) CAP_ANIMAL(com.plotsquared.core.util.entity.EntityCategories.CAP_ANIMAL) Settings(com.plotsquared.core.configuration.Settings) ArrayDeque(java.util.ArrayDeque) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) SchematicHandler(com.plotsquared.core.util.SchematicHandler) SinglePlotArea(com.plotsquared.core.plot.world.SinglePlotArea) Schematic(com.plotsquared.core.plot.schematic.Schematic)

Aggregations

RunnableVal (com.plotsquared.core.util.task.RunnableVal)9 Plot (com.plotsquared.core.plot.Plot)7 Location (com.plotsquared.core.location.Location)6 QueueCoordinator (com.plotsquared.core.queue.QueueCoordinator)3 Inject (com.google.inject.Inject)2 TranslatableCaption (com.plotsquared.core.configuration.caption.TranslatableCaption)2 Permission (com.plotsquared.core.permissions.Permission)2 PlotPlayer (com.plotsquared.core.player.PlotPlayer)2 Permissions (com.plotsquared.core.util.Permissions)2 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)2 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 List (java.util.List)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 PlotSquared (com.plotsquared.core.PlotSquared)1 Like (com.plotsquared.core.command.Like)1 Settings (com.plotsquared.core.configuration.Settings)1