use of com.plotsquared.core.events.PlotMergeEvent in project PlotSquared by IntellectualSites.
the class EventDispatcher method callMerge.
public PlotMergeEvent callMerge(Plot plot, Direction dir, int max, PlotPlayer<?> player) {
PlotMergeEvent event = new PlotMergeEvent(plot.getWorldName(), plot, dir, max, player);
callEvent(event);
return event;
}
use of com.plotsquared.core.events.PlotMergeEvent 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.events.PlotMergeEvent in project PlotSquared by IntellectualSites.
the class PlotModificationManager method create.
/**
* Register a plot and create it in the database<br>
* - The plot will not be created if the owner is null<br>
* - Any setting from before plot creation will not be saved until the server is stopped properly. i.e. Set any values/options after plot
* creation.
*
* @param uuid the uuid of the plot owner
* @param notify notify
* @return {@code true} if plot was created successfully, else {@code false}
*/
public boolean create(@NonNull final UUID uuid, final boolean notify) {
this.plot.setOwnerAbs(uuid);
Plot existing = this.plot.getArea().getOwnedPlotAbs(this.plot.getId());
if (existing != null) {
throw new IllegalStateException("Plot already exists!");
}
if (notify) {
Integer meta = (Integer) this.plot.getArea().getMeta("worldBorder");
if (meta != null) {
this.plot.updateWorldBorder();
}
}
Plot.connected_cache = null;
Plot.regions_cache = null;
this.plot.getTrusted().clear();
this.plot.getMembers().clear();
this.plot.getDenied().clear();
this.plot.settings = new PlotSettings();
if (this.plot.getArea().addPlot(this.plot)) {
DBFunc.createPlotAndSettings(this.plot, () -> {
PlotArea plotworld = plot.getArea();
if (notify && plotworld.isAutoMerge()) {
final PlotPlayer<?> player = PlotSquared.platform().playerManager().getPlayerIfExists(uuid);
PlotMergeEvent event = PlotSquared.get().getEventDispatcher().callMerge(this.plot, Direction.ALL, Integer.MAX_VALUE, player);
if (event.getEventResult() == Result.DENY) {
if (player != null) {
player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Auto merge on claim"));
}
return;
}
if (plot.getPlotModificationManager().autoMerge(event.getDir(), event.getMax(), uuid, player, true)) {
PlotSquared.get().getEventDispatcher().callPostMerge(player, plot);
}
}
});
return true;
}
LOGGER.info("Failed to add plot {} to plot area {}", this.plot.getId().toCommaSeparatedString(), this.plot.getArea().toString());
return false;
}
use of com.plotsquared.core.events.PlotMergeEvent in project PlotSquared by IntellectualSites.
the class AutoClaimFinishTask method call.
@Override
public Boolean call() {
try (final MetaDataAccess<Boolean> autoAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_AUTO)) {
autoAccess.remove();
}
if (plot == null) {
player.sendMessage(TranslatableCaption.of("errors.no_free_plots"));
return false;
}
plot.claim(player, true, schematic, false, true);
eventDispatcher.callPostAuto(player, plot);
if (area.isAutoMerge()) {
PlotMergeEvent event = this.eventDispatcher.callMerge(plot, Direction.ALL, Integer.MAX_VALUE, player);
if (event.getEventResult() == Result.DENY) {
player.sendMessage(TranslatableCaption.of("events.event_denied"), Templates.of("value", "Auto Merge"));
} else {
if (plot.getPlotModificationManager().autoMerge(event.getDir(), event.getMax(), player.getUUID(), player, true)) {
eventDispatcher.callPostMerge(player, plot);
}
}
}
return true;
}
Aggregations