PlotSquared/Core/src/main/java/com/plotsquared/core/queue/QueueCoordinator.java

337 lines
11 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
2020-08-15 14:59:29 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-04-15 21:26:54 +02:00
package com.plotsquared.core.queue;
2016-06-13 06:47:50 +02:00
2020-07-12 12:25:44 +02:00
import com.google.inject.Inject;
2020-05-19 23:05:36 +02:00
import com.plotsquared.core.PlotSquared;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.location.Location;
2020-04-30 12:01:52 +02:00
import com.plotsquared.core.util.PatternUtil;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock;
2019-11-04 20:58:24 +01:00
import com.sk89q.worldedit.world.block.BlockState;
2020-07-14 18:49:40 +02:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
2018-08-10 17:01:10 +02:00
public abstract class QueueCoordinator {
2016-06-13 06:47:50 +02:00
private boolean forceSync = false;
@Nullable private Object chunkObject;
2020-07-12 12:25:44 +02:00
@Inject private GlobalBlockQueue blockQueue;
2020-07-10 22:12:37 +02:00
/**
* Default constructor requires world to indicate any extents given to {@link QueueCoordinator} also need this constructor.
*/
public QueueCoordinator(@Nullable World world) {
2020-07-10 22:12:37 +02:00
PlotSquared.platform().getInjector().injectMembers(this);
2016-06-13 06:47:50 +02:00
}
2020-07-24 18:00:08 +02:00
/**
* Get a {@link ScopedQueueCoordinator} limited to the chunk at the specific chunk Coordinates
*/
public ScopedQueueCoordinator getForChunk(int x, int z) {
int bx = x << 4;
int bz = z << 4;
return new ScopedQueueCoordinator(this, Location.at(getWorld().getName(), bx, 0, bz),
Location.at(getWorld().getName(), bx + 15, 255, bz + 255));
}
2020-07-24 18:00:08 +02:00
/**
* Get the size of the queue in chunks
*/
2016-06-13 06:47:50 +02:00
public abstract int size();
2020-07-24 18:00:08 +02:00
/**
* Set when the queue was last modified
*/
2018-08-10 17:01:10 +02:00
public abstract void setModified(long modified);
2020-07-24 18:00:08 +02:00
/**
* Returns true if the queue should be forced to be synchronous when enqueued.
*/
public boolean isForceSync() {
return forceSync;
}
2020-07-24 18:00:08 +02:00
/**
* Set whether the queue should be forced to be synchronous
*/
public void setForceSync(boolean forceSync) {
this.forceSync = forceSync;
}
2020-07-24 18:00:08 +02:00
/**
* Get the Chunk Object set to the queue
*/
@Nullable public Object getChunkObject() {
return chunkObject;
}
2020-07-24 18:00:08 +02:00
/**
* Set a chunk object (e.g. the Bukkit Chunk object) to the queue
*/
2020-07-24 17:24:53 +02:00
public void setChunkObject(@Nonnull Object chunkObject) {
this.chunkObject = chunkObject;
}
2019-04-29 21:19:49 +02:00
/**
* Sets the block at the coordinates provided to the given id.
*
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
* @param x the x coordinate from from 0 to 15 inclusive
* @param y the y coordinate from from 0 (inclusive) - maxHeight(exclusive)
* @param z the z coordinate from 0 to 15 inclusive
2020-07-24 18:00:08 +02:00
* @param id the BlockState to set the block to
2019-04-29 21:19:49 +02:00
*/
2020-07-24 17:24:53 +02:00
public abstract boolean setBlock(final int x, final int y, final int z, @Nonnull final BlockState id);
2018-12-17 20:57:21 +01:00
2020-07-24 18:00:08 +02:00
/**
* Sets the block at the coordinates provided to the given id.
*
* @param x the x coordinate from from 0 to 15 inclusive
* @param y the y coordinate from from 0 (inclusive) - maxHeight(exclusive)
* @param z the z coordinate from 0 to 15 inclusive
* @param id the BaseBlock to set the block to
*/
2020-07-24 17:24:53 +02:00
public abstract boolean setBlock(final int x, final int y, final int z, @Nonnull final BaseBlock id);
2020-07-24 18:00:08 +02:00
/**
* Sets the block at the coordinates provided to the given id.
*
* @param x the x coordinate from from 0 to 15 inclusive
* @param y the y coordinate from from 0 (inclusive) - maxHeight(exclusive)
* @param z the z coordinate from 0 to 15 inclusive
* @param pattern the pattern to set the block to
*/
2020-07-14 18:49:40 +02:00
public boolean setBlock(final int x, final int y, final int z, @Nonnull final Pattern pattern) {
return setBlock(x, y, z, PatternUtil.apply(pattern, x, y, z));
}
2020-07-24 18:00:08 +02:00
/**
* Sets a tile entity at the coordinates provided to the given CompoundTag
*
* @param x the x coordinate from from 0 to 15 inclusive
* @param y the y coordinate from from 0 (inclusive) - maxHeight(exclusive)
* @param z the z coordinate from 0 to 15 inclusive
* @param tag the CompoundTag to set the tile to
*/
2020-07-24 17:24:53 +02:00
public abstract boolean setTile(int x, int y, int z, @Nonnull CompoundTag tag);
2016-06-13 06:47:50 +02:00
2020-07-24 18:00:08 +02:00
/**
* Whether the queue has any tiles being set
*/
public abstract boolean isSettingTiles();
2020-07-24 18:00:08 +02:00
/**
* Get a block at the given coordinates.
*/
2020-07-24 17:24:53 +02:00
@Nullable public abstract BlockState getBlock(int x, int y, int z);
2016-06-13 06:47:50 +02:00
2020-07-24 18:00:08 +02:00
/**
* Set a biome in XZ. This will likely set to the whole column
*/
2020-07-24 17:24:53 +02:00
@Deprecated public abstract boolean setBiome(int x, int z, @Nonnull BiomeType biome);
2020-07-24 18:00:08 +02:00
/**
* Set a biome in XYZ
*/
2020-07-24 17:24:53 +02:00
public abstract boolean setBiome(int x, int y, int z, @Nonnull BiomeType biome);
2016-06-13 06:47:50 +02:00
2020-07-24 18:00:08 +02:00
/**
* Whether the queue has any biomes to be set
*/
public abstract boolean isSettingBiomes();
2020-07-24 18:00:08 +02:00
/**
* Add entities to be created
*/
2020-07-24 17:24:53 +02:00
public void addEntities(@Nonnull List<? extends Entity> entities) {
for (Entity e : entities) {
this.setEntity(e);
}
}
2020-07-24 18:00:08 +02:00
/**
* Add an entity to be created
*/
2020-07-24 17:24:53 +02:00
public abstract boolean setEntity(@Nonnull Entity entity);
2020-07-24 18:00:08 +02:00
/**
* Get the list of chunks that are added manually. This usually indicated the queue is "read only".
*/
2020-07-24 17:24:53 +02:00
@Nonnull public abstract List<BlockVector2> getReadChunks();
2020-07-24 18:00:08 +02:00
/**
* Add a set of {@link BlockVector2} Chunk coordinates to the Read Chunks list
*/
2020-07-24 17:24:53 +02:00
public abstract void addReadChunks(@Nonnull Set<BlockVector2> readChunks);
2020-07-24 18:00:08 +02:00
/**
* Add a {@link BlockVector2} Chunk coordinate to the Read Chunks list
*/
2020-07-24 17:24:53 +02:00
public abstract void addReadChunk(@Nonnull BlockVector2 chunk);
2020-07-24 18:00:08 +02:00
/**
* Whether chunks should be unloaded after being accessed
*/
public abstract boolean isUnloadAfter();
2020-07-24 18:00:08 +02:00
/**
* Set whether chunks should be unloaded after being accessed
*/
public abstract void setUnloadAfter(boolean unloadAfter);
2020-07-24 18:00:08 +02:00
/**
* Get the {@link CuboidRegion} designated for direct regeneration
*/
2020-07-24 17:24:53 +02:00
@Nullable public abstract CuboidRegion getRegenRegion();
2020-07-24 18:00:08 +02:00
/**
* Set the {@link CuboidRegion} designated for direct regeneration
*/
2020-07-24 17:24:53 +02:00
public abstract void setRegenRegion(@Nonnull CuboidRegion regenRegion);
2020-07-24 18:00:08 +02:00
/**
* Set a specific chunk at the chunk coordinates XZ to be regenerated.
*/
public abstract void regenChunk(int x, int z);
2020-07-24 18:00:08 +02:00
/**
* Get the world the queue is writing to
*/
2020-07-24 17:24:53 +02:00
@Nullable public abstract World getWorld();
2016-06-13 06:47:50 +02:00
2020-07-24 18:00:08 +02:00
/**
* Set the queue as having been modified now
*/
2016-06-13 06:47:50 +02:00
public final void setModified() {
setModified(System.currentTimeMillis());
}
2020-07-24 18:00:08 +02:00
/**
* Enqueue the queue with the {@link GlobalBlockQueue}
*/
2019-09-08 20:02:45 +02:00
public boolean enqueue() {
return blockQueue.enqueue(this);
2016-06-13 06:47:50 +02:00
}
2020-07-24 18:00:08 +02:00
/**
* Start the queue
*/
public abstract void start();
2020-07-24 18:00:08 +02:00
/**
* Cancel the queue. Not yet implemented.
*/
public abstract void cancel();
2020-07-24 18:00:08 +02:00
/**
* Get the task to be run when all chunks have been accessed
*/
public abstract Runnable getCompleteTask();
/**
* Set the task to be run when all chunks have been accessed
*/
2020-07-24 17:24:53 +02:00
public abstract void setCompleteTask(@Nullable Runnable whenDone);
2020-07-24 18:00:08 +02:00
/**
* Return the chunk consumer set to the queue or null if one is not set
*/
@Nullable public abstract Consumer<BlockVector2> getChunkConsumer();
/**
* Set the Consumer that will
*/
2020-07-24 17:24:53 +02:00
public abstract void setChunkConsumer(@Nonnull Consumer<BlockVector2> consumer);
2020-07-24 18:00:08 +02:00
/**
* Fill a cuboid between two positions with a BlockState
*/
2020-07-24 17:24:53 +02:00
public void setCuboid(@Nonnull Location pos1, @Nonnull Location pos2, @Nonnull BlockState block) {
int yMin = Math.min(pos1.getY(), pos2.getY());
int yMax = Math.min(255, Math.max(pos1.getY(), pos2.getY()));
int xMin = Math.min(pos1.getX(), pos2.getX());
int xMax = Math.max(pos1.getX(), pos2.getX());
int zMin = Math.min(pos1.getZ(), pos2.getZ());
int zMax = Math.max(pos1.getZ(), pos2.getZ());
for (int y = yMin; y <= yMax; y++) {
for (int x = xMin; x <= xMax; x++) {
for (int z = zMin; z <= zMax; z++) {
2016-06-13 06:47:50 +02:00
setBlock(x, y, z, block);
}
}
}
}
2020-07-24 18:00:08 +02:00
/**
* Fill a cuboid between two positions with a Pattern
*/
2020-07-24 17:24:53 +02:00
public void setCuboid(@Nonnull Location pos1, @Nonnull Location pos2, @Nonnull Pattern blocks) {
int yMin = Math.min(pos1.getY(), pos2.getY());
int yMax = Math.min(255, Math.max(pos1.getY(), pos2.getY()));
int xMin = Math.min(pos1.getX(), pos2.getX());
int xMax = Math.max(pos1.getX(), pos2.getX());
int zMin = Math.min(pos1.getZ(), pos2.getZ());
int zMax = Math.max(pos1.getZ(), pos2.getZ());
for (int y = yMin; y <= yMax; y++) {
for (int x = xMin; x <= xMax; x++) {
for (int z = zMin; z <= zMax; z++) {
setBlock(x, y, z, blocks);
2016-06-13 06:47:50 +02:00
}
}
}
}
2020-07-24 18:00:08 +02:00
/**
* Fill a cuboid between two positions with a BiomeType
*/
2020-07-24 17:24:53 +02:00
public void setBiomeCuboid(@Nonnull Location pos1, @Nonnull Location pos2, @Nonnull BiomeType biome) {
int yMin = Math.min(pos1.getY(), pos2.getY());
int yMax = Math.min(255, Math.max(pos1.getY(), pos2.getY()));
int xMin = Math.min(pos1.getX(), pos2.getX());
int xMax = Math.max(pos1.getX(), pos2.getX());
int zMin = Math.min(pos1.getZ(), pos2.getZ());
int zMax = Math.max(pos1.getZ(), pos2.getZ());
for (int y = yMin; y <= yMax; y++) {
for (int x = xMin; x <= xMax; x++) {
for (int z = zMin; z <= zMax; z++) {
setBiome(x, y, z, biome);
}
}
}
}
2016-06-13 06:47:50 +02:00
}