use of com.plotsquared.core.queue.QueueCoordinator in project PlotSquared by IntellectualSites.
the class PlotModificationManager method copy.
/**
* Copy a plot to a location, both physically and the settings
*
* @param destination destination plot
* @param actor the actor associated with the copy
* @return Future that completes with {@code true} if the copy was successful, else {@code false}
*/
public CompletableFuture<Boolean> copy(@NonNull final Plot destination, @Nullable PlotPlayer<?> actor) {
final CompletableFuture<Boolean> future = new CompletableFuture<>();
final PlotId offset = PlotId.of(destination.getId().getX() - this.plot.getId().getX(), destination.getId().getY() - this.plot.getId().getY());
final Location db = destination.getBottomAbs();
final Location ob = this.plot.getBottomAbs();
final int offsetX = db.getX() - ob.getX();
final int offsetZ = db.getZ() - ob.getZ();
if (!this.plot.hasOwner()) {
TaskManager.runTaskLater(() -> future.complete(false), TaskTime.ticks(1L));
return future;
}
final Set<Plot> plots = this.plot.getConnectedPlots();
for (final Plot plot : plots) {
final Plot other = plot.getRelative(destination.getArea(), offset.getX(), offset.getY());
if (other.hasOwner()) {
TaskManager.runTaskLater(() -> future.complete(false), TaskTime.ticks(1L));
return future;
}
}
// world border
destination.updateWorldBorder();
// copy data
for (final Plot plot : plots) {
final Plot other = plot.getRelative(destination.getArea(), offset.getX(), offset.getY());
other.getPlotModificationManager().create(plot.getOwner(), false);
if (!plot.getFlagContainer().getFlagMap().isEmpty()) {
final Collection<PlotFlag<?, ?>> existingFlags = other.getFlags();
other.getFlagContainer().clearLocal();
other.getFlagContainer().addAll(plot.getFlagContainer().getFlagMap().values());
// Update the database
for (final PlotFlag<?, ?> flag : existingFlags) {
final PlotFlag<?, ?> newFlag = other.getFlagContainer().queryLocal(flag.getClass());
if (other.getFlagContainer().queryLocal(flag.getClass()) == null) {
DBFunc.removeFlag(other, flag);
} else {
DBFunc.setFlag(other, newFlag);
}
}
}
if (plot.isMerged()) {
other.setMerged(plot.getMerged());
}
if (plot.members != null && !plot.members.isEmpty()) {
other.members = plot.members;
for (UUID member : plot.members) {
DBFunc.setMember(other, member);
}
}
if (plot.trusted != null && !plot.trusted.isEmpty()) {
other.trusted = plot.trusted;
for (UUID trusted : plot.trusted) {
DBFunc.setTrusted(other, trusted);
}
}
if (plot.denied != null && !plot.denied.isEmpty()) {
other.denied = plot.denied;
for (UUID denied : plot.denied) {
DBFunc.setDenied(other, denied);
}
}
}
// copy terrain
final ArrayDeque<CuboidRegion> regions = new ArrayDeque<>(this.plot.getRegions());
final Runnable run = new Runnable() {
@Override
public void run() {
if (regions.isEmpty()) {
final QueueCoordinator queue = plot.getArea().getQueue();
for (final Plot current : plot.getConnectedPlots()) {
destination.getManager().claimPlot(current, queue);
}
if (queue.size() > 0) {
queue.enqueue();
}
destination.getPlotModificationManager().setSign();
future.complete(true);
return;
}
CuboidRegion region = regions.poll();
Location[] corners = Plot.getCorners(plot.getWorldName(), region);
Location pos1 = corners[0];
Location pos2 = corners[1];
Location newPos = pos1.add(offsetX, 0, offsetZ).withWorld(destination.getWorldName());
PlotSquared.platform().regionManager().copyRegion(pos1, pos2, newPos, actor, this);
}
};
run.run();
return future;
}
use of com.plotsquared.core.queue.QueueCoordinator in project PlotSquared by IntellectualSites.
the class ComponentPresetManager method buildInventory.
/**
* Build the component inventory for a player. This also checks
* if the player is in a compatible plot, and sends appropriate
* error messages if not
*
* @param player player
* @return Build inventory, if it could be created
*/
@Nullable
public PlotInventory buildInventory(final PlotPlayer<?> player) {
final Plot plot = player.getCurrentPlot();
if (plot == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
return null;
} else if (!plot.hasOwner()) {
player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
return null;
} else if (!plot.isOwner(player.getUUID()) && !plot.getTrusted().contains(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMPONENTS_OTHER)) {
player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
return null;
} else if (plot.getVolume() > Integer.MAX_VALUE) {
player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
return null;
}
final List<ComponentPreset> allowedPresets = new ArrayList<>(this.presets.size());
for (final ComponentPreset componentPreset : this.presets) {
if (!componentPreset.getPermission().isEmpty() && !Permissions.hasPermission(player, componentPreset.getPermission())) {
continue;
}
allowedPresets.add(componentPreset);
}
if (allowedPresets.isEmpty()) {
player.sendMessage(TranslatableCaption.of("preset.empty"));
return null;
}
final int size = (int) Math.ceil((double) allowedPresets.size() / 9.0D);
final PlotInventory plotInventory = new PlotInventory(this.inventoryUtil, player, size, TranslatableCaption.of("preset.title").getComponent(player)) {
@Override
public boolean onClick(final int index) {
if (!getPlayer().getCurrentPlot().equals(plot)) {
return false;
}
if (index < 0 || index >= allowedPresets.size()) {
return false;
}
final ComponentPreset componentPreset = allowedPresets.get(index);
if (componentPreset == null) {
return false;
}
if (plot.getRunning() > 0) {
getPlayer().sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
return false;
}
final Pattern pattern = PatternUtil.parse(null, componentPreset.getPattern(), false);
if (pattern == null) {
getPlayer().sendMessage(TranslatableCaption.of("preset.preset_invalid"));
return false;
}
if (componentPreset.getCost() > 0.0D) {
if (!econHandler.isEnabled(plot.getArea())) {
getPlayer().sendMessage(TranslatableCaption.of("preset.economy_disabled"), Template.of("preset", componentPreset.getDisplayName()));
return false;
}
if (econHandler.getMoney(getPlayer()) < componentPreset.getCost()) {
getPlayer().sendMessage(TranslatableCaption.of("preset.preset_cannot_afford"));
return false;
} else {
econHandler.withdrawMoney(getPlayer(), componentPreset.getCost());
getPlayer().sendMessage(TranslatableCaption.of("economy.removed_balance"), Template.of("money", econHandler.format(componentPreset.getCost())));
}
}
BackupManager.backup(getPlayer(), plot, () -> {
plot.addRunning();
QueueCoordinator queue = plot.getArea().getQueue();
queue.setCompleteTask(plot::removeRunning);
for (Plot current : plot.getConnectedPlots()) {
current.getPlotModificationManager().setComponent(componentPreset.getComponent().name(), pattern, player, queue);
}
queue.enqueue();
getPlayer().sendMessage(TranslatableCaption.of("working.generating_component"));
});
return false;
}
};
for (int i = 0; i < allowedPresets.size(); i++) {
final ComponentPreset preset = allowedPresets.get(i);
final List<String> lore = new ArrayList<>();
if (preset.getCost() > 0) {
if (!this.econHandler.isEnabled(plot.getArea())) {
lore.add(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("preset.preset_lore_economy_disabled").getComponent(player))));
} else {
lore.add(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("preset.preset_lore_cost").getComponent(player), Template.of("cost", String.format("%.2f", preset.getCost())))));
}
}
lore.add(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("preset.preset_lore_component").getComponent(player), Template.of("component", preset.getComponent().name().toLowerCase()), Template.of("prefix", TranslatableCaption.of("core.prefix").getComponent(player)))));
lore.removeIf(String::isEmpty);
lore.addAll(preset.getDescription());
plotInventory.setItem(i, new PlotItemStack(preset.getIcon().getId().replace("minecraft:", ""), 1, preset.getDisplayName(), lore.toArray(new String[0])));
}
return plotInventory;
}
use of com.plotsquared.core.queue.QueueCoordinator in project PlotSquared by IntellectualSites.
the class PlotModificationManager method clear.
/**
* Clear the plot
*
* <p>
* Use {@link #deletePlot(PlotPlayer, Runnable)} to clear and delete a plot
* </p>
*
* @param checkRunning Whether or not already executing tasks should be checked
* @param isDelete Whether or not the plot is being deleted
* @param actor The actor clearing the plot
* @param whenDone A runnable to execute when clearing finishes, or null
*/
public boolean clear(final boolean checkRunning, final boolean isDelete, @Nullable final PlotPlayer<?> actor, @Nullable final Runnable whenDone) {
if (checkRunning && this.plot.getRunning() != 0) {
return false;
}
final Set<CuboidRegion> regions = this.plot.getRegions();
final Set<Plot> plots = this.plot.getConnectedPlots();
final ArrayDeque<Plot> queue = new ArrayDeque<>(plots);
if (isDelete) {
this.removeSign();
}
PlotUnlinkEvent event = PlotSquared.get().getEventDispatcher().callUnlink(this.plot.getArea(), this.plot, true, !isDelete, isDelete ? PlotUnlinkEvent.REASON.DELETE : PlotUnlinkEvent.REASON.CLEAR);
if (event.getEventResult() != Result.DENY && this.unlinkPlot(event.isCreateRoad(), event.isCreateSign())) {
PlotSquared.get().getEventDispatcher().callPostUnlink(plot, event.getReason());
}
final PlotManager manager = this.plot.getArea().getPlotManager();
Runnable run = new Runnable() {
@Override
public void run() {
if (queue.isEmpty()) {
Runnable run = () -> {
for (CuboidRegion region : regions) {
Location[] corners = Plot.getCorners(plot.getWorldName(), region);
PlotSquared.platform().regionManager().clearAllEntities(corners[0], corners[1]);
}
TaskManager.runTask(whenDone);
};
QueueCoordinator queue = plot.getArea().getQueue();
for (Plot current : plots) {
if (isDelete || !current.hasOwner()) {
manager.unClaimPlot(current, null, queue);
} else {
manager.claimPlot(current, queue);
}
}
if (queue.size() > 0) {
queue.setCompleteTask(run);
queue.enqueue();
return;
}
run.run();
return;
}
Plot current = queue.poll();
if (plot.getArea().getTerrain() != PlotAreaTerrainType.NONE) {
try {
PlotSquared.platform().regionManager().regenerateRegion(current.getBottomAbs(), current.getTopAbs(), false, this);
} catch (UnsupportedOperationException exception) {
exception.printStackTrace();
return;
}
return;
}
manager.clearPlot(current, this, actor, null);
}
};
run.run();
return true;
}
use of com.plotsquared.core.queue.QueueCoordinator in project PlotSquared by IntellectualSites.
the class PlotModificationManager method move.
/**
* Moves a plot physically, as well as the corresponding settings.
*
* @param destination Plot moved to
* @param actor The actor executing the task
* @param whenDone task when done
* @param allowSwap whether to swap plots
* @return {@code true} if the move was successful, else {@code false}
*/
@NonNull
public CompletableFuture<Boolean> move(@NonNull final Plot destination, @Nullable final PlotPlayer<?> actor, @NonNull final Runnable whenDone, final boolean allowSwap) {
final PlotId offset = PlotId.of(destination.getId().getX() - this.plot.getId().getX(), destination.getId().getY() - this.plot.getId().getY());
Location db = destination.getBottomAbs();
Location ob = this.plot.getBottomAbs();
final int offsetX = db.getX() - ob.getX();
final int offsetZ = db.getZ() - ob.getZ();
if (!this.plot.hasOwner()) {
TaskManager.runTaskLater(whenDone, TaskTime.ticks(1L));
return CompletableFuture.completedFuture(false);
}
AtomicBoolean occupied = new AtomicBoolean(false);
Set<Plot> plots = this.plot.getConnectedPlots();
for (Plot plot : plots) {
Plot other = plot.getRelative(destination.getArea(), offset.getX(), offset.getY());
if (other.hasOwner()) {
if (!allowSwap) {
TaskManager.runTaskLater(whenDone, TaskTime.ticks(1L));
return CompletableFuture.completedFuture(false);
}
occupied.set(true);
} else {
plot.getPlotModificationManager().removeSign();
}
}
// world border
destination.updateWorldBorder();
final ArrayDeque<CuboidRegion> regions = new ArrayDeque<>(this.plot.getRegions());
// move / swap data
final PlotArea originArea = this.plot.getArea();
final Iterator<Plot> plotIterator = plots.iterator();
CompletableFuture<Boolean> future = null;
if (plotIterator.hasNext()) {
while (plotIterator.hasNext()) {
final Plot plot = plotIterator.next();
final Plot other = plot.getRelative(destination.getArea(), offset.getX(), offset.getY());
final CompletableFuture<Boolean> swapResult = plot.swapData(other);
if (future == null) {
future = swapResult;
} else {
future = future.thenCombine(swapResult, (fn, th) -> fn);
}
}
} else {
future = CompletableFuture.completedFuture(true);
}
return future.thenApply(result -> {
if (!result) {
return false;
}
// copy terrain
if (occupied.get()) {
new Runnable() {
@Override
public void run() {
if (regions.isEmpty()) {
// Update signs
destination.getPlotModificationManager().setSign();
setSign();
// Run final tasks
TaskManager.runTask(whenDone);
} else {
CuboidRegion region = regions.poll();
Location[] corners = Plot.getCorners(plot.getWorldName(), region);
Location pos1 = corners[0];
Location pos2 = corners[1];
Location pos3 = pos1.add(offsetX, 0, offsetZ).withWorld(destination.getWorldName());
PlotSquared.platform().regionManager().swap(pos1, pos2, pos3, actor, this);
}
}
}.run();
} else {
new Runnable() {
@Override
public void run() {
if (regions.isEmpty()) {
Plot plot = destination.getRelative(0, 0);
Plot originPlot = originArea.getPlotAbs(PlotId.of(plot.getId().getX() - offset.getX(), plot.getId().getY() - offset.getY()));
final Runnable clearDone = () -> {
QueueCoordinator queue = PlotModificationManager.this.plot.getArea().getQueue();
for (final Plot current : plot.getConnectedPlots()) {
PlotModificationManager.this.plot.getManager().claimPlot(current, queue);
}
if (queue.size() > 0) {
queue.enqueue();
}
plot.getPlotModificationManager().setSign();
TaskManager.runTask(whenDone);
};
if (originPlot != null) {
originPlot.getPlotModificationManager().clear(false, true, actor, clearDone);
} else {
clearDone.run();
}
return;
}
final Runnable task = this;
CuboidRegion region = regions.poll();
Location[] corners = Plot.getCorners(PlotModificationManager.this.plot.getWorldName(), region);
final Location pos1 = corners[0];
final Location pos2 = corners[1];
Location newPos = pos1.add(offsetX, 0, offsetZ).withWorld(destination.getWorldName());
PlotSquared.platform().regionManager().copyRegion(pos1, pos2, newPos, actor, task);
}
}.run();
}
return true;
});
}
use of com.plotsquared.core.queue.QueueCoordinator in project PlotSquared by IntellectualSites.
the class PlotModificationManager method unlinkPlot.
/**
* Unlink the plot and all connected plots.
*
* @param createRoad whether to recreate road
* @param createSign whether to recreate signs
* @return success/!cancelled
*/
public boolean unlinkPlot(final boolean createRoad, final boolean createSign) {
if (!this.plot.isMerged()) {
return false;
}
final Set<Plot> plots = this.plot.getConnectedPlots();
ArrayList<PlotId> ids = new ArrayList<>(plots.size());
for (Plot current : plots) {
current.setHome(null);
ids.add(current.getId());
}
this.plot.clearRatings();
QueueCoordinator queue = this.plot.getArea().getQueue();
if (createSign) {
this.removeSign();
}
PlotManager manager = this.plot.getArea().getPlotManager();
if (createRoad) {
manager.startPlotUnlink(ids, queue);
}
if (this.plot.getArea().getTerrain() != PlotAreaTerrainType.ALL && createRoad) {
for (Plot current : plots) {
if (current.isMerged(Direction.EAST)) {
manager.createRoadEast(current, queue);
if (current.isMerged(Direction.SOUTH)) {
manager.createRoadSouth(current, queue);
if (current.isMerged(Direction.SOUTHEAST)) {
manager.createRoadSouthEast(current, queue);
}
}
}
if (current.isMerged(Direction.SOUTH)) {
manager.createRoadSouth(current, queue);
}
}
}
for (Plot current : plots) {
boolean[] merged = new boolean[] { false, false, false, false };
current.setMerged(merged);
}
if (createSign) {
queue.setCompleteTask(() -> TaskManager.runTaskAsync(() -> {
for (Plot current : plots) {
current.getPlotModificationManager().setSign(PlayerManager.resolveName(current.getOwnerAbs()).getComponent(LocaleHolder.console()));
}
}));
}
if (createRoad) {
manager.finishPlotUnlink(ids, queue);
}
if (queue != null) {
queue.enqueue();
}
return true;
}
Aggregations