Allow PlotRateEvent to be cancelled. Fixes #2017

This commit is contained in:
sauilitired 2018-08-19 23:12:59 +02:00
parent 4b8c434669
commit 01a927ccad
8 changed files with 75 additions and 13 deletions

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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));
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());
MainUtil.sendMessage(this.player, C.COMMENT_THIS,
command.getUsage());
}
}
}
return false;
@ -189,9 +192,11 @@ public class Rate extends SubCommand {
return;
}
Rating result = EventUtil.manager.callRating(player, plot, new Rating(rating));
if (result != null) {
plot.addRating(uuid, result);
sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
}
}
};
if (plot.getSettings().ratings == null) {
if (!Settings.Enabled_Components.RATING_CACHE) {

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -3,10 +3,13 @@ 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);
@ -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;
}
}

View File

@ -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();
}