From 9e9b21779c91c414d8be9d7c05f91415a28c06f1 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Wed, 19 Sep 2018 16:57:33 +0100 Subject: [PATCH] Add PlotChangeOwner Event --- .../bukkit/events/PlotChangeOwnerEvent.java | 107 +++++++++++++++++ .../bukkit/util/BukkitEventUtil.java | 22 +--- .../plot/commands/Owner.java | 25 ++-- .../intellectualcrafters/plot/config/C.java | 1 + .../plot/object/Plot.java | 31 +++++ .../plot/util/EventUtil.java | 2 + .../plot/util/EventUtilTest.java | 2 + .../nukkit/events/PlotChangeOwnerEvent.java | 108 ++++++++++++++++++ .../nukkit/util/NukkitEventUtil.java | 22 +--- .../sponge/events/PlotChangeOwnerEvent.java | 97 ++++++++++++++++ .../sponge/util/SpongeEventUtil.java | 24 ++-- 11 files changed, 379 insertions(+), 62 deletions(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotChangeOwnerEvent.java create mode 100644 Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotChangeOwnerEvent.java create mode 100644 Sponge/src/main/java/com/plotsquared/sponge/events/PlotChangeOwnerEvent.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotChangeOwnerEvent.java b/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotChangeOwnerEvent.java new file mode 100644 index 000000000..81dea45a3 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotChangeOwnerEvent.java @@ -0,0 +1,107 @@ +package com.plotsquared.bukkit.events; + +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotId; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +import java.util.UUID; + +public class PlotChangeOwnerEvent extends PlotEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private final Player initiator; + private final UUID newOwner; + private final UUID oldOwner; + private boolean hasOldOwner; + private boolean cancelled; + + /** + * PlotChangeOwnerEvent: Called when a plot's owner is change. + * + * @param newOwner The new owner of the plot + * @param oldOwner The old owner of the plot + * @param plot The plot having its owner changed + */ + public PlotChangeOwnerEvent(Player initiator, Plot plot, UUID oldOwner, UUID newOwner, boolean hasOldOwner) { + super(plot); + this.initiator = initiator; + this.newOwner = newOwner; + this.oldOwner = oldOwner; + this.hasOldOwner = hasOldOwner; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Get the PlotId. + * + * @return PlotId + */ + public PlotId getPlotId() { + return getPlot().getId(); + } + + /** + * Get the world name. + * + * @return String + */ + public String getWorld() { + return getPlot().getWorldName(); + } + + /** + * Get the change-owner initator + * + * @return Player + */ + public Player getInitiator() { + return this.initiator; + } + + /** + * Get the old owner of the plot. Null if not exists. + * + * @return UUID + */ + public UUID getOldOwner() { + return this.oldOwner; + } + + /** + * Get the new owner of the plot + * + * @return UUID + */ + public UUID getNewOwner() { + return this.newOwner; + } + + /** + * Get if the plot had an old owner + * + * @return boolean + */ + public boolean hasOldOwner() { + return this.hasOldOwner; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean b) { + this.cancelled = b; + } +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEventUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEventUtil.java index 0a0a6c644..6564c6dc7 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEventUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEventUtil.java @@ -9,22 +9,7 @@ import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.Rating; import com.intellectualcrafters.plot.util.EventUtil; -import com.plotsquared.bukkit.events.ClusterFlagRemoveEvent; -import com.plotsquared.bukkit.events.PlayerClaimPlotEvent; -import com.plotsquared.bukkit.events.PlayerEnterPlotEvent; -import com.plotsquared.bukkit.events.PlayerLeavePlotEvent; -import com.plotsquared.bukkit.events.PlayerPlotDeniedEvent; -import com.plotsquared.bukkit.events.PlayerPlotHelperEvent; -import com.plotsquared.bukkit.events.PlayerPlotTrustedEvent; -import com.plotsquared.bukkit.events.PlayerTeleportToPlotEvent; -import com.plotsquared.bukkit.events.PlotClearEvent; -import com.plotsquared.bukkit.events.PlotComponentSetEvent; -import com.plotsquared.bukkit.events.PlotDeleteEvent; -import com.plotsquared.bukkit.events.PlotFlagAddEvent; -import com.plotsquared.bukkit.events.PlotFlagRemoveEvent; -import com.plotsquared.bukkit.events.PlotMergeEvent; -import com.plotsquared.bukkit.events.PlotRateEvent; -import com.plotsquared.bukkit.events.PlotUnlinkEvent; +import com.plotsquared.bukkit.events.*; import com.plotsquared.bukkit.object.BukkitPlayer; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -119,6 +104,11 @@ public class BukkitEventUtil extends EventUtil { callEvent(new PlayerPlotHelperEvent(getPlayer(initiator), plot, player, added)); } + @Override + public boolean callOwnerChange(PlotPlayer initiator, Plot plot, UUID oldOwner, UUID newOwner, boolean hasOldOwner) { + return callEvent(new PlotChangeOwnerEvent(getPlayer(initiator), plot, oldOwner, newOwner, hasOldOwner)); + } + @Override public boolean callFlagRemove(Flag flag, Object object, PlotCluster cluster) { return callEvent(new ClusterFlagRemoveEvent(flag, cluster)); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Owner.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Owner.java index f73cb5c61..26a4e413f 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Owner.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Owner.java @@ -4,11 +4,7 @@ import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; -import com.intellectualcrafters.plot.util.CmdConfirm; -import com.intellectualcrafters.plot.util.MainUtil; -import com.intellectualcrafters.plot.util.Permissions; -import com.intellectualcrafters.plot.util.TaskManager; -import com.intellectualcrafters.plot.util.UUIDHandler; +import com.intellectualcrafters.plot.util.*; import com.plotsquared.general.commands.CommandDeclaration; import java.util.Set; @@ -34,7 +30,8 @@ public class Owner extends SetCommand { try { uuid = UUID.fromString(value); name = MainUtil.getName(uuid); - } catch (Exception ignored) {} + } catch (Exception ignored) { + } } else { uuid = UUIDHandler.getUUID(value, null); name = UUIDHandler.getName(uuid); @@ -80,13 +77,15 @@ public class Owner extends SetCommand { Runnable run = new Runnable() { @Override public void run() { - if (removeDenied) plot.removeDenied(finalUUID); - plot.setOwner(finalUUID); - plot.setSign(finalName); - MainUtil.sendMessage(player, C.SET_OWNER); - if (other != null) { - MainUtil.sendMessage(other, C.NOW_OWNER, plot.getArea() + ";" + plot.getId()); - } + if (plot.setOwner(finalUUID, player)) { + if (removeDenied) plot.removeDenied(finalUUID); + plot.setSign(finalName); + MainUtil.sendMessage(player, C.SET_OWNER); + if (other != null) { + MainUtil.sendMessage(other, C.NOW_OWNER, plot.getArea() + ";" + plot.getId()); + } + } else + MainUtil.sendMessage(player, C.SET_OWNER_CANCELLED); } }; if (hasConfirmation(player)) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/config/C.java b/Core/src/main/java/com/intellectualcrafters/plot/config/C.java index e7d84ff5e..8027ea622 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -692,6 +692,7 @@ public enum C { * Set Owner */ SET_OWNER("$4You successfully set the plot owner", "Owner"), + SET_OWNER_CANCELLED("$2The setowner action was cancelled", "Owner"), NOW_OWNER("$4You are now owner of plot %s", "Owner"), /* * Signs diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java b/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java index e67a98ce3..658613dbc 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/Plot.java @@ -746,6 +746,37 @@ public class Plot { } } + /** + * Set the plot owner (and update the database) + * @param owner + * @param initiator + * @return boolean + */ + public boolean setOwner(UUID owner, PlotPlayer initiator) { + boolean result = EventUtil.manager.callOwnerChange(initiator, this, owner, hasOwner() ? this.owner : null, hasOwner()); + if(!result) + return false; + if (!hasOwner()) { + this.owner = owner; + create(); + return true; + } + if (!isMerged()) { + if (!this.owner.equals(owner)) { + this.owner = owner; + DBFunc.setOwner(this, owner); + } + return true; + } + for (Plot current : getConnectedPlots()) { + if (!owner.equals(current.owner)) { + current.owner = owner; + DBFunc.setOwner(current, owner); + } + } + return true; + } + /** * Clear a plot. * @see this#clear(boolean, boolean, Runnable) diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/EventUtil.java b/Core/src/main/java/com/intellectualcrafters/plot/util/EventUtil.java index c1117c5a0..b832e3f82 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/EventUtil.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/EventUtil.java @@ -67,6 +67,8 @@ public abstract class EventUtil { public abstract void callMember(PlotPlayer initiator, Plot plot, UUID player, boolean added); + public abstract boolean callOwnerChange(PlotPlayer initiator, Plot plot, UUID newOwner, UUID oldOwner, boolean hasOldOwner); + public void doJoinTask(final PlotPlayer player) { if (player == null) { return; //possible future warning message to figure out where we are retrieving null diff --git a/Core/src/test/java/com/intellectualcrafters/plot/util/EventUtilTest.java b/Core/src/test/java/com/intellectualcrafters/plot/util/EventUtilTest.java index 314f7572a..a8b83c2b5 100644 --- a/Core/src/test/java/com/intellectualcrafters/plot/util/EventUtilTest.java +++ b/Core/src/test/java/com/intellectualcrafters/plot/util/EventUtilTest.java @@ -63,4 +63,6 @@ public class EventUtilTest extends EventUtil { @Override public void callTrusted(PlotPlayer initiator, Plot plot, UUID player, boolean added) {} @Override public void callMember(PlotPlayer initiator, Plot plot, UUID player, boolean added) {} + + @Override public boolean callOwnerChange(PlotPlayer initiator, Plot plot, UUID newOwner, UUID oldOwner, boolean hasOldOwner) {return false;} } diff --git a/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotChangeOwnerEvent.java b/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotChangeOwnerEvent.java new file mode 100644 index 000000000..e2988519e --- /dev/null +++ b/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotChangeOwnerEvent.java @@ -0,0 +1,108 @@ +package com.plotsquared.nukkit.events; + +import cn.nukkit.Player; +import cn.nukkit.event.Cancellable; +import cn.nukkit.event.HandlerList; +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotId; + +import java.util.UUID; + +public class PlotChangeOwnerEvent extends PlotEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private final Plot plot; + private final Player initiator; + private final UUID newOwner; + private final UUID oldOwner; + private final boolean hasOldOwner; + private boolean cancelled; + + /** + * PlotChangeOwnerEvent: Called when a plot's owner is change. + * + * @param newOwner The new owner of the plot + * @param oldOwner The old owner of the plot + * @param plot The plot having its owner changed + */ + public PlotChangeOwnerEvent(Player initiator, Plot plot, UUID oldOwner, UUID newOwner, boolean hasOldOwner) { + super(plot); + this.plot = plot; + this.initiator = initiator; + this.newOwner = newOwner; + this.hasOldOwner = hasOldOwner; + this.oldOwner = oldOwner; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Get the PlotId. + * + * @return PlotId + */ + public PlotId getPlotId() { + return getPlot().getId(); + } + + /** + * Get the world name. + * + * @return String + */ + public String getWorld() { + return getPlot().getWorldName(); + } + + /** + * Get the change-owner initator + * + * @return Player + */ + public Player getInitiator() { + return this.initiator; + } + + /** + * Get the old owner of the plot + * + * @return UUID + */ + public UUID getOldOwner() { + return this.oldOwner; + } + + /** + * Get the new owner of the plot + * + * @return UUID + */ + public UUID getNewOwner() { + return this.newOwner; + } + + /** + * Get if the plot had an old owner + * + * @return boolean + */ + public boolean hasOldOwner() { + return this.hasOldOwner; + } + + public HandlerList getHandlers() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean b) { + this.cancelled = b; + } +} diff --git a/Nukkit/src/main/java/com/plotsquared/nukkit/util/NukkitEventUtil.java b/Nukkit/src/main/java/com/plotsquared/nukkit/util/NukkitEventUtil.java index 0168f63db..c8be83cef 100644 --- a/Nukkit/src/main/java/com/plotsquared/nukkit/util/NukkitEventUtil.java +++ b/Nukkit/src/main/java/com/plotsquared/nukkit/util/NukkitEventUtil.java @@ -13,22 +13,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.Rating; import com.intellectualcrafters.plot.util.EventUtil; import com.plotsquared.nukkit.NukkitMain; -import com.plotsquared.nukkit.events.ClusterFlagRemoveEvent; -import com.plotsquared.nukkit.events.PlayerClaimPlotEvent; -import com.plotsquared.nukkit.events.PlayerEnterPlotEvent; -import com.plotsquared.nukkit.events.PlayerLeavePlotEvent; -import com.plotsquared.nukkit.events.PlayerPlotDeniedEvent; -import com.plotsquared.nukkit.events.PlayerPlotHelperEvent; -import com.plotsquared.nukkit.events.PlayerPlotTrustedEvent; -import com.plotsquared.nukkit.events.PlayerTeleportToPlotEvent; -import com.plotsquared.nukkit.events.PlotClearEvent; -import com.plotsquared.nukkit.events.PlotComponentSetEvent; -import com.plotsquared.nukkit.events.PlotDeleteEvent; -import com.plotsquared.nukkit.events.PlotFlagAddEvent; -import com.plotsquared.nukkit.events.PlotFlagRemoveEvent; -import com.plotsquared.nukkit.events.PlotMergeEvent; -import com.plotsquared.nukkit.events.PlotRateEvent; -import com.plotsquared.nukkit.events.PlotUnlinkEvent; +import com.plotsquared.nukkit.events.*; import com.plotsquared.nukkit.object.NukkitPlayer; import javax.annotation.Nullable; @@ -125,6 +110,11 @@ public class NukkitEventUtil extends EventUtil { callEvent(new PlayerPlotHelperEvent(getPlayer(initiator), plot, player, added)); } + @Override + public boolean callOwnerChange(PlotPlayer initiator, Plot plot, UUID oldOwner, UUID newOwner, boolean hasOldOwner) { + return callEvent(new PlotChangeOwnerEvent(getPlayer(initiator), plot, oldOwner, newOwner, hasOldOwner)); + } + @Override public boolean callFlagRemove(Flag flag, Object object, PlotCluster cluster) { return callEvent(new ClusterFlagRemoveEvent(flag, cluster)); diff --git a/Sponge/src/main/java/com/plotsquared/sponge/events/PlotChangeOwnerEvent.java b/Sponge/src/main/java/com/plotsquared/sponge/events/PlotChangeOwnerEvent.java new file mode 100644 index 000000000..34b721498 --- /dev/null +++ b/Sponge/src/main/java/com/plotsquared/sponge/events/PlotChangeOwnerEvent.java @@ -0,0 +1,97 @@ +package com.plotsquared.sponge.events; + +import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotId; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Cancellable; + +import java.util.UUID; + +public class PlotChangeOwnerEvent extends PlotEvent implements Cancellable { + + private final Player initiator; + private final UUID newOwner; + private final UUID oldOwner; + private final boolean hasOldOwner; + private boolean cancelled; + + /** + * PlotChangeOwnerEvent: Called when a plot's owner is change. + * + * @param newOwner The new owner of the plot + * @param oldOwner The old owner of the plot + * @param plot The plot having its owner changed + */ + public PlotChangeOwnerEvent(Player initiator, Plot plot, UUID oldOwner, UUID newOwner, boolean hasOldOwner) { + super(plot); + this.initiator = initiator; + this.newOwner = newOwner; + this.oldOwner = oldOwner; + this.hasOldOwner = hasOldOwner; + } + + + /** + * Get the PlotId. + * + * @return PlotId + */ + public PlotId getPlotId() { + return getPlot().getId(); + } + + /** + * Get the world name. + * + * @return String + */ + public String getWorld() { + return getPlot().getWorldName(); + } + + /** + * Get the change-owner initator + * + * @return Player + */ + public Player getInitiator() { + return this.initiator; + } + + /** + * Get the old owner of the plot. Null if not exists. + * + * @return UUID + */ + public UUID getOldOwner() { + return this.oldOwner; + } + + /** + * Get the new owner of the plot + * + * @return UUID + */ + public UUID getNewOwner() { + return this.newOwner; + } + + /** + * Get if the plot had an old owner + * + * @return boolean + */ + public boolean hasOldOwner() { + return this.hasOldOwner; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean b) { + this.cancelled = b; + } +} diff --git a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeEventUtil.java b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeEventUtil.java index 5ef1a95d0..a6b13e85f 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeEventUtil.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeEventUtil.java @@ -10,22 +10,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.Rating; import com.intellectualcrafters.plot.util.EventUtil; import com.plotsquared.sponge.SpongeMain; -import com.plotsquared.sponge.events.ClusterFlagRemoveEvent; -import com.plotsquared.sponge.events.PlayerClaimPlotEvent; -import com.plotsquared.sponge.events.PlayerEnterPlotEvent; -import com.plotsquared.sponge.events.PlayerLeavePlotEvent; -import com.plotsquared.sponge.events.PlayerPlotDeniedEvent; -import com.plotsquared.sponge.events.PlayerPlotHelperEvent; -import com.plotsquared.sponge.events.PlayerPlotTrustedEvent; -import com.plotsquared.sponge.events.PlayerTeleportToPlotEvent; -import com.plotsquared.sponge.events.PlotClearEvent; -import com.plotsquared.sponge.events.PlotComponentSetEvent; -import com.plotsquared.sponge.events.PlotDeleteEvent; -import com.plotsquared.sponge.events.PlotFlagAddEvent; -import com.plotsquared.sponge.events.PlotFlagRemoveEvent; -import com.plotsquared.sponge.events.PlotMergeEvent; -import com.plotsquared.sponge.events.PlotRateEvent; -import com.plotsquared.sponge.events.PlotUnlinkEvent; +import com.plotsquared.sponge.events.*; import org.spongepowered.api.event.Event; import org.spongepowered.api.event.EventManager; @@ -114,7 +99,12 @@ public class SpongeEventUtil extends EventUtil { public void callMember(PlotPlayer initiator, Plot plot, UUID player, boolean added) { callEvent(new PlayerPlotHelperEvent(SpongeUtil.getPlayer(initiator), plot, player, added)); } - + + @Override + public boolean callOwnerChange(PlotPlayer initiator, Plot plot, UUID oldOwner, UUID newOwner, boolean hasOldOwner) { + return callEvent(new PlotChangeOwnerEvent(SpongeUtil.getPlayer(initiator), plot, oldOwner, newOwner, hasOldOwner)); + } + @Override public boolean callFlagRemove(Flag flag, Object object, PlotCluster cluster) { return callEvent(new ClusterFlagRemoveEvent(flag, cluster));