Added IslandBanEvent and IslandUnbanEvent (cancellable)

This commit is contained in:
Florian CUNY 2019-01-05 10:23:53 +01:00
parent e3574e5ee3
commit 9387f65dbf
4 changed files with 92 additions and 25 deletions

View File

@ -10,6 +10,8 @@ import org.bukkit.Sound;
import org.bukkit.entity.Player;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
@ -86,15 +88,26 @@ public class IslandBanCommand extends CompositeCommand {
// Check if player can ban any more players
int banLimit = issuer.getPermissionValue(getPermissionPrefix() + "ban.maxlimit", getIWM().getBanLimit(getWorld()));
if (banLimit <= -1 || island.getBanned().size() < banLimit) {
if (island.ban(issuer.getUniqueId(), target.getUniqueId())) {
issuer.sendMessage("general.success");
target.sendMessage("commands.island.ban.owner-banned-you", TextVariables.NAME, issuer.getName());
// If the player is online, has an island and on the banned island, move them home immediately
if (target.isOnline() && getIslands().hasIsland(getWorld(), target.getUniqueId()) && island.onIsland(target.getLocation())) {
getIslands().homeTeleport(getWorld(), target.getPlayer());
island.getWorld().playSound(target.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 1F, 1F);
// Run the event
IslandBaseEvent banEvent = IslandEvent.builder()
.island(island)
.involvedPlayer(target.getUniqueId())
.admin(false)
.reason(IslandEvent.Reason.BAN)
.build();
// Event is not cancelled
if (!banEvent.isCancelled()) {
if (island.ban(issuer.getUniqueId(), target.getUniqueId())) {
issuer.sendMessage("general.success");
target.sendMessage("commands.island.ban.owner-banned-you", TextVariables.NAME, issuer.getName());
// If the player is online, has an island and on the banned island, move them home immediately
if (target.isOnline() && getIslands().hasIsland(getWorld(), target.getUniqueId()) && island.onIsland(target.getLocation())) {
getIslands().homeTeleport(getWorld(), target.getPlayer());
island.getWorld().playSound(target.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 1F, 1F);
}
return true;
}
return true;
}
} else {
issuer.sendMessage("commands.island.ban.cannot-ban-more-players");

View File

@ -6,6 +6,8 @@ import java.util.UUID;
import java.util.stream.Collectors;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
@ -65,15 +67,26 @@ public class IslandUnbanCommand extends CompositeCommand {
}
private boolean unban(User issuer, User target) {
if (getIslands().getIsland(getWorld(), issuer.getUniqueId()).unban(issuer.getUniqueId(), target.getUniqueId())) {
issuer.sendMessage("general.success");
target.sendMessage("commands.island.unban.you-are-unbanned", TextVariables.NAME, issuer.getName());
// Set cooldown
if (getSettings().getBanCooldown() > 0 && getParent() != null) {
getParent().getSubCommand("ban").ifPresent(subCommand ->
subCommand.setCooldown(issuer.getUniqueId(), target.getUniqueId(), getSettings().getBanCooldown() * 60));
// Run the event
IslandBaseEvent unbanEvent = IslandEvent.builder()
.island(getIslands().getIsland(getWorld(), issuer.getUniqueId()))
.involvedPlayer(target.getUniqueId())
.admin(false)
.reason(IslandEvent.Reason.UNBAN)
.build();
// Event is not cancelled
if (!unbanEvent.isCancelled()) {
if (getIslands().getIsland(getWorld(), issuer.getUniqueId()).unban(issuer.getUniqueId(), target.getUniqueId())) {
issuer.sendMessage("general.success");
target.sendMessage("commands.island.unban.you-are-unbanned", TextVariables.NAME, issuer.getName());
// Set cooldown
if (getSettings().getBanCooldown() > 0 && getParent() != null) {
getParent().getSubCommand("ban").ifPresent(subCommand ->
subCommand.setCooldown(issuer.getUniqueId(), target.getUniqueId(), getSettings().getBanCooldown() * 60));
}
return true;
}
return true;
}
// Unbanning was blocked, maybe due to an event cancellation. Fail silently.
return false;

View File

@ -40,9 +40,13 @@ public class IslandEvent extends IslandBaseEvent {
/**
* Reason for the event
*
*/
public enum Reason {
/**
* Fired when a player will be banned from an island.
* @since 1.1
*/
BAN,
/**
* Called when a player has been allocated a new island spot
* but before the island itself has been pasted or the player teleported.
@ -85,6 +89,11 @@ public class IslandEvent extends IslandBaseEvent {
* ie., island pasted, player teleported, etc.
*/
RESETTED,
/**
* Fired when a player will be unbanned from an island.
* @since 1.1
*/
UNBAN,
/**
* Reserved
*/
@ -99,6 +108,34 @@ public class IslandEvent extends IslandBaseEvent {
return new IslandEventBuilder();
}
/**
* Fired when a player will be banned from an island.
* May be cancelled.
* Cancellation will result in the ban being aborted.
*
* @since 1.1
*/
public static class IslandBanEvent extends IslandBaseEvent {
private IslandBanEvent(Island island, UUID player, boolean admin, Location location) {
// Final variables have to be declared in the constructor
super(island, player, admin, location);
}
}
/**
* Fired when a player will be banned from an island.
* May be cancelled.
* Cancellation will result in the unban being aborted.
*
* @since 1.1
*/
public static class IslandUnbanEvent extends IslandBaseEvent {
public IslandUnbanEvent(Island island, UUID player, boolean admin, Location location) {
// Final variables have to be declared in the constructor
super(island, player, admin, location);
}
}
/**
* Fired when an island is going to be created.
* May be cancelled.
@ -115,13 +152,6 @@ public class IslandEvent extends IslandBaseEvent {
*
*/
public static class IslandCreatedEvent extends IslandBaseEvent {
/**
* Fired when an island has been created
* @param island
* @param player
* @param admin
* @param location
*/
private IslandCreatedEvent(Island island, UUID player, boolean admin, Location location) {
// Final variables have to be declared in the constructor
super(island, player, admin, location);
@ -270,6 +300,10 @@ public class IslandEvent extends IslandBaseEvent {
Bukkit.getServer().getPluginManager().callEvent(new IslandEvent(island, player, admin, location, reason));
// Generate explicit events
switch (reason) {
case BAN:
IslandBanEvent ban = new IslandBanEvent(island, player, admin, location);
Bukkit.getServer().getPluginManager().callEvent(ban);
return ban;
case CREATE:
IslandCreateEvent create = new IslandCreateEvent(island, player, admin, location);
Bukkit.getServer().getPluginManager().callEvent(create);
@ -306,6 +340,10 @@ public class IslandEvent extends IslandBaseEvent {
IslandResettedEvent resetted = new IslandResettedEvent(island, player, admin, location);
Bukkit.getServer().getPluginManager().callEvent(resetted);
return resetted;
case UNBAN:
IslandUnbanEvent unban = new IslandUnbanEvent(island, player, admin, location);
Bukkit.getServer().getPluginManager().callEvent(unban);
return unban;
case UNLOCK:
IslandUnlockEvent unlock = new IslandUnlockEvent(island, player, admin, location);
Bukkit.getServer().getPluginManager().callEvent(unlock);
@ -315,7 +353,6 @@ public class IslandEvent extends IslandBaseEvent {
Bukkit.getServer().getPluginManager().callEvent(general);
return general;
}
}
}
}

View File

@ -128,6 +128,8 @@ public class Island implements DataObject {
/**
* Bans the target player from this Island.
* If the player is a member, coop or trustee, they will be removed from those lists.
* <br/>
* Calling this method won't call the {@link world.bentobox.bentobox.api.events.island.IslandEvent.IslandBanEvent}.
* @param issuer UUID of the issuer, may be null.
* Whenever possible, one should be provided.
* @param target UUID of the target, must be provided.
@ -157,6 +159,8 @@ public class Island implements DataObject {
/**
* Unbans the target player from this Island.
* <br/>
* Calling this method won't call the {@link world.bentobox.bentobox.api.events.island.IslandEvent.IslandUnbanEvent}.
* @param issuer UUID of the issuer, may be null.
* Whenever possible, one should be provided.
* @param target UUID of the target, must be provided.