use of com.plotsquared.core.events.Result in project PlotSquared by IntellectualSites.
the class Delete method onCommand.
@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
Location location = player.getLocation();
final Plot plot = location.getPlotAbs();
if (plot == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
return false;
}
if (!plot.hasOwner()) {
player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
return false;
}
if (plot.getVolume() > Integer.MAX_VALUE) {
player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
return false;
}
Result eventResult = this.eventDispatcher.callDelete(plot).getEventResult();
if (eventResult == Result.DENY) {
player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Delete"));
return true;
}
boolean force = eventResult == Result.FORCE;
if (!force && !plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND_DELETE)) {
player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
return false;
}
final PlotArea plotArea = plot.getArea();
final java.util.Set<Plot> plots = plot.getConnectedPlots();
final int currentPlots = Settings.Limit.GLOBAL ? player.getPlotCount() : player.getPlotCount(location.getWorldName());
Runnable run = () -> {
if (plot.getRunning() > 0) {
player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
return;
}
final long start = System.currentTimeMillis();
if (Settings.Teleport.ON_DELETE) {
plot.getPlayersInPlot().forEach(playerInPlot -> plot.teleportPlayer(playerInPlot, TeleportCause.COMMAND_DELETE, result -> {
}));
}
boolean result = plot.getPlotModificationManager().deletePlot(player, () -> {
plot.removeRunning();
if (this.econHandler.isEnabled(plotArea)) {
PlotExpression valueExr = plotArea.getPrices().get("sell");
double value = plots.size() * valueExr.evaluate(currentPlots);
if (value > 0d) {
this.econHandler.depositMoney(player, value);
player.sendMessage(TranslatableCaption.of("economy.added_balance"), Template.of("money", this.econHandler.format(value)));
}
}
player.sendMessage(TranslatableCaption.of("working.deleting_done"), Template.of("amount", String.valueOf(System.currentTimeMillis() - start)), Template.of("plot", plot.getId().toString()));
eventDispatcher.callPostDelete(plot);
});
if (result) {
plot.addRunning();
} else {
player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
}
};
if (hasConfirmation(player)) {
CmdConfirm.addPending(player, getCommandString() + ' ' + plot.getId(), run);
} else {
TaskManager.runTask(run);
}
return true;
}
use of com.plotsquared.core.events.Result in project PlotSquared by IntellectualSites.
the class Clear method execute.
@Override
public CompletableFuture<Boolean> execute(final PlotPlayer<?> player, String[] args, RunnableVal3<Command, Runnable, Runnable> confirm, RunnableVal2<Command, CommandResult> whenDone) throws CommandException {
if (args.length != 0) {
sendUsage(player);
return CompletableFuture.completedFuture(false);
}
final Plot plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot"));
Result eventResult = this.eventDispatcher.callClear(plot).getEventResult();
if (eventResult == Result.DENY) {
player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Clear"));
return CompletableFuture.completedFuture(true);
}
if (plot.getVolume() > Integer.MAX_VALUE) {
player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
return CompletableFuture.completedFuture(true);
}
boolean force = eventResult == Result.FORCE;
checkTrue(force || plot.isOwner(player.getUUID()) || Permissions.hasPermission(player, "plots.admin.command.clear"), TranslatableCaption.of("permission.no_plot_perms"));
checkTrue(plot.getRunning() == 0, TranslatableCaption.of("errors.wait_for_timer"));
checkTrue(force || !Settings.Done.RESTRICT_BUILDING || !DoneFlag.isDone(plot) || Permissions.hasPermission(player, "plots.continue"), TranslatableCaption.of("done.done_already_done"));
confirm.run(this, () -> {
if (Settings.Teleport.ON_CLEAR) {
plot.getPlayersInPlot().forEach(playerInPlot -> plot.teleportPlayer(playerInPlot, TeleportCause.COMMAND_CLEAR, result -> {
}));
}
BackupManager.backup(player, plot, () -> {
final long start = System.currentTimeMillis();
boolean result = plot.getPlotModificationManager().clear(true, false, player, () -> {
plot.getPlotModificationManager().unlink();
TaskManager.runTask(() -> {
plot.removeRunning();
// If the state changes, then mark it as no longer done
if (DoneFlag.isDone(plot)) {
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(DoneFlag.class);
PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot);
if (event.getEventResult() != Result.DENY) {
plot.removeFlag(event.getFlag());
}
}
if (!plot.getFlag(AnalysisFlag.class).isEmpty()) {
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(AnalysisFlag.class);
PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot);
if (event.getEventResult() != Result.DENY) {
plot.removeFlag(event.getFlag());
}
}
player.sendMessage(TranslatableCaption.of("working.clearing_done"), Template.of("amount", String.valueOf(System.currentTimeMillis() - start)), Template.of("plot", plot.getId().toString()));
});
});
if (!result) {
player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
} else {
plot.addRunning();
}
});
}, null);
return CompletableFuture.completedFuture(true);
}
use of com.plotsquared.core.events.Result in project PlotSquared by IntellectualSites.
the class Plot method teleportPlayer.
/**
* Teleport a player to a plot and send them the teleport message.
*
* @param player the player
* @param cause the cause of the teleport
* @param resultConsumer Called with the result of the teleportation
*/
public void teleportPlayer(final PlotPlayer<?> player, TeleportCause cause, Consumer<Boolean> resultConsumer) {
Plot plot = this.getBasePlot(false);
Result result = this.eventDispatcher.callTeleport(player, player.getLocation(), plot, cause).getEventResult();
if (result == Result.DENY) {
player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Teleport"));
resultConsumer.accept(false);
return;
}
final Consumer<Location> locationConsumer = location -> {
if (Settings.Teleport.DELAY == 0 || Permissions.hasPermission(player, "plots.teleport.delay.bypass")) {
player.sendMessage(TranslatableCaption.of("teleport.teleported_to_plot"));
player.teleport(location, cause);
resultConsumer.accept(true);
return;
}
player.sendMessage(TranslatableCaption.of("teleport.teleport_in_seconds"), Template.of("amount", String.valueOf(Settings.Teleport.DELAY)));
final String name = player.getName();
TaskManager.addToTeleportQueue(name);
TaskManager.runTaskLater(() -> {
if (!TaskManager.removeFromTeleportQueue(name)) {
return;
}
try {
player.sendMessage(TranslatableCaption.of("teleport.teleported_to_plot"));
player.teleport(location, cause);
} catch (final Exception ignored) {
}
}, TaskTime.seconds(Settings.Teleport.DELAY));
resultConsumer.accept(true);
};
if (this.area.isHomeAllowNonmember() || plot.isAdded(player.getUUID())) {
this.getHome(locationConsumer);
} else {
this.getDefaultHome(false, locationConsumer);
}
}
use of com.plotsquared.core.events.Result 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.events.Result 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;
}
Aggregations