diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotRateEvent.java b/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotRateEvent.java index 29aef2de8..2c7bcb230 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotRateEvent.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/events/PlotRateEvent.java @@ -3,13 +3,15 @@ package com.plotsquared.bukkit.events; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.Rating; +import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; -public class PlotRateEvent extends PlotEvent { +public class PlotRateEvent extends PlotEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); private final PlotPlayer rater; private Rating rating; + private boolean cancelled = false; public PlotRateEvent(PlotPlayer rater, Rating rating, Plot plot) { super(plot); @@ -38,4 +40,13 @@ public class PlotRateEvent extends PlotEvent { return handlers; } + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } } 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 b004e6ff4..0a0a6c644 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEventUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEventUtil.java @@ -31,6 +31,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.UUID; @@ -124,10 +125,13 @@ public class BukkitEventUtil extends EventUtil { } @Override + @Nullable public Rating callRating(PlotPlayer player, Plot plot, Rating rating) { PlotRateEvent event = new PlotRateEvent(player, rating, plot); Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return null; + } return event.getRating(); } - } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Rate.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Rate.java index 3121f5c21..2c9090e68 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Rate.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Rate.java @@ -122,12 +122,15 @@ public class Rate extends SubCommand { if (index.getValue() >= Settings.Ratings.CATEGORIES.size()) { int rV = rating.getValue(); Rating result = EventUtil.manager.callRating(this.player, plot, new Rating(rV)); - plot.addRating(this.player.getUUID(), result); - sendMessage(this.player, C.RATING_APPLIED, plot.getId().toString()); - if (Permissions.hasPermission(this.player, C.PERMISSION_COMMENT)) { - Command command = MainCommand.getInstance().getCommand(Comment.class); - if (command != null) { - MainUtil.sendMessage(this.player, C.COMMENT_THIS, command.getUsage()); + if (result != null) { + plot.addRating(this.player.getUUID(), result); + sendMessage(this.player, C.RATING_APPLIED, plot.getId().toString()); + if (Permissions.hasPermission(this.player, C.PERMISSION_COMMENT)) { + Command command = MainCommand.getInstance().getCommand(Comment.class); + if (command != null) { + MainUtil.sendMessage(this.player, C.COMMENT_THIS, + command.getUsage()); + } } } return false; @@ -189,8 +192,10 @@ public class Rate extends SubCommand { return; } Rating result = EventUtil.manager.callRating(player, plot, new Rating(rating)); - plot.addRating(uuid, result); - sendMessage(player, C.RATING_APPLIED, plot.getId().toString()); + if (result != null) { + plot.addRating(uuid, result); + sendMessage(player, C.RATING_APPLIED, plot.getId().toString()); + } } }; if (plot.getSettings().ratings == null) { 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 33759c4cf..c1117c5a0 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/EventUtil.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/EventUtil.java @@ -18,6 +18,7 @@ import com.intellectualcrafters.plot.object.Rating; import com.intellectualcrafters.plot.util.expiry.ExpireManager; import com.plotsquared.listener.PlayerBlockEventType; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.HashSet; import java.util.UUID; @@ -26,6 +27,14 @@ public abstract class EventUtil { public static EventUtil manager = null; + /** + * Submit a plot rate event and return the updated rating + * @param player Player that rated the plot + * @param plot Plot that was rated + * @param rating Rating given to the plot + * @return Updated rating or null if the event was cancelled + */ + @Nullable public abstract Rating callRating(PlotPlayer player, Plot plot, Rating rating); public abstract boolean callClaim(PlotPlayer player, Plot plot, boolean auto); diff --git a/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotRateEvent.java b/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotRateEvent.java index de9a9f0b5..b9d048635 100644 --- a/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotRateEvent.java +++ b/Nukkit/src/main/java/com/plotsquared/nukkit/events/PlotRateEvent.java @@ -1,15 +1,17 @@ package com.plotsquared.nukkit.events; +import cn.nukkit.event.Cancellable; import cn.nukkit.event.HandlerList; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.Rating; -public class PlotRateEvent extends PlotEvent { +public class PlotRateEvent extends PlotEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); private final PlotPlayer rater; private Rating rating; + private boolean cancelled = false; public PlotRateEvent(PlotPlayer rater, Rating rating, Plot plot) { super(plot); @@ -33,4 +35,11 @@ public class PlotRateEvent extends PlotEvent { this.rating = rating; } + @Override public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @Override public boolean isCancelled() { + return this.cancelled; + } } 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 fdb1135ea..0168f63db 100644 --- a/Nukkit/src/main/java/com/plotsquared/nukkit/util/NukkitEventUtil.java +++ b/Nukkit/src/main/java/com/plotsquared/nukkit/util/NukkitEventUtil.java @@ -30,6 +30,8 @@ import com.plotsquared.nukkit.events.PlotMergeEvent; import com.plotsquared.nukkit.events.PlotRateEvent; import com.plotsquared.nukkit.events.PlotUnlinkEvent; import com.plotsquared.nukkit.object.NukkitPlayer; + +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.UUID; @@ -129,9 +131,13 @@ public class NukkitEventUtil extends EventUtil { } @Override + @Nullable public Rating callRating(PlotPlayer player, Plot plot, Rating rating) { PlotRateEvent event = new PlotRateEvent(player, rating, plot); plugin.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return null; + } return event.getRating(); } diff --git a/Sponge/src/main/java/com/plotsquared/sponge/events/PlotRateEvent.java b/Sponge/src/main/java/com/plotsquared/sponge/events/PlotRateEvent.java index 7c90f346b..5f576c24b 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/events/PlotRateEvent.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/events/PlotRateEvent.java @@ -3,11 +3,14 @@ package com.plotsquared.sponge.events; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.Rating; +import org.spongepowered.api.event.Cancellable; + +public class PlotRateEvent extends PlotEvent implements Cancellable { -public class PlotRateEvent extends PlotEvent { private final PlotPlayer rater; private Rating rating; - + private boolean cancelled = false; + public PlotRateEvent(final PlotPlayer rater, final Rating rating, final Plot plot) { super(plot); this.rater = rater; @@ -25,4 +28,14 @@ public class PlotRateEvent extends PlotEvent { public Rating getRating() { return rating; } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } } 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 48dfde521..5ef1a95d0 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeEventUtil.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeEventUtil.java @@ -29,6 +29,7 @@ import com.plotsquared.sponge.events.PlotUnlinkEvent; import org.spongepowered.api.event.Event; import org.spongepowered.api.event.EventManager; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.UUID; @@ -120,9 +121,13 @@ public class SpongeEventUtil extends EventUtil { } @Override + @Nullable public Rating callRating(PlotPlayer player, Plot plot, Rating rating) { PlotRateEvent event = new PlotRateEvent(player, rating, plot); this.events.post(event); + if (event.isCancelled()) { + return null; + } return event.getRating(); }