Added IslandPreclearEvent

This event gets called prior to an island being cleared of players and
other data. This can happen before a reset or an admin delete. It is
there so addons can perform tasks on the players before the island is
cleared.

Fixes https://github.com/BentoBoxWorld/BentoBox/issues/1200
This commit is contained in:
tastybento 2020-02-20 16:56:35 -08:00
parent d3a7a908f1
commit 65dbd530d5
4 changed files with 65 additions and 22 deletions

View File

@ -10,6 +10,8 @@ import org.bukkit.util.Vector;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand; import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
@ -72,6 +74,14 @@ public class AdminDeleteCommand extends ConfirmableCommand {
Island oldIsland = getIslands().getIsland(getWorld(), targetUUID); Island oldIsland = getIslands().getIsland(getWorld(), targetUUID);
Vector vector = null; Vector vector = null;
if (oldIsland != null) { if (oldIsland != null) {
// Fire island preclear event
IslandEvent.builder()
.involvedPlayer(user.getUniqueId())
.reason(Reason.PRECLEAR)
.island(oldIsland)
.oldIsland(oldIsland)
.location(oldIsland.getCenter())
.build();
// Check if player is online and on the island // Check if player is online and on the island
User target = User.getInstance(targetUUID); User target = User.getInstance(targetUUID);
// Remove them from this island (it still exists and will be deleted later) // Remove them from this island (it still exists and will be deleted later)

View File

@ -9,6 +9,7 @@ import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand; import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent.Reason; import world.bentobox.bentobox.api.events.island.IslandEvent.Reason;
import world.bentobox.bentobox.api.events.team.TeamEvent; import world.bentobox.bentobox.api.events.team.TeamEvent;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
@ -115,11 +116,21 @@ public class IslandResetCommand extends ConfirmableCommand {
} }
private boolean resetIsland(User user, String name) { private boolean resetIsland(User user, String name) {
// Reset the island
user.sendMessage("commands.island.create.creating-island");
// Get the player's old island // Get the player's old island
Island oldIsland = getIslands().getIsland(getWorld(), user); Island oldIsland = getIslands().getIsland(getWorld(), user);
// Fire island preclear event
IslandEvent.builder()
.involvedPlayer(user.getUniqueId())
.reason(Reason.PRECLEAR)
.island(oldIsland)
.oldIsland(oldIsland)
.location(oldIsland.getCenter())
.build();
// Reset the island
user.sendMessage("commands.island.create.creating-island");
// Kick all island members (including the owner) // Kick all island members (including the owner)
kickMembers(oldIsland); kickMembers(oldIsland);

View File

@ -86,6 +86,13 @@ public class IslandEvent extends IslandBaseEvent {
* To read the rank value, check the {@link Flags#LOCK} flag. * To read the rank value, check the {@link Flags#LOCK} flag.
*/ */
LOCK, LOCK,
/**
* Called before an island is going to be cleared of island members.
* This event occurs before resets or other island clearing activities.
* Cannot be cancelled.
* @since 1.12.0
*/
PRECLEAR,
/** /**
* Called when a player has been reset and a new island spot allocated * Called when a player has been reset and a new island spot allocated
* but before the island itself has been pasted or the player teleported. * but before the island itself has been pasted or the player teleported.
@ -332,19 +339,20 @@ public class IslandEvent extends IslandBaseEvent {
super(island, player, admin, location); super(island, player, admin, location);
} }
} }
/** /**
* Fired when an island is going to be reset. * Fired before an island has its player data cleared, e.g., just beofre a reset
* May be cancelled. * @since 1.12.0
*/ */
public static class IslandResetEvent extends IslandBaseEvent { public static class IslandPreclearEvent extends IslandBaseEvent {
private @NonNull Island oldIsland; private @NonNull final Island oldIsland;
private @NonNull BlueprintBundle blueprintBundle; private @NonNull BlueprintBundle blueprintBundle;
private IslandResetEvent(Island island, UUID player, boolean admin, Location location, @NonNull BlueprintBundle blueprintBundle, @NonNull Island oldIsland) { private IslandPreclearEvent(Island island, UUID player, boolean admin, Location location, @NonNull Island oldIsland) {
// Final variables have to be declared in the constructor // Final variables have to be declared in the constructor
super(island, player, admin, location); super(island, player, admin, location);
this.blueprintBundle = blueprintBundle; // Create a copy of the old island
this.oldIsland = oldIsland; this.oldIsland = new Island(oldIsland);
} }
/** /**
@ -355,11 +363,30 @@ public class IslandEvent extends IslandBaseEvent {
return oldIsland; return oldIsland;
} }
}
/**
* Fired when an island is going to be reset.
* May be cancelled.
*/
public static class IslandResetEvent extends IslandBaseEvent {
private final @NonNull Island oldIsland;
private @NonNull BlueprintBundle blueprintBundle;
private IslandResetEvent(Island island, UUID player, boolean admin, Location location, @NonNull BlueprintBundle blueprintBundle, @NonNull Island oldIsland) {
// Final variables have to be declared in the constructor
super(island, player, admin, location);
this.blueprintBundle = blueprintBundle;
// Create a copy of the old island
this.oldIsland = new Island(oldIsland);
}
/** /**
* @since 1.12.0 * @since 1.12.0
*/ */
public void setOldIsland(Island oldIsland) { @NonNull
this.oldIsland = oldIsland; public Island getOldIsland() {
return oldIsland;
} }
/** /**
@ -382,12 +409,13 @@ public class IslandEvent extends IslandBaseEvent {
* *
*/ */
public static class IslandResettedEvent extends IslandBaseEvent { public static class IslandResettedEvent extends IslandBaseEvent {
private @NonNull Island oldIsland; private @NonNull final Island oldIsland;
private IslandResettedEvent(Island island, UUID player, boolean admin, Location location, Island oldIsland) { private IslandResettedEvent(Island island, UUID player, boolean admin, Location location, Island oldIsland) {
// Final variables have to be declared in the constructor // Final variables have to be declared in the constructor
super(island, player, admin, location); super(island, player, admin, location);
this.oldIsland = oldIsland; // Create a copy of the old island
this.oldIsland = new Island(oldIsland);
} }
/** /**
@ -398,12 +426,6 @@ public class IslandEvent extends IslandBaseEvent {
return oldIsland; return oldIsland;
} }
/**
* @since 1.12.0
*/
public void setOldIsland(Island oldIsland) {
this.oldIsland = oldIsland;
}
} }
/** /**
* Fired when something happens to the island not covered by other events * Fired when something happens to the island not covered by other events
@ -661,7 +683,7 @@ public class IslandEvent extends IslandBaseEvent {
return unreg; return unreg;
case RANGE_CHANGE: case RANGE_CHANGE:
IslandProtectionRangeChangeEvent IslandProtectionRangeChangeEvent
change = new IslandProtectionRangeChangeEvent(island, player, admin, location, newRange, oldRange); change = new IslandProtectionRangeChangeEvent(island, player, admin, location, newRange, oldRange);
Bukkit.getPluginManager().callEvent(change); Bukkit.getPluginManager().callEvent(change);
return change; return change;
default: default:

View File

@ -251,7 +251,7 @@ public class IslandResetCommandTest {
// TODO Verify that panel was shown // TODO Verify that panel was shown
// verify(bpm).showPanel(any(), eq(user), eq(irc.getLabel())); // verify(bpm).showPanel(any(), eq(user), eq(irc.getLabel()));
// Verify event // Verify event
verify(pim, times(12)).callEvent(any(IslandBaseEvent.class)); verify(pim, times(14)).callEvent(any(IslandBaseEvent.class));
// Verify messaging // Verify messaging
verify(user).sendMessage("commands.island.create.creating-island"); verify(user).sendMessage("commands.island.create.creating-island");
verify(user, never()).sendMessage(eq("commands.island.reset.kicked-from-island"), eq("[gamemode]"), anyString()); verify(user, never()).sendMessage(eq("commands.island.reset.kicked-from-island"), eq("[gamemode]"), anyString());
@ -440,7 +440,7 @@ public class IslandResetCommandTest {
assertTrue(irc.execute(user, irc.getLabel(), Collections.singletonList("custom"))); assertTrue(irc.execute(user, irc.getLabel(), Collections.singletonList("custom")));
verify(user).sendMessage("commands.island.create.creating-island"); verify(user).sendMessage("commands.island.create.creating-island");
// Verify event // Verify event
verify(pim, times(12)).callEvent(any(IslandBaseEvent.class)); verify(pim, times(14)).callEvent(any(IslandBaseEvent.class));
} }
} }