use of com.plotsquared.core.util.PlotExpression 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.util.PlotExpression in project PlotSquared by IntellectualSites.
the class MainCommand method execute.
@Override
public CompletableFuture<Boolean> execute(final PlotPlayer<?> player, String[] args, RunnableVal3<Command, Runnable, Runnable> confirm, RunnableVal2<Command, CommandResult> whenDone) {
// Optional command scope //
Location location = null;
Plot plot = null;
boolean tp = false;
if (args.length >= 2) {
PlotArea area = player.getApplicablePlotArea();
Plot newPlot = Plot.fromString(area, args[0]);
if (newPlot != null && (player instanceof ConsolePlayer || newPlot.getArea().equals(area) || Permissions.hasPermission(player, Permission.PERMISSION_ADMIN) || Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_AREA_SUDO)) && !newPlot.isDenied(player.getUUID())) {
final Location newLoc;
if (newPlot.getArea() instanceof SinglePlotArea) {
newLoc = newPlot.isLoaded() ? newPlot.getCenterSynchronous() : Location.at("", 0, 0, 0);
} else {
newLoc = newPlot.getCenterSynchronous();
}
if (player.canTeleport(newLoc)) {
// Save meta
try (final MetaDataAccess<Location> locationMetaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LOCATION)) {
location = locationMetaDataAccess.get().orElse(null);
locationMetaDataAccess.set(newLoc);
}
try (final MetaDataAccess<Plot> plotMetaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
plot = plotMetaDataAccess.get().orElse(null);
plotMetaDataAccess.set(newPlot);
}
tp = true;
} else {
player.sendMessage(TranslatableCaption.of("border.denied"));
}
// Trim command
args = Arrays.copyOfRange(args, 1, args.length);
}
if (args.length >= 2 && !args[0].isEmpty() && args[0].charAt(0) == '-') {
if ("f".equals(args[0].substring(1))) {
confirm = new RunnableVal3<>() {
@Override
public void run(Command cmd, Runnable success, Runnable failure) {
if (area != null && PlotSquared.platform().econHandler().isEnabled(area)) {
PlotExpression priceEval = area.getPrices().get(cmd.getFullId());
double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (price != 0d && PlotSquared.platform().econHandler().getMoney(player) < price) {
if (failure != null) {
failure.run();
}
return;
}
}
if (success != null) {
success.run();
}
}
};
args = Arrays.copyOfRange(args, 1, args.length);
} else {
player.sendMessage(TranslatableCaption.of("errors.invalid_command_flag"));
return CompletableFuture.completedFuture(false);
}
}
}
try {
super.execute(player, args, confirm, whenDone);
} catch (CommandException e) {
throw e;
} catch (Throwable e) {
e.printStackTrace();
String message = e.getMessage();
if (message != null) {
player.sendMessage(TranslatableCaption.of("errors.error"), net.kyori.adventure.text.minimessage.Template.of("value", message));
} else {
player.sendMessage(TranslatableCaption.of("errors.error_console"));
}
}
// Reset command scope //
if (tp && !(player instanceof ConsolePlayer)) {
try (final MetaDataAccess<Location> locationMetaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LOCATION)) {
if (location == null) {
locationMetaDataAccess.remove();
} else {
locationMetaDataAccess.set(location);
}
}
try (final MetaDataAccess<Plot> plotMetaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
if (plot == null) {
plotMetaDataAccess.remove();
} else {
plotMetaDataAccess.set(plot);
}
}
}
return CompletableFuture.completedFuture(true);
}
use of com.plotsquared.core.util.PlotExpression in project PlotSquared by IntellectualSites.
the class MainCommand method onCommand.
public static boolean onCommand(final PlotPlayer<?> player, String... args) {
final EconHandler econHandler = PlotSquared.platform().econHandler();
if (args.length >= 1 && args[0].contains(":")) {
String[] split2 = args[0].split(":");
if (split2.length == 2) {
// Ref: c:v, this will push value to the last spot in the array
// ex. /p h:2 SomeUsername
// > /p h SomeUsername 2
String[] tmp = new String[args.length + 1];
tmp[0] = split2[0];
tmp[args.length] = split2[1];
if (args.length >= 2) {
System.arraycopy(args, 1, tmp, 1, args.length - 1);
}
args = tmp;
}
}
try {
getInstance().execute(player, args, new RunnableVal3<>() {
@Override
public void run(final Command cmd, final Runnable success, final Runnable failure) {
if (cmd.hasConfirmation(player)) {
CmdConfirm.addPending(player, cmd.getUsage(), () -> {
PlotArea area = player.getApplicablePlotArea();
if (area != null && econHandler.isEnabled(area)) {
PlotExpression priceEval = area.getPrices().get(cmd.getFullId());
double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (econHandler.getMoney(player) < price) {
if (failure != null) {
failure.run();
}
return;
}
}
if (success != null) {
success.run();
}
});
return;
}
PlotArea area = player.getApplicablePlotArea();
if (area != null && econHandler.isEnabled(area)) {
PlotExpression priceEval = area.getPrices().get(cmd.getFullId());
double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (price != 0d && econHandler.getMoney(player) < price) {
if (failure != null) {
failure.run();
}
return;
}
}
if (success != null) {
success.run();
}
}
}, new RunnableVal2<>() {
@Override
public void run(Command cmd, CommandResult result) {
// Post command stuff!?
}
}).thenAccept(result -> {
// TODO: Something with the command result
});
} catch (CommandException e) {
e.perform(player);
}
// Always true
return true;
}
use of com.plotsquared.core.util.PlotExpression in project PlotSquared by IntellectualSites.
the class Claim method onCommand.
@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
String schematic = null;
if (args.length >= 1) {
schematic = args[0];
}
Location location = player.getLocation();
Plot plot = location.getPlotAbs();
if (plot == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
return false;
}
final PlayerClaimPlotEvent event = this.eventDispatcher.callClaim(player, plot, schematic);
schematic = event.getSchematic();
if (event.getEventResult() == Result.DENY) {
player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Claim"));
return true;
}
boolean force = event.getEventResult() == Result.FORCE;
int currentPlots = Settings.Limit.GLOBAL ? player.getPlotCount() : player.getPlotCount(location.getWorldName());
final PlotArea area = plot.getArea();
try (final MetaDataAccess<Integer> metaDataAccess = player.accessPersistentMetaData(PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) {
int grants = 0;
if (currentPlots >= player.getAllowedPlots() && !force) {
if (metaDataAccess.isPresent()) {
grants = metaDataAccess.get().orElse(0);
if (grants <= 0) {
player.sendMessage(TranslatableCaption.of("permission.cant_claim_more_plots"), Template.of("amount", String.valueOf(grants)));
metaDataAccess.remove();
}
} else {
player.sendMessage(TranslatableCaption.of("permission.cant_claim_more_plots"), Template.of("amount", String.valueOf(player.getAllowedPlots())));
return false;
}
}
if (!plot.canClaim(player)) {
player.sendMessage(TranslatableCaption.of("working.plot_is_claimed"));
return false;
}
if (schematic != null && !schematic.isEmpty()) {
if (area.isSchematicClaimSpecify()) {
if (!area.hasSchematic(schematic)) {
player.sendMessage(TranslatableCaption.of("schematics.schematic_invalid_named"), Template.of("schemname", schematic), Template.of("reason", "non-existent"));
}
if (!Permissions.hasPermission(player, Permission.PERMISSION_CLAIM_SCHEMATIC.format(schematic)) && !Permissions.hasPermission(player, "plots.admin.command.schematic") && !force) {
player.sendMessage(TranslatableCaption.of("permission.no_schematic_permission"), Template.of("value", schematic));
}
}
}
if (this.econHandler.isEnabled(area) && !force) {
PlotExpression costExr = area.getPrices().get("claim");
double cost = costExr.evaluate(currentPlots);
if (cost > 0d) {
if (!this.econHandler.isSupported()) {
player.sendMessage(TranslatableCaption.of("economy.vault_or_consumer_null"));
return false;
}
if (this.econHandler.getMoney(player) < cost) {
player.sendMessage(TranslatableCaption.of("economy.cannot_afford_plot"), Template.of("money", this.econHandler.format(cost)), Template.of("balance", this.econHandler.format(this.econHandler.getMoney(player))));
return false;
}
this.econHandler.withdrawMoney(player, cost);
player.sendMessage(TranslatableCaption.of("economy.removed_balance"), Template.of("money", this.econHandler.format(cost)), Template.of("balance", this.econHandler.format(this.econHandler.getMoney(player))));
}
}
if (grants > 0) {
if (grants == 1) {
metaDataAccess.remove();
} else {
metaDataAccess.set(grants - 1);
}
player.sendMessage(TranslatableCaption.of("economy.removed_granted_plot"), Template.of("usedGrants", String.valueOf((grants - 1))), Template.of("remainingGrants", String.valueOf(grants)));
}
}
if (!Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_BYPASS_BORDER)) {
int border = area.getBorder();
if (border != Integer.MAX_VALUE && plot.getDistanceFromOrigin() > border && !force) {
player.sendMessage(TranslatableCaption.of("border.denied"));
return false;
}
}
plot.setOwnerAbs(player.getUUID());
final String finalSchematic = schematic;
DBFunc.createPlotSafe(plot, () -> {
try {
TaskManager.getPlatformImplementation().sync(() -> {
if (!plot.claim(player, true, finalSchematic, false, false)) {
LOGGER.info("Failed to claim plot {}", plot.getId().toCommaSeparatedString());
player.sendMessage(TranslatableCaption.of("working.plot_not_claimed"));
plot.setOwnerAbs(null);
} else if (area.isAutoMerge()) {
PlotMergeEvent mergeEvent = Claim.this.eventDispatcher.callMerge(plot, Direction.ALL, Integer.MAX_VALUE, player);
if (mergeEvent.getEventResult() == Result.DENY) {
player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Auto merge on claim"));
} else {
if (plot.getPlotModificationManager().autoMerge(mergeEvent.getDir(), mergeEvent.getMax(), player.getUUID(), player, true)) {
eventDispatcher.callPostMerge(player, plot);
}
}
}
return null;
});
} catch (final Exception e) {
e.printStackTrace();
}
}, () -> {
LOGGER.info("Failed to add plot to database: {}", plot.getId().toCommaSeparatedString());
player.sendMessage(TranslatableCaption.of("working.plot_not_claimed"));
plot.setOwnerAbs(null);
});
return true;
}
use of com.plotsquared.core.util.PlotExpression in project PlotSquared by IntellectualSites.
the class Auto method onCommand.
@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
PlotArea plotarea = player.getApplicablePlotArea();
if (plotarea == null) {
final PermissionHandler permissionHandler = PlotSquared.platform().permissionHandler();
if (permissionHandler.hasCapability(PermissionHandler.PermissionHandlerCapability.PER_WORLD_PERMISSIONS)) {
for (final PlotArea area : this.plotAreaManager.getAllPlotAreas()) {
if (player.hasPermission(area.getWorldName(), "plots.auto")) {
if (plotarea != null) {
plotarea = null;
break;
}
plotarea = area;
}
}
}
if (this.plotAreaManager.getAllPlotAreas().length == 1) {
plotarea = this.plotAreaManager.getAllPlotAreas()[0];
}
if (plotarea == null) {
player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
return false;
}
}
int sizeX = 1;
int sizeZ = 1;
String schematic = null;
boolean mega = false;
if (args.length > 0) {
try {
String[] split = args[0].split("[,;]");
if (split.length == 2) {
sizeX = Integer.parseInt(split[0]);
sizeZ = Integer.parseInt(split[1]);
} else {
player.sendMessage(TranslatableCaption.of("commandconfig.command_syntax"), Template.of("value", getUsage()));
return true;
}
if (sizeX < 1 || sizeZ < 1) {
player.sendMessage(TranslatableCaption.of("error.plot_size_negative"));
return true;
}
if (args.length > 1) {
schematic = args[1];
}
mega = true;
} catch (NumberFormatException ignored) {
sizeX = 1;
sizeZ = 1;
schematic = args[0];
}
}
PlayerAutoPlotEvent event = this.eventDispatcher.callAuto(player, plotarea, schematic, sizeX, sizeZ);
if (event.getEventResult() == Result.DENY) {
player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Auto claim"));
return true;
}
boolean force = event.getEventResult() == Result.FORCE;
sizeX = event.getSizeX();
sizeZ = event.getSizeZ();
schematic = event.getSchematic();
if (!force && mega && !Permissions.hasPermission(player, Permission.PERMISSION_AUTO_MEGA)) {
player.sendMessage(TranslatableCaption.of("permission.no_permission"), Template.of("node", String.valueOf(Permission.PERMISSION_AUTO_MEGA)));
}
if (!force && sizeX * sizeZ > Settings.Claim.MAX_AUTO_AREA) {
player.sendMessage(TranslatableCaption.of("permission.cant_claim_more_plots_num"), Template.of("amount", String.valueOf(Settings.Claim.MAX_AUTO_AREA)));
return false;
}
final int allowed_plots = player.getAllowedPlots();
try (final MetaDataAccess<Boolean> metaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_AUTO)) {
if (!force && (metaDataAccess.get().orElse(false) || !checkAllowedPlots(player, plotarea, allowed_plots, sizeX, sizeZ))) {
return false;
}
}
if (schematic != null && !schematic.isEmpty()) {
if (!plotarea.hasSchematic(schematic)) {
player.sendMessage(TranslatableCaption.of("schematics.schematic_invalid_named"), Template.of("schemname", schematic), Template.of("reason", "non-existent"));
return true;
}
if (!force && !Permissions.hasPermission(player, Permission.PERMISSION_CLAIM_SCHEMATIC.format(schematic)) && !Permissions.hasPermission(player, "plots.admin.command.schematic")) {
player.sendMessage(TranslatableCaption.of("permission.no_permission"), Template.of("node", "plots.claim.%s0"));
return true;
}
}
if (this.econHandler != null && plotarea.useEconomy()) {
PlotExpression costExp = plotarea.getPrices().get("claim");
double cost = costExp.evaluate(Settings.Limit.GLOBAL ? player.getPlotCount() : player.getPlotCount(plotarea.getWorldName()));
cost = (sizeX * sizeZ) * cost;
if (cost > 0d) {
if (!this.econHandler.isSupported()) {
player.sendMessage(TranslatableCaption.of("economy.vault_or_consumer_null"));
return false;
}
if (!force && this.econHandler.getMoney(player) < cost) {
player.sendMessage(TranslatableCaption.of("economy.cannot_afford_plot"), Template.of("money", this.econHandler.format(cost)), Template.of("balance", this.econHandler.format(this.econHandler.getMoney(player))));
return false;
}
this.econHandler.withdrawMoney(player, cost);
player.sendMessage(TranslatableCaption.of("economy.removed_balance"), Template.of("money", this.econHandler.format(cost)));
}
}
List<Plot> plots = this.servicePipeline.pump(new AutoService.AutoQuery(player, null, sizeX, sizeZ, plotarea)).through(AutoService.class).getResult();
plots = this.eventDispatcher.callAutoPlotsChosen(player, plots).getPlots();
if (plots.isEmpty()) {
player.sendMessage(TranslatableCaption.of("errors.no_free_plots"));
return false;
} else if (plots.size() == 1) {
this.claimSingle(player, plots.get(0), plotarea, schematic);
} else {
final Iterator<Plot> plotIterator = plots.iterator();
while (plotIterator.hasNext()) {
Plot plot = plotIterator.next();
if (!plot.canClaim(player)) {
continue;
}
plot.claim(player, !plotIterator.hasNext(), null, true, true);
eventDispatcher.callPostAuto(player, plot);
}
final PlotAutoMergeEvent mergeEvent = this.eventDispatcher.callAutoMerge(plots.get(0), plots.stream().map(Plot::getId).collect(Collectors.toList()));
if (!force && mergeEvent.getEventResult() == Result.DENY) {
player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Auto merge"));
return false;
}
return plotarea.mergePlots(mergeEvent.getPlots(), true);
}
return true;
}
Aggregations