From 9a3d4bef036e982e0826bc451620835aa3c15c0d Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 10 Feb 2019 22:04:05 -0800 Subject: [PATCH] Handles setspawn better when a player owns the island Island members were not being cleared from the island properly. It should be done via IslandManager and not directly on the island object because the island manager holds a number of ownership maps. Also the player needs to have their island home locations cleared. Further, any members of the island also need to be completely cleared from the island. Finally, as this is similar to unregistering the player from the island, the event should be fired so that any addons know the island is no longer owned and the owner no longer owns an island. --- .../commands/admin/AdminSetspawnCommand.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetspawnCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetspawnCommand.java index 62001dd5a..442636d8d 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetspawnCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminSetspawnCommand.java @@ -2,9 +2,16 @@ package world.bentobox.bentobox.api.commands.admin; import java.util.List; import java.util.Optional; +import java.util.UUID; + +import org.bukkit.Bukkit; + +import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.ConfirmableCommand; +import world.bentobox.bentobox.api.events.IslandBaseEvent; +import world.bentobox.bentobox.api.events.island.IslandEvent; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; @@ -38,14 +45,35 @@ public class AdminSetspawnCommand extends ConfirmableCommand { } // Everything's fine, we can set the island as spawn :) - askConfirmation(user, user.getTranslation("commands.admin.setspawn.confirmation"), () -> { - getIslands().setSpawn(island.get()); - user.sendMessage("general.success"); - }); + askConfirmation(user, user.getTranslation("commands.admin.setspawn.confirmation"), () -> setSpawn(user, island.get())); return true; } else { user.sendMessage("commands.admin.setspawn.no-island-here"); return false; } } + + private void setSpawn(User user, Island i) { + if (!i.getMembers().isEmpty()) { + if (i.getOwner() != null) { + // Fire event + IslandBaseEvent event = IslandEvent.builder() + .island(i) + .location(i.getCenter()) + .reason(IslandEvent.Reason.UNREGISTERED) + .involvedPlayer(i.getOwner()) + .admin(true) + .build(); + Bukkit.getServer().getPluginManager().callEvent(event); + } + // If island is owned, then unregister the owner and any members + new ImmutableSet.Builder().addAll(i.getMembers().keySet()).build().forEach(m -> { + getIslands().removePlayer(getWorld(), m); + getPlayers().clearHomeLocations(getWorld(), m); + }); + } + getIslands().setSpawn(i); + user.sendMessage("general.success"); + + } }