PlotSquared/Core/src/main/java/com/plotsquared/core/util/ChunkManager.java

205 lines
8.1 KiB
Java
Raw Normal View History

/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2020-04-15 21:26:54 +02:00
package com.plotsquared.core.util;
2015-02-23 06:29:45 +01:00
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.queue.GlobalBlockQueue;
import com.plotsquared.core.queue.LocalBlockQueue;
import com.plotsquared.core.queue.ScopedLocalBlockQueue;
2020-04-30 12:01:52 +02:00
import com.plotsquared.core.util.task.RunnableVal;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.util.task.TaskManager;
2019-11-04 18:44:23 +01:00
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.regions.CuboidRegion;
2018-08-10 17:01:10 +02:00
2019-05-17 21:32:05 +02:00
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
2016-03-23 02:41:37 +01:00
2015-09-13 06:04:31 +02:00
public abstract class ChunkManager {
2018-08-10 17:01:10 +02:00
2019-11-04 18:44:23 +01:00
private static final Map<BlockVector2, RunnableVal<ScopedLocalBlockQueue>> forceChunks =
2019-04-24 00:48:22 +02:00
new ConcurrentHashMap<>();
2019-11-04 18:44:23 +01:00
private static final Map<BlockVector2, RunnableVal<ScopedLocalBlockQueue>> addChunks =
2019-04-24 00:48:22 +02:00
new ConcurrentHashMap<>();
2015-02-23 06:29:45 +01:00
public static ChunkManager manager = null;
2018-08-10 17:01:10 +02:00
public static void setChunkInPlotArea(RunnableVal<ScopedLocalBlockQueue> force,
2019-11-04 18:44:23 +01:00
RunnableVal<ScopedLocalBlockQueue> add, String world, BlockVector2 loc) {
2016-06-13 06:47:50 +02:00
LocalBlockQueue queue = GlobalBlockQueue.IMP.getNewQueue(world, false);
2020-07-08 15:09:25 +02:00
if (PlotSquared.get().getPlotAreaManager().isAugmented(world) && PlotSquared.get().isNonStandardGeneration(world, loc)) {
2019-11-04 18:44:23 +01:00
int blockX = loc.getX() << 4;
int blockZ = loc.getZ() << 4;
2018-08-10 17:01:10 +02:00
ScopedLocalBlockQueue scoped =
new ScopedLocalBlockQueue(queue, Location.at(world, blockX, 0, blockZ),
Location.at(world, blockX + 15, 255, blockZ + 15));
2016-02-10 19:59:51 +01:00
if (force != null) {
2016-06-13 06:47:50 +02:00
force.run(scoped);
} else {
2019-11-04 18:44:23 +01:00
scoped.regenChunk(loc.getX(), loc.getZ());
2016-06-13 06:47:50 +02:00
if (add != null) {
add.run(scoped);
}
2016-02-10 19:59:51 +01:00
}
2016-06-13 06:47:50 +02:00
queue.flush();
2016-02-10 19:59:51 +01:00
} else {
if (force != null) {
forceChunks.put(loc, force);
}
addChunks.put(loc, add);
2019-11-04 18:44:23 +01:00
queue.regenChunk(loc.getX(), loc.getZ());
forceChunks.remove(loc);
addChunks.remove(loc);
2016-02-10 19:59:51 +01:00
}
}
2018-08-10 17:01:10 +02:00
2019-11-04 18:44:23 +01:00
public static boolean preProcessChunk(BlockVector2 loc, ScopedLocalBlockQueue queue) {
final RunnableVal<ScopedLocalBlockQueue> forceChunk = forceChunks.get(loc);
if (forceChunk != null) {
forceChunk.run(queue);
forceChunks.remove(loc);
return true;
2016-02-10 19:59:51 +01:00
}
return false;
2016-02-10 19:59:51 +01:00
}
2018-08-10 17:01:10 +02:00
2019-11-04 18:44:23 +01:00
public static boolean postProcessChunk(BlockVector2 loc, ScopedLocalBlockQueue queue) {
final RunnableVal<ScopedLocalBlockQueue> addChunk = forceChunks.get(loc);
if (addChunk != null) {
addChunk.run(queue);
addChunks.remove(loc);
return true;
2016-02-10 19:59:51 +01:00
}
return false;
2016-02-10 19:59:51 +01:00
}
2018-08-10 17:01:10 +02:00
public static void chunkTask(final Plot plot, final RunnableVal<int[]> task,
final Runnable whenDone, final int allocate) {
final ArrayList<CuboidRegion> regions = new ArrayList<>(plot.getRegions());
2016-03-29 19:34:48 +02:00
Runnable smallTask = new Runnable() {
2018-08-10 17:01:10 +02:00
@Override public void run() {
2016-04-02 07:30:26 +02:00
if (regions.isEmpty()) {
2016-03-29 19:34:48 +02:00
TaskManager.runTask(whenDone);
return;
}
CuboidRegion value = regions.remove(0);
Location pos1 = Location.at(plot.getWorldName(), value.getMinimumPoint().getX(), 0,
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
value.getMinimumPoint().getZ());
Location pos2 = Location.at(plot.getWorldName(), value.getMaximumPoint().getX(), 0,
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
value.getMaximumPoint().getZ());
2016-03-29 19:34:48 +02:00
chunkTask(pos1, pos2, task, this, allocate);
}
};
smallTask.run();
}
/**
* The int[] will be in the form: [chunkX, chunkZ, pos1x, pos1z, pos2x, pos2z, isEdge] and will represent the bottom and top parts of the chunk
2018-08-10 17:01:10 +02:00
*
* @param pos1
* @param pos2
* @param task
* @param whenDone
*/
2018-08-10 17:01:10 +02:00
public static void chunkTask(Location pos1, Location pos2, final RunnableVal<int[]> task,
final Runnable whenDone, final int allocate) {
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;
2019-11-04 18:44:23 +01:00
final ArrayList<BlockVector2> chunks = new ArrayList<>();
2018-08-10 17:01:10 +02:00
2015-09-13 06:04:31 +02:00
for (int x = bcx; x <= tcx; x++) {
for (int z = bcz; z <= tcz; z++) {
2019-11-04 18:44:23 +01:00
chunks.add(BlockVector2.at(x, z));
}
}
2015-09-13 06:04:31 +02:00
TaskManager.runTask(new Runnable() {
2018-08-10 17:01:10 +02:00
@Override public void run() {
2016-03-23 02:41:37 +01:00
long start = System.currentTimeMillis();
while (!chunks.isEmpty() && ((System.currentTimeMillis() - start) < allocate)) {
2019-11-04 18:44:23 +01:00
BlockVector2 chunk = chunks.remove(0);
task.value = new int[7];
2019-11-04 18:44:23 +01:00
task.value[0] = chunk.getX();
task.value[1] = chunk.getZ();
task.value[2] = task.value[0] << 4;
task.value[3] = task.value[1] << 4;
task.value[4] = task.value[2] + 15;
task.value[5] = task.value[3] + 15;
2015-09-13 06:04:31 +02:00
if (task.value[0] == bcx) {
2015-09-11 12:09:22 +02:00
task.value[2] = p1x;
task.value[6] = 1;
}
2015-09-13 06:04:31 +02:00
if (task.value[0] == tcx) {
task.value[4] = p2x;
task.value[6] = 1;
}
2015-09-13 06:04:31 +02:00
if (task.value[1] == bcz) {
task.value[3] = p1z;
task.value[6] = 1;
}
2015-09-13 06:04:31 +02:00
if (task.value[1] == tcz) {
task.value[5] = p2z;
task.value[6] = 1;
}
task.run();
}
if (!chunks.isEmpty()) {
TaskManager.runTaskLater(this, 1);
2015-09-13 06:04:31 +02:00
} else {
TaskManager.runTask(whenDone);
}
}
});
}
2018-08-10 17:01:10 +02:00
2019-11-04 18:44:23 +01:00
public abstract CompletableFuture loadChunk(String world, BlockVector2 loc, boolean force);
2016-03-23 02:41:37 +01:00
2019-11-04 18:44:23 +01:00
public abstract void unloadChunk(String world, BlockVector2 loc, boolean save);
2016-03-23 02:41:37 +01:00
2019-11-04 18:44:23 +01:00
public Plot hasPlot(String world, BlockVector2 chunk) {
int x1 = chunk.getX() << 4;
int z1 = chunk.getZ() << 4;
2016-03-23 02:41:37 +01:00
int x2 = x1 + 15;
int z2 = z1 + 15;
Location bot = Location.at(world, x1, 0, z1);
2016-02-10 19:59:51 +01:00
Plot plot = bot.getOwnedPlotAbs();
if (plot != null) {
2015-10-07 08:33:33 +02:00
return plot;
}
Location top = Location.at(world, x2, 0, z2);
2016-02-10 19:59:51 +01:00
plot = top.getOwnedPlotAbs();
return plot;
2015-10-07 08:33:33 +02:00
}
2018-08-10 17:01:10 +02:00
2015-02-23 06:29:45 +01:00
}