diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java index f00e1715..cb0164a3 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java +++ b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java @@ -2,6 +2,7 @@ package net.william278.huskhomes.event; import net.william278.huskhomes.BukkitHuskHomes; import net.william278.huskhomes.player.OnlineUser; +import net.william278.huskhomes.player.User; import net.william278.huskhomes.position.Home; import net.william278.huskhomes.position.Warp; import net.william278.huskhomes.teleport.Teleport; @@ -106,4 +107,26 @@ public class BukkitEventDispatcher implements EventDispatcher { return dispatchFuture; } + @Override + public CompletableFuture dispatchDeleteAllHomesEvent(@NotNull User user) { + final CompletableFuture dispatchFuture = new CompletableFuture<>(); + Bukkit.getScheduler().runTask(plugin, () -> { + final DeleteAllHomesEvent event = new DeleteAllHomesEvent(user); + Bukkit.getPluginManager().callEvent(event); + dispatchFuture.complete(event); + }); + return dispatchFuture; + } + + @Override + public CompletableFuture dispatchDeleteAllWarpsEvent() { + final CompletableFuture dispatchFuture = new CompletableFuture<>(); + Bukkit.getScheduler().runTask(plugin, () -> { + final DeleteAllWarpsEvent event = new DeleteAllWarpsEvent(); + Bukkit.getPluginManager().callEvent(event); + dispatchFuture.complete(event); + }); + return dispatchFuture; + } + } diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/DeleteAllHomesEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/DeleteAllHomesEvent.java new file mode 100644 index 00000000..b9c76bb6 --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/DeleteAllHomesEvent.java @@ -0,0 +1,47 @@ +package net.william278.huskhomes.event; + +import net.william278.huskhomes.player.User; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class DeleteAllHomesEvent extends Event implements IDeleteAllHomesEvent, Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private boolean cancelled; + + @NotNull + private final User homeOwner; + + public DeleteAllHomesEvent(@NotNull User homeOwner) { + this.homeOwner = homeOwner; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + @SuppressWarnings("unused") + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public @NotNull User getHomeOwner() { + return homeOwner; + } +} diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/DeleteAllWarpsEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/DeleteAllWarpsEvent.java new file mode 100644 index 00000000..a364fc35 --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/DeleteAllWarpsEvent.java @@ -0,0 +1,38 @@ +package net.william278.huskhomes.event; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class DeleteAllWarpsEvent extends Event implements IDeleteAllWarpsEvent, Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private boolean cancelled; + + public DeleteAllWarpsEvent() { + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + @SuppressWarnings("unused") + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + +} diff --git a/bukkit/src/main/java/net/william278/huskhomes/hook/DynMapHook.java b/bukkit/src/main/java/net/william278/huskhomes/hook/DynMapHook.java index b93d2e5b..690cd2e1 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/hook/DynMapHook.java +++ b/bukkit/src/main/java/net/william278/huskhomes/hook/DynMapHook.java @@ -3,13 +3,14 @@ package net.william278.huskhomes.hook; import net.william278.huskhomes.BukkitHuskHomes; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.command.BukkitCommandType; +import net.william278.huskhomes.player.User; import net.william278.huskhomes.position.Home; import net.william278.huskhomes.position.Warp; import org.apache.commons.lang.StringEscapeUtils; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import org.dynmap.DynmapAPI; -import org.dynmap.markers.GenericMarker; +import org.dynmap.markers.Marker; import org.dynmap.markers.MarkerAPI; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -69,7 +70,7 @@ public class DynMapHook extends MapHook { final CompletableFuture updatedFuture = new CompletableFuture<>(); removeHome(home).thenRun(() -> Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> { dynmapAPI.getMarkerAPI().getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).createMarker( - home.uuid.toString(), home.meta.name, home.world.name, + home.owner.uuid + ":" + home.uuid, home.meta.name, home.world.name, home.x, home.y, home.z, dynmapAPI.getMarkerAPI().getMarkerIcon(PUBLIC_HOME_MARKER_IMAGE_NAME), false) .setDescription(MarkerInformationPopup.create(home.meta.name) .setThumbnailMarker(PUBLIC_HOME_MARKER_IMAGE_NAME) @@ -88,16 +89,27 @@ public class DynMapHook extends MapHook { final CompletableFuture removedFuture = new CompletableFuture<>(); Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> { - dynmapAPI.getMarkerAPI().getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).getMarkers() - .stream() - .filter(marker -> marker.getMarkerID().equals(home.uuid.toString())) + dynmapAPI.getMarkerAPI().getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).getMarkers().stream() + .filter(marker -> marker.getMarkerID().equals(home.owner.uuid + ":" + home.uuid)) .findFirst() - .ifPresent(GenericMarker::deleteMarker); + .ifPresent(Marker::deleteMarker); removedFuture.complete(null); }); return removedFuture; } + @Override + public CompletableFuture clearHomes(@NotNull User user) { + final CompletableFuture clearedFuture = new CompletableFuture<>(); + Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> { + dynmapAPI.getMarkerAPI().getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).getMarkers().stream() + .filter(marker -> marker.getMarkerID().startsWith(user.uuid.toString())) + .forEach(Marker::deleteMarker); + clearedFuture.complete(null); + }); + return clearedFuture; + } + @Override public CompletableFuture updateWarp(@NotNull Warp warp) { if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null); @@ -123,16 +135,26 @@ public class DynMapHook extends MapHook { final CompletableFuture removedFuture = new CompletableFuture<>(); CompletableFuture.runAsync(() -> Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> { - dynmapAPI.getMarkerAPI().getMarkerSet(WARPS_MARKER_SET_ID).getMarkers() - .stream() + dynmapAPI.getMarkerAPI().getMarkerSet(WARPS_MARKER_SET_ID).getMarkers().stream() .filter(marker -> marker.getMarkerID().equals(warp.uuid.toString())) .findFirst() - .ifPresent(GenericMarker::deleteMarker); + .ifPresent(Marker::deleteMarker); removedFuture.complete(null); })); return removedFuture; } + @Override + public CompletableFuture clearWarps() { + final CompletableFuture clearedFuture = new CompletableFuture<>(); + Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> { + dynmapAPI.getMarkerAPI().getMarkerSet(WARPS_MARKER_SET_ID).getMarkers() + .forEach(Marker::deleteMarker); + clearedFuture.complete(null); + }); + return clearedFuture; + } + /** * Creates an HTML Dynmap marker information popup widget */ diff --git a/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java index bb76d355..8bac4805 100644 --- a/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java @@ -3,7 +3,6 @@ package net.william278.huskhomes.command; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.player.OnlineUser; import net.william278.huskhomes.player.User; -import net.william278.huskhomes.position.Home; import net.william278.huskhomes.util.Permission; import net.william278.huskhomes.util.RegexUtil; import org.jetbrains.annotations.NotNull; @@ -12,7 +11,6 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; public class DelHomeCommand extends CommandBase implements TabCompletable { @@ -76,7 +74,7 @@ public class DelHomeCommand extends CommandBase implements TabCompletable { plugin.getLocales().getLocale("home_deleted", homeName).ifPresent(deleter::sendMessage); return; } - if (homeName.equals("all")) { + if (homeName.equalsIgnoreCase("all")) { deleteAllHomes(deleter, homeOwner, delHomeAllConfirm); return; } @@ -86,7 +84,7 @@ public class DelHomeCommand extends CommandBase implements TabCompletable { plugin.getLocales().getLocale("home_deleted_other", homeOwner.username, homeName).ifPresent(deleter::sendMessage); return; } - if (homeName.equals("all")) { + if (homeName.equalsIgnoreCase("all")) { deleteAllHomes(deleter, homeOwner, delHomeAllConfirm); return; } @@ -104,26 +102,21 @@ public class DelHomeCommand extends CommandBase implements TabCompletable { */ private void deleteAllHomes(@NotNull OnlineUser deleter, @NotNull User homeOwner, final boolean confirm) { - plugin.getDatabase().getHomes(homeOwner).thenAccept(homes -> { - if (homes.isEmpty()) { + if (!confirm) { + plugin.getLocales().getLocale("delete_all_homes_confirm") + .ifPresent(deleter::sendMessage); + return; + } + + plugin.getSavedPositionManager().deleteAllHomes(homeOwner).thenAccept(deleted -> { + if (deleted == 0) { plugin.getLocales().getLocale("error_no_warps_set") .ifPresent(deleter::sendMessage); return; } - if (!confirm) { - plugin.getLocales().getLocale("delete_all_homes_confirm") - .ifPresent(deleter::sendMessage); - return; - } - - final List> homeDeletionFuture = new ArrayList<>(); - for (final Home toDelete : homes) { - homeDeletionFuture.add(plugin.getSavedPositionManager().deleteHome(homeOwner, toDelete.meta.name)); - } - CompletableFuture.allOf(homeDeletionFuture.toArray(new CompletableFuture[0])) - .thenRun(() -> plugin.getLocales().getLocale("delete_all_homes_success", - Integer.toString(homeDeletionFuture.size())).ifPresent(deleter::sendMessage)); + plugin.getLocales().getLocale("delete_all_homes_success", Integer.toString(deleted)) + .ifPresent(deleter::sendMessage); }); } diff --git a/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java index 4c75088b..1737b944 100644 --- a/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java @@ -2,15 +2,12 @@ package net.william278.huskhomes.command; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.player.OnlineUser; -import net.william278.huskhomes.position.Warp; import net.william278.huskhomes.util.Permission; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; public class DelWarpCommand extends CommandBase implements TabCompletable { @@ -30,7 +27,7 @@ public class DelWarpCommand extends CommandBase implements TabCompletable { .ifPresent(onlineUser::sendMessage); return; } - if (warpName.equals("all")) { + if (warpName.equalsIgnoreCase("all")) { deleteAllWarps(onlineUser, confirm); return; } @@ -51,26 +48,21 @@ public class DelWarpCommand extends CommandBase implements TabCompletable { * @param confirm whether to skip the confirmation prompt */ private void deleteAllWarps(@NotNull OnlineUser deleter, final boolean confirm) { - plugin.getDatabase().getWarps().thenAccept(warps -> { - if (warps.isEmpty()) { + if (!confirm) { + plugin.getLocales().getLocale("delete_all_warps_confirm") + .ifPresent(deleter::sendMessage); + return; + } + + plugin.getSavedPositionManager().deleteAllWarps().thenAccept(deleted -> { + if (deleted == 0) { plugin.getLocales().getLocale("error_no_warps_set") .ifPresent(deleter::sendMessage); return; } - if (!confirm) { - plugin.getLocales().getLocale("confirm_delete_all_warps") - .ifPresent(deleter::sendMessage); - return; - } - - final List> homeDeletionFuture = new ArrayList<>(); - for (final Warp toDelete : warps) { - homeDeletionFuture.add(plugin.getSavedPositionManager().deleteWarp(toDelete.meta.name)); - } - CompletableFuture.allOf(homeDeletionFuture.toArray(new CompletableFuture[0])) - .thenRun(() -> plugin.getLocales().getLocale("delete_all_warps_success", - Integer.toString(homeDeletionFuture.size())).ifPresent(deleter::sendMessage)); + plugin.getLocales().getLocale("delete_all_warps_success", Integer.toString(deleted)) + .ifPresent(deleter::sendMessage); }); } diff --git a/common/src/main/java/net/william278/huskhomes/database/Database.java b/common/src/main/java/net/william278/huskhomes/database/Database.java index edca3e05..b3dfd209 100644 --- a/common/src/main/java/net/william278/huskhomes/database/Database.java +++ b/common/src/main/java/net/william278/huskhomes/database/Database.java @@ -443,6 +443,14 @@ public abstract class Database { */ public abstract CompletableFuture deleteHome(@NotNull UUID uuid); + /** + * Deletes all {@link Home}s of a {@link User} from the home table on the database. + * + * @param user The {@link User} to delete all homes of + * @return A future returning an integer; the number of deleted homes + */ + public abstract CompletableFuture deleteAllHomes(@NotNull User user); + /** * Deletes a {@link Warp} by the given unique id from the warp table on the database. * @@ -451,6 +459,13 @@ public abstract class Database { */ public abstract CompletableFuture deleteWarp(@NotNull UUID uuid); + /** + * Deletes all {@link Warp}s set on the database table + * + * @return A future returning an integer; the number of deleted warps + */ + public abstract CompletableFuture deleteAllWarps(); + /** * Close any remaining connection to the database source */ diff --git a/common/src/main/java/net/william278/huskhomes/database/MySqlDatabase.java b/common/src/main/java/net/william278/huskhomes/database/MySqlDatabase.java index 5be3f515..9d5f7490 100644 --- a/common/src/main/java/net/william278/huskhomes/database/MySqlDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/database/MySqlDatabase.java @@ -1011,6 +1011,32 @@ public class MySqlDatabase extends Database { }); } + @Override + public CompletableFuture deleteAllHomes(@NotNull User user) { + return CompletableFuture.supplyAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM `%positions_table%` + WHERE `%positions_table%`.`id` IN ( + SELECT `position_id` + FROM `%saved_positions_table%` + WHERE `%saved_positions_table%`.`id` IN ( + SELECT `saved_position_id` + FROM `%homes_table%` + WHERE `owner_uuid`=? + ) + );"""))) { + + statement.setString(1, user.uuid.toString()); + return statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to delete all homes for " + user.username + " from the database", e); + } + return 0; + }); + } + @Override public CompletableFuture deleteWarp(@NotNull UUID uuid) { return CompletableFuture.runAsync(() -> { @@ -1036,6 +1062,29 @@ public class MySqlDatabase extends Database { }); } + @Override + public CompletableFuture deleteAllWarps() { + return CompletableFuture.supplyAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM `%positions_table%` + WHERE `%positions_table%`.`id` IN ( + SELECT `position_id` + FROM `%saved_positions_table%` + WHERE `%saved_positions_table%`.`id` IN ( + SELECT `saved_position_id` + FROM `%warps_table%` + ) + );"""))) { + return statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to delete all warps from the database", e); + } + return 0; + }); + } + @Override public void terminate() { if (dataSource != null) { diff --git a/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java b/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java index 22b0f3f3..49cc5d0d 100644 --- a/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java @@ -1011,6 +1011,32 @@ public class SqLiteDatabase extends Database { }); } + @Override + public CompletableFuture deleteAllHomes(@NotNull User user) { + return CompletableFuture.supplyAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM `%positions_table%` + WHERE `%positions_table%`.`id` IN ( + SELECT `position_id` + FROM `%saved_positions_table%` + WHERE `%saved_positions_table%`.`id` IN ( + SELECT `saved_position_id` + FROM `%homes_table%` + WHERE `owner_uuid`=? + ) + );"""))) { + + statement.setString(1, user.uuid.toString()); + return statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to delete all homes for " + user.username + " from the database", e); + } + return 0; + }); + } + @Override public CompletableFuture deleteWarp(@NotNull UUID uuid) { return CompletableFuture.runAsync(() -> { @@ -1036,6 +1062,29 @@ public class SqLiteDatabase extends Database { }); } + @Override + public CompletableFuture deleteAllWarps() { + return CompletableFuture.supplyAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM `%positions_table%` + WHERE `%positions_table%`.`id` IN ( + SELECT `position_id` + FROM `%saved_positions_table%` + WHERE `%saved_positions_table%`.`id` IN ( + SELECT `saved_position_id` + FROM `%warps_table%` + ) + );"""))) { + return statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to delete all warps from the database", e); + } + return 0; + }); + } + @Override public void terminate() { try { diff --git a/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java b/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java index b64938b2..6323661d 100644 --- a/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java +++ b/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java @@ -1,6 +1,7 @@ package net.william278.huskhomes.event; import net.william278.huskhomes.player.OnlineUser; +import net.william278.huskhomes.player.User; import net.william278.huskhomes.position.Home; import net.william278.huskhomes.position.Warp; import net.william278.huskhomes.teleport.Teleport; @@ -32,4 +33,7 @@ public interface EventDispatcher { CompletableFuture dispatchViewWarpListEvent(@NotNull List homes, @NotNull OnlineUser user); + CompletableFuture dispatchDeleteAllHomesEvent(@NotNull User user); + + CompletableFuture dispatchDeleteAllWarpsEvent(); } diff --git a/common/src/main/java/net/william278/huskhomes/event/IDeleteAllHomesEvent.java b/common/src/main/java/net/william278/huskhomes/event/IDeleteAllHomesEvent.java new file mode 100644 index 00000000..10f57a99 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/event/IDeleteAllHomesEvent.java @@ -0,0 +1,19 @@ +package net.william278.huskhomes.event; + +import net.william278.huskhomes.player.User; +import org.jetbrains.annotations.NotNull; + +/** + * Representation of an event that fires when all homes are deleted + */ +public interface IDeleteAllHomesEvent extends CancellableEvent { + + /** + * Get the player whose homes are being deleted + * + * @return the {@link User} whose homes are being deleted + */ + @NotNull + User getHomeOwner(); + +} diff --git a/common/src/main/java/net/william278/huskhomes/event/IDeleteAllWarpsEvent.java b/common/src/main/java/net/william278/huskhomes/event/IDeleteAllWarpsEvent.java new file mode 100644 index 00000000..fbeef111 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/event/IDeleteAllWarpsEvent.java @@ -0,0 +1,8 @@ +package net.william278.huskhomes.event; + +/** + * Representation of an event that fires when all warps are deleted + */ +public interface IDeleteAllWarpsEvent extends CancellableEvent { + +} diff --git a/common/src/main/java/net/william278/huskhomes/event/IHomeDeleteEvent.java b/common/src/main/java/net/william278/huskhomes/event/IHomeDeleteEvent.java index 5d7243b1..ce921830 100644 --- a/common/src/main/java/net/william278/huskhomes/event/IHomeDeleteEvent.java +++ b/common/src/main/java/net/william278/huskhomes/event/IHomeDeleteEvent.java @@ -4,7 +4,7 @@ import net.william278.huskhomes.position.Home; import org.jetbrains.annotations.NotNull; /** - * Representation of an event that fires when a home is deleted + * Representation of an event that fires when a single home is deleted */ public interface IHomeDeleteEvent extends CancellableEvent { diff --git a/common/src/main/java/net/william278/huskhomes/event/IWarpDeleteEvent.java b/common/src/main/java/net/william278/huskhomes/event/IWarpDeleteEvent.java index f02a6dc0..cf139669 100644 --- a/common/src/main/java/net/william278/huskhomes/event/IWarpDeleteEvent.java +++ b/common/src/main/java/net/william278/huskhomes/event/IWarpDeleteEvent.java @@ -4,7 +4,7 @@ import net.william278.huskhomes.position.Warp; import org.jetbrains.annotations.NotNull; /** - * Representation of an event that fires when a warp is deleted + * Representation of an event that fires when a single warp is deleted */ public interface IWarpDeleteEvent extends CancellableEvent { diff --git a/common/src/main/java/net/william278/huskhomes/hook/BlueMapHook.java b/common/src/main/java/net/william278/huskhomes/hook/BlueMapHook.java index 8e8132e1..18c398e7 100644 --- a/common/src/main/java/net/william278/huskhomes/hook/BlueMapHook.java +++ b/common/src/main/java/net/william278/huskhomes/hook/BlueMapHook.java @@ -6,6 +6,7 @@ import de.bluecolored.bluemap.api.BlueMapWorld; import de.bluecolored.bluemap.api.markers.MarkerSet; import de.bluecolored.bluemap.api.markers.POIMarker; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.player.User; import net.william278.huskhomes.position.Home; import net.william278.huskhomes.position.Warp; import net.william278.huskhomes.position.World; @@ -73,7 +74,7 @@ public class BlueMapHook extends MapHook { blueMapAPI -> getBlueMapWorld(blueMapAPI, home.world)).ifPresent(blueMapWorld -> blueMapWorld.getMaps() .forEach(blueMapMap -> blueMapMap.getMarkerSets() .computeIfPresent(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> { - markerSet.getMarkers().put(home.uuid.toString(), + markerSet.getMarkers().put(home.owner.uuid + ":" + home.uuid, POIMarker.toBuilder() .label("/phome" + home.owner.username + "." + home.meta.name) .position((int) home.x, (int) home.y, (int) home.z) @@ -91,12 +92,25 @@ public class BlueMapHook extends MapHook { BlueMapAPI.getInstance().flatMap(blueMapAPI -> getBlueMapWorld(blueMapAPI, home.world)) .ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(blueMapMap -> blueMapMap.getMarkerSets() .computeIfPresent(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> { - markerSet.getMarkers().remove(home.uuid.toString()); + markerSet.getMarkers().remove(home.owner.uuid + ":" + home.uuid); return markerSet; }))); return CompletableFuture.completedFuture(null); } + @Override + public CompletableFuture clearHomes(@NotNull User user) { + BlueMapAPI.getInstance().ifPresent((BlueMapAPI blueMapAPI) -> blueMapAPI.getWorlds() + .forEach(blueMapWorld -> blueMapWorld.getMaps() + .forEach(blueMapMap -> blueMapMap.getMarkerSets() + .computeIfPresent(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> { + markerSet.getMarkers().keySet().removeIf(key -> key.startsWith(user.uuid.toString())); + return markerSet; + })))); + + return CompletableFuture.completedFuture(null); + } + @Override public CompletableFuture updateWarp(@NotNull Warp warp) { if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null); @@ -128,6 +142,19 @@ public class BlueMapHook extends MapHook { return CompletableFuture.completedFuture(null); } + @Override + public CompletableFuture clearWarps() { + BlueMapAPI.getInstance().ifPresent((BlueMapAPI blueMapAPI) -> blueMapAPI.getWorlds() + .forEach(blueMapWorld -> blueMapWorld.getMaps() + .forEach(blueMapMap -> blueMapMap.getMarkerSets() + .computeIfPresent(blueMapWorld.getId() + ":" + WARPS_MARKER_SET_ID, (s, markerSet) -> { + markerSet.getMarkers().clear(); + return markerSet; + })))); + + return CompletableFuture.completedFuture(null); + } + /** * Get the {@link BlueMapWorld} for a world * diff --git a/common/src/main/java/net/william278/huskhomes/hook/MapHook.java b/common/src/main/java/net/william278/huskhomes/hook/MapHook.java index 3af01f94..22f8cc5d 100644 --- a/common/src/main/java/net/william278/huskhomes/hook/MapHook.java +++ b/common/src/main/java/net/william278/huskhomes/hook/MapHook.java @@ -2,6 +2,7 @@ package net.william278.huskhomes.hook; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomesException; +import net.william278.huskhomes.player.User; import net.william278.huskhomes.position.Home; import net.william278.huskhomes.position.SavedPosition; import net.william278.huskhomes.position.Warp; @@ -60,6 +61,14 @@ public abstract class MapHook extends PluginHook { */ public abstract CompletableFuture removeHome(@NotNull Home home); + /** + * Clears homes owned by a player from the map + * + * @param user the player whose homes to clear + * @return a {@link CompletableFuture} that completes when the homes have been cleared + */ + public abstract CompletableFuture clearHomes(@NotNull User user); + /** * Update a warp, adding it to the map if it exists, or updating it on the map if it doesn't * @@ -75,6 +84,13 @@ public abstract class MapHook extends PluginHook { */ public abstract CompletableFuture removeWarp(@NotNull Warp warp); + /** + * Clears all warps from the map + * + * @return a {@link CompletableFuture} that completes when the warps have been cleared + */ + public abstract CompletableFuture clearWarps(); + /** * Returns if the position is valid to be set on this server * diff --git a/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java b/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java index 19956710..fdc1ea47 100644 --- a/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java +++ b/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java @@ -152,6 +152,20 @@ public class SavedPositionManager { }); } + public CompletableFuture deleteAllHomes(@NotNull User user) { + return CompletableFuture.supplyAsync(() -> eventDispatcher.dispatchDeleteAllHomesEvent(user).join().isCancelled()) + .thenApply(cancelled -> database.deleteAllHomes(user) + .thenApply(result -> { + cache.homes.remove(user.uuid); + cache.privateHomeLists.remove(user.username); + cache.publicHomeLists.clear(); + if (mapHook != null) { + mapHook.clearHomes(user); + } + return result; + }).join()); + } + /** * Tries to update an existing home with new metadata * @@ -320,6 +334,18 @@ public class SavedPositionManager { }); } + public CompletableFuture deleteAllWarps() { + return CompletableFuture.supplyAsync(() -> eventDispatcher.dispatchDeleteAllWarpsEvent().join().isCancelled()) + .thenApply(cancelled -> database.deleteAllWarps() + .thenApply(result -> { + cache.warps.clear(); + if (mapHook != null) { + mapHook.clearWarps(); + } + return result; + }).join()); + } + /** * Tries to update an existing warp with new metadata *