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.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.user.User;
import world.bentobox.bentobox.database.objects.Island;
@ -72,6 +74,14 @@ public class AdminDeleteCommand extends ConfirmableCommand {
Island oldIsland = getIslands().getIsland(getWorld(), targetUUID);
Vector vector = 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
User target = User.getInstance(targetUUID);
// 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.commands.CompositeCommand;
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.team.TeamEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
@ -115,11 +116,21 @@ public class IslandResetCommand extends ConfirmableCommand {
}
private boolean resetIsland(User user, String name) {
// Reset the island
user.sendMessage("commands.island.create.creating-island");
// Get the player's old island
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)
kickMembers(oldIsland);

View File

@ -86,6 +86,13 @@ public class IslandEvent extends IslandBaseEvent {
* To read the rank value, check the {@link Flags#LOCK} flag.
*/
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
* 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);
}
}
/**
* Fired when an island is going to be reset.
* May be cancelled.
* Fired before an island has its player data cleared, e.g., just beofre a reset
* @since 1.12.0
*/
public static class IslandResetEvent extends IslandBaseEvent {
private @NonNull Island oldIsland;
public static class IslandPreclearEvent extends IslandBaseEvent {
private @NonNull final Island oldIsland;
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
super(island, player, admin, location);
this.blueprintBundle = blueprintBundle;
this.oldIsland = oldIsland;
// Create a copy of the old island
this.oldIsland = new Island(oldIsland);
}
/**
@ -355,11 +363,30 @@ public class IslandEvent extends IslandBaseEvent {
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
*/
public void setOldIsland(Island oldIsland) {
this.oldIsland = oldIsland;
@NonNull
public Island getOldIsland() {
return oldIsland;
}
/**
@ -382,12 +409,13 @@ public class IslandEvent 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) {
// Final variables have to be declared in the constructor
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;
}
/**
* @since 1.12.0
*/
public void setOldIsland(Island oldIsland) {
this.oldIsland = oldIsland;
}
}
/**
* Fired when something happens to the island not covered by other events
@ -661,7 +683,7 @@ public class IslandEvent extends IslandBaseEvent {
return unreg;
case RANGE_CHANGE:
IslandProtectionRangeChangeEvent
change = new IslandProtectionRangeChangeEvent(island, player, admin, location, newRange, oldRange);
change = new IslandProtectionRangeChangeEvent(island, player, admin, location, newRange, oldRange);
Bukkit.getPluginManager().callEvent(change);
return change;
default:

View File

@ -251,7 +251,7 @@ public class IslandResetCommandTest {
// TODO Verify that panel was shown
// verify(bpm).showPanel(any(), eq(user), eq(irc.getLabel()));
// Verify event
verify(pim, times(12)).callEvent(any(IslandBaseEvent.class));
verify(pim, times(14)).callEvent(any(IslandBaseEvent.class));
// Verify messaging
verify(user).sendMessage("commands.island.create.creating-island");
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")));
verify(user).sendMessage("commands.island.create.creating-island");
// Verify event
verify(pim, times(12)).callEvent(any(IslandBaseEvent.class));
verify(pim, times(14)).callEvent(any(IslandBaseEvent.class));
}
}