From d77a8c8fa43c2b9aa1e45e33b0c8e7b2b5460b99 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 9 Sep 2021 20:37:44 +0100 Subject: [PATCH] Some improvements to events (#3238) #3222 #3072 --- .../bukkit/player/BukkitPlayer.java | 17 +++- .../com/plotsquared/core/command/Area.java | 14 +-- .../com/plotsquared/core/command/Auto.java | 61 ++++++------- .../com/plotsquared/core/command/Claim.java | 2 +- .../com/plotsquared/core/command/Clear.java | 2 +- .../com/plotsquared/core/command/Cluster.java | 2 +- .../com/plotsquared/core/command/Delete.java | 2 +- .../com/plotsquared/core/command/Deny.java | 5 +- .../plotsquared/core/command/HomeCommand.java | 2 +- .../com/plotsquared/core/command/Like.java | 2 +- .../com/plotsquared/core/command/Middle.java | 2 +- .../com/plotsquared/core/command/Rate.java | 2 +- .../plotsquared/core/command/Template.java | 2 +- .../com/plotsquared/core/command/Visit.java | 2 +- .../core/events/PlayerAutoPlotEvent.java | 91 ++++++++++++++++--- .../events/PlayerAutoPlotsChosenEvent.java | 86 ++++++++++++++++++ .../events/PlayerTeleportToPlotEvent.java | 23 ++++- .../core/events/PlotClaimedNotifyEvent.java | 57 ++++++++++++ .../plotsquared/core/events/PlotEvent.java | 2 +- .../core/events/TeleportCause.java | 42 ++++++++- .../plotsquared/core/player/PlotPlayer.java | 6 +- .../java/com/plotsquared/core/plot/Plot.java | 23 ++++- .../core/setup/CommonSetupSteps.java | 2 +- .../core/util/EventDispatcher.java | 28 +++++- .../core/util/task/AutoClaimFinishTask.java | 2 +- build.gradle.kts | 2 +- 26 files changed, 398 insertions(+), 83 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotsChosenEvent.java create mode 100644 Core/src/main/java/com/plotsquared/core/events/PlotClaimedNotifyEvent.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java index caafbea77..34d85df6e 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java @@ -362,12 +362,19 @@ public class BukkitPlayer extends PlotPlayer { } } + /** + * Convert from PlotSquared's {@link TeleportCause} to Bukkit's {@link PlayerTeleportEvent.TeleportCause} + * + * @param cause PlotSquared teleport cause to convert + * @return Bukkit's equivalent teleport cause + */ public PlayerTeleportEvent.TeleportCause getTeleportCause(final @NonNull TeleportCause cause) { - return switch (cause) { - case COMMAND -> PlayerTeleportEvent.TeleportCause.COMMAND; - case PLUGIN -> PlayerTeleportEvent.TeleportCause.PLUGIN; - default -> PlayerTeleportEvent.TeleportCause.UNKNOWN; - }; + if (TeleportCause.CauseSets.COMMAND.contains(cause)) { + return PlayerTeleportEvent.TeleportCause.COMMAND; + } else if (cause == TeleportCause.UNKNOWN) { + return PlayerTeleportEvent.TeleportCause.UNKNOWN; + } + return PlayerTeleportEvent.TeleportCause.PLUGIN; } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Area.java b/Core/src/main/java/com/plotsquared/core/command/Area.java index 08a2451b7..51df7bb9a 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Area.java +++ b/Core/src/main/java/com/plotsquared/core/command/Area.java @@ -395,7 +395,7 @@ public class Area extends SubCommand { final String world = this.setupUtils.setupWorld(builder); if (this.worldUtil.isWorld(world)) { PlotSquared.get().loadWorld(world, null); - player.teleport(this.worldUtil.getSpawn(world), TeleportCause.COMMAND); + player.teleport(this.worldUtil.getSpawn(world), TeleportCause.COMMAND_AREA_CREATE); player.sendMessage(TranslatableCaption.of("setup.setup_finished")); if (area.getTerrain() != PlotAreaTerrainType.ALL) { QueueCoordinator queue = blockQueue.getNewQueue(worldUtil.getWeWorld(world)); @@ -525,7 +525,7 @@ public class Area extends SubCommand { builder.generatorName(PlotSquared.platform().pluginName()); String world = this.setupUtils.setupWorld(builder); if (this.worldUtil.isWorld(world)) { - player.teleport(this.worldUtil.getSpawn(world), TeleportCause.COMMAND); + player.teleport(this.worldUtil.getSpawn(world), TeleportCause.COMMAND_AREA_CREATE); player.sendMessage(TranslatableCaption.of("setup.setup_finished")); } else { player.sendMessage( @@ -560,13 +560,13 @@ public class Area extends SubCommand { } if (this.worldUtil.isWorld(pa.getWorldName())) { if (!player.getLocation().getWorldName().equals(pa.getWorldName())) { - player.teleport(this.worldUtil.getSpawn(pa.getWorldName()), TeleportCause.COMMAND); + player.teleport(this.worldUtil.getSpawn(pa.getWorldName()), TeleportCause.COMMAND_AREA_CREATE); } } else { builder.terrainType(PlotAreaTerrainType.NONE); builder.plotAreaType(PlotAreaType.NORMAL); this.setupUtils.setupWorld(builder); - player.teleport(this.worldUtil.getSpawn(pa.getWorldName()), TeleportCause.COMMAND); + player.teleport(this.worldUtil.getSpawn(pa.getWorldName()), TeleportCause.COMMAND_AREA_CREATE); } metaData.computeIfAbsent(player.getUUID(), missingUUID -> new HashMap<>()).put("area_create_area", pa); player.sendMessage( @@ -795,10 +795,10 @@ public class Area extends SubCommand { if (area instanceof SinglePlotArea) { ((SinglePlotArea) area).loadWorld(PlotId.of(0, 0)); center = this.worldUtil.getSpawn(PlotId.of(0, 0).toUnderscoreSeparatedString()); - player.teleport(center, TeleportCause.COMMAND); + player.teleport(center, TeleportCause.COMMAND_AREA_TELEPORT); } else if (area.getType() != PlotAreaType.PARTIAL) { center = this.worldUtil.getSpawn(area.getWorldName()); - player.teleport(center, TeleportCause.COMMAND); + player.teleport(center, TeleportCause.COMMAND_AREA_TELEPORT); } else { CuboidRegion region = area.getRegion(); center = Location.at(area.getWorldName(), @@ -810,7 +810,7 @@ public class Area extends SubCommand { .getZ()) / 2 ); this.worldUtil.getHighestBlock(area.getWorldName(), center.getX(), center.getZ(), - y -> player.teleport(center.withY(1 + y), TeleportCause.COMMAND) + y -> player.teleport(center.withY(1 + y), TeleportCause.COMMAND_AREA_TELEPORT) ); } return true; diff --git a/Core/src/main/java/com/plotsquared/core/command/Auto.java b/Core/src/main/java/com/plotsquared/core/command/Auto.java index 64c3a254e..f4392f4d8 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Auto.java +++ b/Core/src/main/java/com/plotsquared/core/command/Auto.java @@ -209,31 +209,24 @@ public class Auto extends SubCommand { return false; } } - int size_x = 1; - int size_z = 1; + int sizeX = 1; + int sizeZ = 1; String schematic = null; boolean mega = false; if (args.length > 0) { try { - String[] split = args[0].split(",|;"); - switch (split.length) { - case 1 -> { - size_x = 1; - size_z = 1; - } - case 2 -> { - size_x = Integer.parseInt(split[0]); - size_z = Integer.parseInt(split[1]); - } - default -> { - player.sendMessage( - TranslatableCaption.of("commandconfig.command_syntax"), - Template.of("value", getUsage()) - ); - return true; - } + 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 (size_x < 1 || size_z < 1) { + if (sizeX < 1 || sizeZ < 1) { player.sendMessage(TranslatableCaption.of("error.plot_size_negative")); return true; } @@ -242,13 +235,13 @@ public class Auto extends SubCommand { } mega = true; } catch (NumberFormatException ignored) { - size_x = 1; - size_z = 1; + sizeX = 1; + sizeZ = 1; schematic = args[0]; } } PlayerAutoPlotEvent event = this.eventDispatcher - .callAuto(player, plotarea, schematic, size_x, size_z); + .callAuto(player, plotarea, schematic, sizeX, sizeZ); if (event.getEventResult() == Result.DENY) { player.sendMessage( TranslatableCaption.of("events.event_denied"), @@ -257,8 +250,8 @@ public class Auto extends SubCommand { return true; } boolean force = event.getEventResult() == Result.FORCE; - size_x = event.getSize_x(); - size_z = event.getSize_z(); + sizeX = event.getSizeX(); + sizeZ = event.getSizeZ(); schematic = event.getSchematic(); if (!force && mega && !Permissions.hasPermission(player, Permission.PERMISSION_AUTO_MEGA)) { player.sendMessage( @@ -266,7 +259,7 @@ public class Auto extends SubCommand { Template.of("node", String.valueOf(Permission.PERMISSION_AUTO_MEGA)) ); } - if (!force && size_x * size_z > Settings.Claim.MAX_AUTO_AREA) { + 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)) @@ -277,7 +270,7 @@ public class Auto extends SubCommand { try (final MetaDataAccess metaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_AUTO)) { if (!force && (metaDataAccess.get().orElse(false) || !checkAllowedPlots(player, - plotarea, allowed_plots, size_x, size_z + plotarea, allowed_plots, sizeX, sizeZ ))) { return false; } @@ -309,7 +302,7 @@ public class Auto extends SubCommand { double cost = costExp.evaluate(Settings.Limit.GLOBAL ? player.getPlotCount() : player.getPlotCount(plotarea.getWorldName())); - cost = (size_x * size_z) * cost; + cost = (sizeX * sizeZ) * cost; if (cost > 0d) { if (!this.econHandler.isSupported()) { player.sendMessage(TranslatableCaption.of("economy.vault_or_consumer_null")); @@ -331,11 +324,13 @@ public class Auto extends SubCommand { } } - final List plots = this.servicePipeline - .pump(new AutoService.AutoQuery(player, null, size_x, size_z, plotarea)) + List 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; @@ -344,7 +339,11 @@ public class Auto extends SubCommand { } else { final Iterator plotIterator = plots.iterator(); while (plotIterator.hasNext()) { - plotIterator.next().claim(player, !plotIterator.hasNext(), null); + Plot plot = plotIterator.next(); + if (!plot.canClaim(player)) { + continue; + } + plot.claim(player, !plotIterator.hasNext(), null, true, true); } final PlotAutoMergeEvent mergeEvent = this.eventDispatcher.callAutoMerge( plots.get(0), diff --git a/Core/src/main/java/com/plotsquared/core/command/Claim.java b/Core/src/main/java/com/plotsquared/core/command/Claim.java index 4edbf9dde..c55cbd13d 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Claim.java +++ b/Core/src/main/java/com/plotsquared/core/command/Claim.java @@ -193,7 +193,7 @@ public class Claim extends SubCommand { DBFunc.createPlotSafe(plot, () -> { try { TaskManager.getPlatformImplementation().sync(() -> { - if (!plot.claim(player, true, finalSchematic, false)) { + 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); diff --git a/Core/src/main/java/com/plotsquared/core/command/Clear.java b/Core/src/main/java/com/plotsquared/core/command/Clear.java index ac141779a..20abe1beb 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Clear.java +++ b/Core/src/main/java/com/plotsquared/core/command/Clear.java @@ -104,7 +104,7 @@ public class Clear extends Command { .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, + plot.getPlayersInPlot().forEach(playerInPlot -> plot.teleportPlayer(playerInPlot, TeleportCause.COMMAND_CLEAR, result -> { } )); diff --git a/Core/src/main/java/com/plotsquared/core/command/Cluster.java b/Core/src/main/java/com/plotsquared/core/command/Cluster.java index 63e07c8ec..b1e017db0 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Cluster.java +++ b/Core/src/main/java/com/plotsquared/core/command/Cluster.java @@ -730,7 +730,7 @@ public class Cluster extends SubCommand { return false; } } - cluster.getHome(home -> player.teleport(home, TeleportCause.COMMAND)); + cluster.getHome(home -> player.teleport(home, TeleportCause.COMMAND_CLUSTER_TELEPORT)); player.sendMessage(TranslatableCaption.of("cluster.cluster_teleporting")); return true; } diff --git a/Core/src/main/java/com/plotsquared/core/command/Delete.java b/Core/src/main/java/com/plotsquared/core/command/Delete.java index 86ed9a944..e68133855 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Delete.java +++ b/Core/src/main/java/com/plotsquared/core/command/Delete.java @@ -107,7 +107,7 @@ public class Delete extends SubCommand { } final long start = System.currentTimeMillis(); if (Settings.Teleport.ON_DELETE) { - plot.getPlayersInPlot().forEach(playerInPlot -> plot.teleportPlayer(playerInPlot, TeleportCause.COMMAND, + plot.getPlayersInPlot().forEach(playerInPlot -> plot.teleportPlayer(playerInPlot, TeleportCause.COMMAND_DELETE, result -> { } )); diff --git a/Core/src/main/java/com/plotsquared/core/command/Deny.java b/Core/src/main/java/com/plotsquared/core/command/Deny.java index 69d7b434c..ce80a649e 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Deny.java +++ b/Core/src/main/java/com/plotsquared/core/command/Deny.java @@ -30,6 +30,7 @@ import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.database.DBFunc; +import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.location.Location; import com.plotsquared.core.permissions.Permission; import com.plotsquared.core.player.PlotPlayer; @@ -183,10 +184,10 @@ public class Deny extends SubCommand { player.kick("You got kicked from the plot! This server did not set up a loaded spawn, so you got " + "kicked from the server."); } else { - player.teleport(newSpawn); + player.teleport(newSpawn, TeleportCause.DENIED); } } else { - player.teleport(spawn); + player.teleport(spawn, TeleportCause.DENIED); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java b/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java index e0b47358e..dc10b60d1 100644 --- a/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/HomeCommand.java @@ -85,7 +85,7 @@ public class HomeCommand extends Command { return; } Plot plot = plots.get(page - 1); - confirm.run(this, () -> plot.teleportPlayer(player, TeleportCause.COMMAND, result -> { + confirm.run(this, () -> plot.teleportPlayer(player, TeleportCause.COMMAND_HOME, result -> { if (result) { whenDone.run(this, CommandResult.SUCCESS); } else { diff --git a/Core/src/main/java/com/plotsquared/core/command/Like.java b/Core/src/main/java/com/plotsquared/core/command/Like.java index 32b9abf79..82303fd32 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Like.java +++ b/Core/src/main/java/com/plotsquared/core/command/Like.java @@ -113,7 +113,7 @@ public class Like extends SubCommand { for (final Plot plot : plots) { if ((!Settings.Done.REQUIRED_FOR_RATINGS || DoneFlag.isDone(plot)) && plot .isBasePlot() && (!plot.getLikes().containsKey(uuid))) { - plot.teleportPlayer(player, TeleportCause.COMMAND, result -> { + plot.teleportPlayer(player, TeleportCause.COMMAND_LIKE, result -> { }); player.sendMessage(TranslatableCaption.of("tutorial.rate_this")); return true; diff --git a/Core/src/main/java/com/plotsquared/core/command/Middle.java b/Core/src/main/java/com/plotsquared/core/command/Middle.java index 5c2837ddb..6acb04254 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Middle.java +++ b/Core/src/main/java/com/plotsquared/core/command/Middle.java @@ -49,7 +49,7 @@ public class Middle extends SubCommand { player.sendMessage(TranslatableCaption.of("errors.not_in_plot")); return false; } - plot.getCenter(center -> player.teleport(center, TeleportCause.COMMAND)); + plot.getCenter(center -> player.teleport(center, TeleportCause.COMMAND_MIDDLE)); player.sendMessage(TranslatableCaption.of("teleport.teleported_to_plot")); return true; } diff --git a/Core/src/main/java/com/plotsquared/core/command/Rate.java b/Core/src/main/java/com/plotsquared/core/command/Rate.java index 9f685af54..de22f4ce6 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Rate.java +++ b/Core/src/main/java/com/plotsquared/core/command/Rate.java @@ -106,7 +106,7 @@ public class Rate extends SubCommand { if ((!Settings.Done.REQUIRED_FOR_RATINGS || DoneFlag.isDone(p)) && p .isBasePlot() && (!p.getRatings().containsKey(uuid)) && !p .isAdded(uuid)) { - p.teleportPlayer(player, TeleportCause.COMMAND, result -> { + p.teleportPlayer(player, TeleportCause.COMMAND_RATE, result -> { }); player.sendMessage(TranslatableCaption.of("tutorial.rate_this")); return true; diff --git a/Core/src/main/java/com/plotsquared/core/command/Template.java b/Core/src/main/java/com/plotsquared/core/command/Template.java index c169f4d09..a12cb35c2 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Template.java +++ b/Core/src/main/java/com/plotsquared/core/command/Template.java @@ -240,7 +240,7 @@ public class Template extends SubCommand { this.setupUtils.setupWorld(builder); TaskManager.runTask(() -> { - player.teleport(this.worldUtil.getSpawn(world), TeleportCause.COMMAND); + player.teleport(this.worldUtil.getSpawn(world), TeleportCause.COMMAND_TEMPLATE); player.sendMessage(TranslatableCaption.of("setup.setup_finished")); }); return true; diff --git a/Core/src/main/java/com/plotsquared/core/command/Visit.java b/Core/src/main/java/com/plotsquared/core/command/Visit.java index dbd8cea61..d71c30789 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Visit.java +++ b/Core/src/main/java/com/plotsquared/core/command/Visit.java @@ -166,7 +166,7 @@ public class Visit extends Command { } } - confirm.run(this, () -> plot.teleportPlayer(player, TeleportCause.COMMAND, result -> { + confirm.run(this, () -> plot.teleportPlayer(player, TeleportCause.COMMAND_VISIT, result -> { if (result) { whenDone.run(Visit.this, CommandResult.SUCCESS); } else { diff --git a/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotEvent.java index bba7b8a60..c8bb229be 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotEvent.java @@ -27,6 +27,7 @@ package com.plotsquared.core.events; import com.plotsquared.core.command.Claim; import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; import org.checkerframework.checker.nullness.qual.Nullable; @@ -39,8 +40,8 @@ public class PlayerAutoPlotEvent extends PlotEvent implements CancellablePlotEve private final PlotArea plotArea; private Result eventResult; private String schematic; - private int size_x; - private int size_z; + private int sizeX; + private int sizeZ; /** * PlayerAutoPlotEvent: called when a player attempts to auto claim a plot. @@ -48,19 +49,31 @@ public class PlayerAutoPlotEvent extends PlotEvent implements CancellablePlotEve * @param player The player attempting to auto claim * @param plotArea The applicable plot area * @param schematic The schematic defined or null - * @param size_x The size of the auto area - * @param size_z The size of the auto area + * @param sizeX The size of the auto area + * @param sizeZ The size of the auto area */ public PlayerAutoPlotEvent( PlotPlayer player, PlotArea plotArea, @Nullable String schematic, - int size_x, int size_z + int sizeX, int sizeZ ) { super(null); this.player = player; this.plotArea = plotArea; this.schematic = schematic; - this.size_x = size_x; - this.size_z = size_z; + this.sizeX = sizeX; + this.sizeZ = sizeZ; + } + + /** + * Returns null as the plots to be claimed haven't been chosen yet. This will depend on the size of the auto + * ({@link PlayerAutoPlotEvent#setSizeX(int)} and {@link PlayerAutoPlotEvent#setSizeZ(int)}). To see which plots have been + * chosen, see {@link PlayerAutoPlotsChosenEvent}. + * + * @return null + */ + @Override + public @Nullable Plot getPlot() { + return null; } /** @@ -99,20 +112,72 @@ public class PlayerAutoPlotEvent extends PlotEvent implements CancellablePlotEve return this.plotArea; } + /** + * @deprecated for removal. Use {@link PlayerAutoPlotEvent#getSizeX()} + */ + @Deprecated(forRemoval = true) public int getSize_x() { - return this.size_x; + return getSizeX(); } - public void setSize_x(int size_x) { - this.size_x = size_x; + /** + * @deprecated for removal. Use {@link PlayerAutoPlotEvent#setSizeX(int)} )} + */ + @Deprecated(forRemoval = true) + public void setSize_x(int sizeX) { + setSizeX(sizeX); } + /** + * @deprecated for removal. Use {@link PlayerAutoPlotEvent#getSizeZ()} + */ + @Deprecated(forRemoval = true) public int getSize_z() { - return this.size_z; + return getSizeZ(); } - public void setSize_z(int size_z) { - this.size_z = size_z; + /** + * @deprecated for removal. Use {@link PlayerAutoPlotEvent#setSizeZ(int)} )} + */ + @Deprecated(forRemoval = true) + public void setSize_z(int sizeZ) { + setSizeZ(sizeZ); + } + + /** + * Get the x size of the auto-area + * + * @return x size + */ + public int getSizeX() { + return this.sizeX; + } + + /** + * Set the x size of the auto-area + * + * @param sizeX x size + */ + public void setSizeX(int sizeX) { + this.sizeX = sizeX; + } + + /** + * Get the z size of the auto-area + * + * @return z size + */ + public int getSizeZ() { + return this.sizeZ; + } + + /** + * Set the z size of the auto-area + * + * @param sizeZ z size + */ + public void setSizeZ(int sizeZ) { + this.sizeZ = sizeZ; } } diff --git a/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotsChosenEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotsChosenEvent.java new file mode 100644 index 000000000..c1daf6e00 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/events/PlayerAutoPlotsChosenEvent.java @@ -0,0 +1,86 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2021 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 + * along with this program. If not, see . + */ +package com.plotsquared.core.events; + +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Collections; +import java.util.List; + +/** + * Event fired when the plots that are to be claimed by a player executing a /plot auto have been chosen. It contains an + * unmodifiable list of the plots selected. This may be of length 0. This event is effectively cancellable by setting the list + * of plots to an empty list. + */ +public class PlayerAutoPlotsChosenEvent extends PlotPlayerEvent { + + private Result eventResult; + + private List plots; + + /** + * PlayerAutoPlotsChosenEvent: Called when one or more plots are chosen for a /plot auto + * + * @param player Player that executed the auto + * @param plots Plots that have been chosen to be set to the player + */ + public PlayerAutoPlotsChosenEvent(PlotPlayer player, List plots) { + super(player, plots.size() > 0 ? plots.get(0) : null); + this.plots = Collections.unmodifiableList(plots); + } + + /** + * Returns the plot at index 0 in the list of plots selected. May be null if the list was of length 0. + * + * @return plot at index 0 or null. + */ + @Override + public @Nullable Plot getPlot() { + return super.getPlot(); + } + + /** + * Get the immutable list of plots selected to be claimed by the player. May be of length 0. + * + * @return immutable list. + */ + public @NonNull List getPlots() { + return plots; + } + + /** + * Set the plots to be claimed by the player. + * + * @param plots list of plots. + */ + public void setPlots(final @NonNull List plots) { + this.plots = List.copyOf(plots); + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/events/PlayerTeleportToPlotEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlayerTeleportToPlotEvent.java index b6ae7123b..acf74f957 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlayerTeleportToPlotEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlayerTeleportToPlotEvent.java @@ -35,18 +35,39 @@ import com.plotsquared.core.plot.Plot; public class PlayerTeleportToPlotEvent extends PlotPlayerEvent implements CancellablePlotEvent { private final Location from; + private final TeleportCause cause; private Result eventResult; + /** + * @deprecated use {@link PlayerTeleportToPlotEvent#PlayerTeleportToPlotEvent(PlotPlayer, Location, Plot, TeleportCause)}. + * You should not be creating events in the first place. + */ + @Deprecated(forRemoval = true) + public PlayerTeleportToPlotEvent(PlotPlayer player, Location from, Plot plot) { + this(player, from, plot, TeleportCause.UNKNOWN); + } + /** * PlayerTeleportToPlotEvent: Called when a player teleports to a plot * * @param player That was teleported * @param from Start location * @param plot Plot to which the player was teleported + * @param cause Why the teleport is being completed */ - public PlayerTeleportToPlotEvent(PlotPlayer player, Location from, Plot plot) { + public PlayerTeleportToPlotEvent(PlotPlayer player, Location from, Plot plot, TeleportCause cause) { super(player, plot); this.from = from; + this.cause = cause; + } + + /** + * Get the teleport cause + * + * @return TeleportCause + */ + public TeleportCause getCause() { + return cause; } /** diff --git a/Core/src/main/java/com/plotsquared/core/events/PlotClaimedNotifyEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlotClaimedNotifyEvent.java new file mode 100644 index 000000000..9b7767148 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/events/PlotClaimedNotifyEvent.java @@ -0,0 +1,57 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2021 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 + * along with this program. If not, see . + */ +package com.plotsquared.core.events; + +import com.plotsquared.core.plot.Plot; + +/** + * To be used as a notification that a plot has been claimed. For cancelling events, see {@link PlayerClaimPlotEvent} + */ +public class PlotClaimedNotifyEvent extends PlotEvent { + + private final boolean auto; + + /** + * New event instance. + * + * @param plot Plot that was claimed + * @param auto If the plot was claimed using /plot auto + */ + public PlotClaimedNotifyEvent(Plot plot, boolean auto) { + super(plot); + this.auto = auto; + } + + /** + * If the plot was claimed using /plot auto + * + * @return if claimed with auto + */ + public boolean wasAuto() { + return auto; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/events/PlotEvent.java b/Core/src/main/java/com/plotsquared/core/events/PlotEvent.java index d13dbc3ac..44de9c1dc 100644 --- a/Core/src/main/java/com/plotsquared/core/events/PlotEvent.java +++ b/Core/src/main/java/com/plotsquared/core/events/PlotEvent.java @@ -42,7 +42,7 @@ public abstract class PlotEvent { * * @return Plot */ - public final Plot getPlot() { + public Plot getPlot() { return this.plot; } diff --git a/Core/src/main/java/com/plotsquared/core/events/TeleportCause.java b/Core/src/main/java/com/plotsquared/core/events/TeleportCause.java index 87b827d25..403fd6ca4 100644 --- a/Core/src/main/java/com/plotsquared/core/events/TeleportCause.java +++ b/Core/src/main/java/com/plotsquared/core/events/TeleportCause.java @@ -25,8 +25,48 @@ */ package com.plotsquared.core.events; +import com.google.common.collect.Sets; + +import java.util.EnumSet; +import java.util.Set; + +/** + * The reason for an internal player teleport. + */ public enum TeleportCause { + COMMAND, + COMMAND_AREA_CREATE, + COMMAND_AREA_TELEPORT, + COMMAND_AUTO, + COMMAND_CLAIM, + COMMAND_CLEAR, + COMMAND_CLUSTER_TELEPORT, + COMMAND_DELETE, + COMMAND_HOME, + COMMAND_LIKE, + COMMAND_MIDDLE, + COMMAND_RATE, + COMMAND_SETUP, + COMMAND_TEMPLATE, + COMMAND_VISIT, + DEATH, + DENIED, + KICK, + LOGIN, PLUGIN, - UNKNOWN + UNKNOWN; + + public static final class CauseSets { + + public static final Set COMMAND = Sets.immutableEnumSet(EnumSet.range( + TeleportCause.COMMAND, + TeleportCause.COMMAND_VISIT + )); + public static final Set PLUGIN = Sets.immutableEnumSet(EnumSet.range( + TeleportCause.DEATH, + TeleportCause.PLUGIN + )); + + } } diff --git a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java index 81ca8eb20..ee5ae277f 100644 --- a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java @@ -473,7 +473,7 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, */ public void plotkick(Location location) { setMeta("kick", true); - teleport(location); + teleport(location, TeleportCause.KICK); deleteMeta("kick"); } @@ -712,7 +712,7 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, if (plot.isLoaded()) { TaskManager.runTask(() -> { if (getMeta("teleportOnLogin", true)) { - teleport(location); + teleport(location, TeleportCause.LOGIN); sendMessage( TranslatableCaption.of("teleport.teleported_to_plot")); } @@ -724,7 +724,7 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, result -> TaskManager.runTask(() -> { if (getMeta("teleportOnLogin", true)) { if (plot.isLoaded()) { - teleport(location); + teleport(location, TeleportCause.LOGIN); sendMessage(TranslatableCaption .of("teleport.teleported_to_plot")); } diff --git a/Core/src/main/java/com/plotsquared/core/plot/Plot.java b/Core/src/main/java/com/plotsquared/core/plot/Plot.java index 6eb6de683..65a6d8926 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/Plot.java +++ b/Core/src/main/java/com/plotsquared/core/plot/Plot.java @@ -1666,6 +1666,7 @@ public class Plot { return base.settings != null && base.settings.getRatings() != null; } + @Deprecated(forRemoval = true) public boolean claim(final @NonNull PlotPlayer player, boolean teleport, String schematic) { if (!canClaim(player)) { return false; @@ -1673,8 +1674,26 @@ public class Plot { return claim(player, teleport, schematic, true); } + @Deprecated(forRemoval = true) public boolean claim(final @NonNull PlotPlayer player, boolean teleport, String schematic, boolean updateDB) { + return claim(player, teleport, schematic, updateDB, false); + } + /** + * 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 + */ + public boolean claim( + final @NonNull 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(), @@ -1689,7 +1708,7 @@ public class Plot { this.getPlotModificationManager().setSign(player.getName()); player.sendMessage(TranslatableCaption.of("working.claimed"), Template.of("plot", this.getId().toString())); if (teleport && Settings.Teleport.ON_CLAIM) { - teleportPlayer(player, TeleportCause.COMMAND, result -> { + teleportPlayer(player, auto ? TeleportCause.COMMAND_AUTO : TeleportCause.COMMAND_CLAIM, result -> { }); } PlotArea plotworld = getArea(); @@ -2570,7 +2589,7 @@ public class Plot { */ public void teleportPlayer(final PlotPlayer player, TeleportCause cause, Consumer resultConsumer) { Plot plot = this.getBasePlot(false); - Result result = this.eventDispatcher.callTeleport(player, player.getLocation(), plot).getEventResult(); + Result result = this.eventDispatcher.callTeleport(player, player.getLocation(), plot, cause).getEventResult(); if (result == Result.DENY) { player.sendMessage( TranslatableCaption.of("events.event_denied"), diff --git a/Core/src/main/java/com/plotsquared/core/setup/CommonSetupSteps.java b/Core/src/main/java/com/plotsquared/core/setup/CommonSetupSteps.java index d235d5479..28f46249d 100644 --- a/Core/src/main/java/com/plotsquared/core/setup/CommonSetupSteps.java +++ b/Core/src/main/java/com/plotsquared/core/setup/CommonSetupSteps.java @@ -230,7 +230,7 @@ public enum CommonSetupSteps implements SetupStep { world = builder.setupManager().setupWorld(builder); } try { - plotPlayer.teleport(PlotSquared.platform().worldUtil().getSpawn(world), TeleportCause.COMMAND); + plotPlayer.teleport(PlotSquared.platform().worldUtil().getSpawn(world), TeleportCause.COMMAND_SETUP); } catch (Exception e) { plotPlayer.sendMessage(TranslatableCaption.of("errors.error_console")); e.printStackTrace(); diff --git a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java index 88ac22b43..93f04c2f2 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -29,6 +29,7 @@ import com.google.common.eventbus.EventBus; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.events.PlayerAutoPlotEvent; +import com.plotsquared.core.events.PlayerAutoPlotsChosenEvent; import com.plotsquared.core.events.PlayerClaimPlotEvent; import com.plotsquared.core.events.PlayerEnterPlotEvent; import com.plotsquared.core.events.PlayerLeavePlotEvent; @@ -38,6 +39,7 @@ import com.plotsquared.core.events.PlayerPlotTrustedEvent; import com.plotsquared.core.events.PlayerTeleportToPlotEvent; import com.plotsquared.core.events.PlotAutoMergeEvent; import com.plotsquared.core.events.PlotChangeOwnerEvent; +import com.plotsquared.core.events.PlotClaimedNotifyEvent; import com.plotsquared.core.events.PlotClearEvent; import com.plotsquared.core.events.PlotComponentSetEvent; import com.plotsquared.core.events.PlotDeleteEvent; @@ -48,6 +50,7 @@ import com.plotsquared.core.events.PlotFlagRemoveEvent; import com.plotsquared.core.events.PlotMergeEvent; import com.plotsquared.core.events.PlotRateEvent; import com.plotsquared.core.events.PlotUnlinkEvent; +import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.listener.PlayerBlockEventType; import com.plotsquared.core.location.Direction; import com.plotsquared.core.location.Location; @@ -75,11 +78,13 @@ import com.sk89q.worldedit.world.block.BlockTypes; import net.kyori.adventure.text.minimessage.Template; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; +import org.jetbrains.annotations.ApiStatus; import java.util.ArrayList; import java.util.List; import java.util.UUID; +@ApiStatus.Internal public class EventDispatcher { private final EventBus eventBus = new EventBus("PlotSquaredEvents"); @@ -130,8 +135,23 @@ public class EventDispatcher { return event; } - public PlayerTeleportToPlotEvent callTeleport(PlotPlayer player, Location from, Plot plot) { - PlayerTeleportToPlotEvent event = new PlayerTeleportToPlotEvent(player, from, plot); + public PlayerAutoPlotsChosenEvent callAutoPlotsChosen( + PlotPlayer player, List plots + ) { + PlayerAutoPlotsChosenEvent event = + new PlayerAutoPlotsChosenEvent(player, plots); + callEvent(event); + return event; + } + + public PlotClaimedNotifyEvent callPlotClaimedNotify(Plot plot, boolean auto) { + PlotClaimedNotifyEvent event = new PlotClaimedNotifyEvent(plot, auto); + callEvent(event); + return event; + } + + public PlayerTeleportToPlotEvent callTeleport(PlotPlayer player, Location from, Plot plot, TeleportCause cause) { + PlayerTeleportToPlotEvent event = new PlayerTeleportToPlotEvent(player, from, plot, cause); callEvent(event); return event; } @@ -263,7 +283,7 @@ public class EventDispatcher { final Plot plot = player.getCurrentPlot(); if (Settings.Teleport.ON_LOGIN && plot != null && !(plot .getArea() instanceof SinglePlotArea)) { - TaskManager.runTask(() -> plot.teleportPlayer(player, result -> { + TaskManager.runTask(() -> plot.teleportPlayer(player, TeleportCause.LOGIN, result -> { })); player.sendMessage(TranslatableCaption.of("teleport.teleported_to_road")); } @@ -272,7 +292,7 @@ public class EventDispatcher { public void doRespawnTask(final PlotPlayer player) { final Plot plot = player.getCurrentPlot(); if (Settings.Teleport.ON_DEATH && plot != null) { - TaskManager.runTask(() -> plot.teleportPlayer(player, result -> { + TaskManager.runTask(() -> plot.teleportPlayer(player, TeleportCause.DEATH, result -> { })); player.sendMessage(TranslatableCaption.of("teleport.teleported_to_road")); } diff --git a/Core/src/main/java/com/plotsquared/core/util/task/AutoClaimFinishTask.java b/Core/src/main/java/com/plotsquared/core/util/task/AutoClaimFinishTask.java index 64dabc48a..301ea50c0 100644 --- a/Core/src/main/java/com/plotsquared/core/util/task/AutoClaimFinishTask.java +++ b/Core/src/main/java/com/plotsquared/core/util/task/AutoClaimFinishTask.java @@ -68,7 +68,7 @@ public final class AutoClaimFinishTask implements Callable { player.sendMessage(TranslatableCaption.of("errors.no_free_plots")); return false; } - plot.claim(player, true, schematic, false); + plot.claim(player, true, schematic, false, true); if (area.isAutoMerge()) { PlotMergeEvent event = this.eventDispatcher.callMerge(plot, Direction.ALL, Integer.MAX_VALUE, player); if (event.getEventResult() == Result.DENY) { diff --git a/build.gradle.kts b/build.gradle.kts index 721c918cc..a023acd28 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -16,7 +16,7 @@ plugins { idea } -var ver by extra("6.0.10") +var ver by extra("6.1.0") var versuffix by extra("-SNAPSHOT") val versionsuffix: String? by project if (versionsuffix != null) {