mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 14:38:53 +00:00
delhome/delwarp all rewrite; add events for bulk-deletes, close #265
This commit is contained in:
1 parent
2402f12536
commit
834fdbfc0e
17 files changed
+376
-48
No files matched your search
@@ -2,6 +2,7 @@ package net.william278.huskhomes.event;
|
|||||||
|
|
||||||
import net.william278.huskhomes.BukkitHuskHomes;
|
import net.william278.huskhomes.BukkitHuskHomes;
|
||||||
import net.william278.huskhomes.player.OnlineUser;
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
|
import net.william278.huskhomes.player.User;
|
||||||
import net.william278.huskhomes.position.Home;
|
import net.william278.huskhomes.position.Home;
|
||||||
import net.william278.huskhomes.position.Warp;
|
import net.william278.huskhomes.position.Warp;
|
||||||
import net.william278.huskhomes.teleport.Teleport;
|
import net.william278.huskhomes.teleport.Teleport;
|
||||||
@@ -106,4 +107,26 @@ public class BukkitEventDispatcher implements EventDispatcher {
|
|||||||
return dispatchFuture;
|
return dispatchFuture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<IDeleteAllHomesEvent> dispatchDeleteAllHomesEvent(@NotNull User user) {
|
||||||
|
final CompletableFuture<IDeleteAllHomesEvent> 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<IDeleteAllWarpsEvent> dispatchDeleteAllWarpsEvent() {
|
||||||
|
final CompletableFuture<IDeleteAllWarpsEvent> dispatchFuture = new CompletableFuture<>();
|
||||||
|
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||||
|
final DeleteAllWarpsEvent event = new DeleteAllWarpsEvent();
|
||||||
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
|
dispatchFuture.complete(event);
|
||||||
|
});
|
||||||
|
return dispatchFuture;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,13 +3,14 @@ package net.william278.huskhomes.hook;
|
|||||||
import net.william278.huskhomes.BukkitHuskHomes;
|
import net.william278.huskhomes.BukkitHuskHomes;
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.command.BukkitCommandType;
|
import net.william278.huskhomes.command.BukkitCommandType;
|
||||||
|
import net.william278.huskhomes.player.User;
|
||||||
import net.william278.huskhomes.position.Home;
|
import net.william278.huskhomes.position.Home;
|
||||||
import net.william278.huskhomes.position.Warp;
|
import net.william278.huskhomes.position.Warp;
|
||||||
import org.apache.commons.lang.StringEscapeUtils;
|
import org.apache.commons.lang.StringEscapeUtils;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.dynmap.DynmapAPI;
|
import org.dynmap.DynmapAPI;
|
||||||
import org.dynmap.markers.GenericMarker;
|
import org.dynmap.markers.Marker;
|
||||||
import org.dynmap.markers.MarkerAPI;
|
import org.dynmap.markers.MarkerAPI;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@@ -69,7 +70,7 @@ public class DynMapHook extends MapHook {
|
|||||||
final CompletableFuture<Void> updatedFuture = new CompletableFuture<>();
|
final CompletableFuture<Void> updatedFuture = new CompletableFuture<>();
|
||||||
removeHome(home).thenRun(() -> Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
removeHome(home).thenRun(() -> Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
||||||
dynmapAPI.getMarkerAPI().getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).createMarker(
|
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)
|
home.x, home.y, home.z, dynmapAPI.getMarkerAPI().getMarkerIcon(PUBLIC_HOME_MARKER_IMAGE_NAME), false)
|
||||||
.setDescription(MarkerInformationPopup.create(home.meta.name)
|
.setDescription(MarkerInformationPopup.create(home.meta.name)
|
||||||
.setThumbnailMarker(PUBLIC_HOME_MARKER_IMAGE_NAME)
|
.setThumbnailMarker(PUBLIC_HOME_MARKER_IMAGE_NAME)
|
||||||
@@ -88,16 +89,27 @@ public class DynMapHook extends MapHook {
|
|||||||
|
|
||||||
final CompletableFuture<Void> removedFuture = new CompletableFuture<>();
|
final CompletableFuture<Void> removedFuture = new CompletableFuture<>();
|
||||||
Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
||||||
dynmapAPI.getMarkerAPI().getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).getMarkers()
|
dynmapAPI.getMarkerAPI().getMarkerSet(PUBLIC_HOMES_MARKER_SET_ID).getMarkers().stream()
|
||||||
.stream()
|
.filter(marker -> marker.getMarkerID().equals(home.owner.uuid + ":" + home.uuid))
|
||||||
.filter(marker -> marker.getMarkerID().equals(home.uuid.toString()))
|
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.ifPresent(GenericMarker::deleteMarker);
|
.ifPresent(Marker::deleteMarker);
|
||||||
removedFuture.complete(null);
|
removedFuture.complete(null);
|
||||||
});
|
});
|
||||||
return removedFuture;
|
return removedFuture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Void> clearHomes(@NotNull User user) {
|
||||||
|
final CompletableFuture<Void> 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
|
@Override
|
||||||
public CompletableFuture<Void> updateWarp(@NotNull Warp warp) {
|
public CompletableFuture<Void> updateWarp(@NotNull Warp warp) {
|
||||||
if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null);
|
if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null);
|
||||||
@@ -123,16 +135,26 @@ public class DynMapHook extends MapHook {
|
|||||||
|
|
||||||
final CompletableFuture<Void> removedFuture = new CompletableFuture<>();
|
final CompletableFuture<Void> removedFuture = new CompletableFuture<>();
|
||||||
CompletableFuture.runAsync(() -> Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
CompletableFuture.runAsync(() -> Bukkit.getScheduler().runTask((BukkitHuskHomes) plugin, () -> {
|
||||||
dynmapAPI.getMarkerAPI().getMarkerSet(WARPS_MARKER_SET_ID).getMarkers()
|
dynmapAPI.getMarkerAPI().getMarkerSet(WARPS_MARKER_SET_ID).getMarkers().stream()
|
||||||
.stream()
|
|
||||||
.filter(marker -> marker.getMarkerID().equals(warp.uuid.toString()))
|
.filter(marker -> marker.getMarkerID().equals(warp.uuid.toString()))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.ifPresent(GenericMarker::deleteMarker);
|
.ifPresent(Marker::deleteMarker);
|
||||||
removedFuture.complete(null);
|
removedFuture.complete(null);
|
||||||
}));
|
}));
|
||||||
return removedFuture;
|
return removedFuture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Void> clearWarps() {
|
||||||
|
final CompletableFuture<Void> 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
|
* Creates an HTML Dynmap marker information popup widget
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package net.william278.huskhomes.command;
|
|||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.player.OnlineUser;
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
import net.william278.huskhomes.player.User;
|
import net.william278.huskhomes.player.User;
|
||||||
import net.william278.huskhomes.position.Home;
|
|
||||||
import net.william278.huskhomes.util.Permission;
|
import net.william278.huskhomes.util.Permission;
|
||||||
import net.william278.huskhomes.util.RegexUtil;
|
import net.william278.huskhomes.util.RegexUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -12,7 +11,6 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class DelHomeCommand extends CommandBase implements TabCompletable {
|
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);
|
plugin.getLocales().getLocale("home_deleted", homeName).ifPresent(deleter::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (homeName.equals("all")) {
|
if (homeName.equalsIgnoreCase("all")) {
|
||||||
deleteAllHomes(deleter, homeOwner, delHomeAllConfirm);
|
deleteAllHomes(deleter, homeOwner, delHomeAllConfirm);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -86,7 +84,7 @@ public class DelHomeCommand extends CommandBase implements TabCompletable {
|
|||||||
plugin.getLocales().getLocale("home_deleted_other", homeOwner.username, homeName).ifPresent(deleter::sendMessage);
|
plugin.getLocales().getLocale("home_deleted_other", homeOwner.username, homeName).ifPresent(deleter::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (homeName.equals("all")) {
|
if (homeName.equalsIgnoreCase("all")) {
|
||||||
deleteAllHomes(deleter, homeOwner, delHomeAllConfirm);
|
deleteAllHomes(deleter, homeOwner, delHomeAllConfirm);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -104,26 +102,21 @@ public class DelHomeCommand extends CommandBase implements TabCompletable {
|
|||||||
*/
|
*/
|
||||||
private void deleteAllHomes(@NotNull OnlineUser deleter, @NotNull User homeOwner,
|
private void deleteAllHomes(@NotNull OnlineUser deleter, @NotNull User homeOwner,
|
||||||
final boolean confirm) {
|
final boolean confirm) {
|
||||||
plugin.getDatabase().getHomes(homeOwner).thenAccept(homes -> {
|
|
||||||
if (homes.isEmpty()) {
|
|
||||||
plugin.getLocales().getLocale("error_no_warps_set")
|
|
||||||
.ifPresent(deleter::sendMessage);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!confirm) {
|
if (!confirm) {
|
||||||
plugin.getLocales().getLocale("delete_all_homes_confirm")
|
plugin.getLocales().getLocale("delete_all_homes_confirm")
|
||||||
.ifPresent(deleter::sendMessage);
|
.ifPresent(deleter::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<CompletableFuture<Boolean>> homeDeletionFuture = new ArrayList<>();
|
plugin.getSavedPositionManager().deleteAllHomes(homeOwner).thenAccept(deleted -> {
|
||||||
for (final Home toDelete : homes) {
|
if (deleted == 0) {
|
||||||
homeDeletionFuture.add(plugin.getSavedPositionManager().deleteHome(homeOwner, toDelete.meta.name));
|
plugin.getLocales().getLocale("error_no_warps_set")
|
||||||
|
.ifPresent(deleter::sendMessage);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
CompletableFuture.allOf(homeDeletionFuture.toArray(new CompletableFuture[0]))
|
|
||||||
.thenRun(() -> plugin.getLocales().getLocale("delete_all_homes_success",
|
plugin.getLocales().getLocale("delete_all_homes_success", Integer.toString(deleted))
|
||||||
Integer.toString(homeDeletionFuture.size())).ifPresent(deleter::sendMessage));
|
.ifPresent(deleter::sendMessage);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,15 +2,12 @@ package net.william278.huskhomes.command;
|
|||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.player.OnlineUser;
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
import net.william278.huskhomes.position.Warp;
|
|
||||||
import net.william278.huskhomes.util.Permission;
|
import net.william278.huskhomes.util.Permission;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class DelWarpCommand extends CommandBase implements TabCompletable {
|
public class DelWarpCommand extends CommandBase implements TabCompletable {
|
||||||
@@ -30,7 +27,7 @@ public class DelWarpCommand extends CommandBase implements TabCompletable {
|
|||||||
.ifPresent(onlineUser::sendMessage);
|
.ifPresent(onlineUser::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (warpName.equals("all")) {
|
if (warpName.equalsIgnoreCase("all")) {
|
||||||
deleteAllWarps(onlineUser, confirm);
|
deleteAllWarps(onlineUser, confirm);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -51,26 +48,21 @@ public class DelWarpCommand extends CommandBase implements TabCompletable {
|
|||||||
* @param confirm whether to skip the confirmation prompt
|
* @param confirm whether to skip the confirmation prompt
|
||||||
*/
|
*/
|
||||||
private void deleteAllWarps(@NotNull OnlineUser deleter, final boolean confirm) {
|
private void deleteAllWarps(@NotNull OnlineUser deleter, final boolean confirm) {
|
||||||
plugin.getDatabase().getWarps().thenAccept(warps -> {
|
if (!confirm) {
|
||||||
if (warps.isEmpty()) {
|
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")
|
plugin.getLocales().getLocale("error_no_warps_set")
|
||||||
.ifPresent(deleter::sendMessage);
|
.ifPresent(deleter::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!confirm) {
|
plugin.getLocales().getLocale("delete_all_warps_success", Integer.toString(deleted))
|
||||||
plugin.getLocales().getLocale("confirm_delete_all_warps")
|
|
||||||
.ifPresent(deleter::sendMessage);
|
.ifPresent(deleter::sendMessage);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final List<CompletableFuture<Boolean>> 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));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -443,6 +443,14 @@ public abstract class Database {
|
|||||||
*/
|
*/
|
||||||
public abstract CompletableFuture<Void> deleteHome(@NotNull UUID uuid);
|
public abstract CompletableFuture<Void> 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<Integer> deleteAllHomes(@NotNull User user);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes a {@link Warp} by the given unique id from the warp table on the database.
|
* 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<Void> deleteWarp(@NotNull UUID uuid);
|
public abstract CompletableFuture<Void> 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<Integer> deleteAllWarps();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close any remaining connection to the database source
|
* Close any remaining connection to the database source
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1011,6 +1011,32 @@ public class MySqlDatabase extends Database {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Integer> 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
|
@Override
|
||||||
public CompletableFuture<Void> deleteWarp(@NotNull UUID uuid) {
|
public CompletableFuture<Void> deleteWarp(@NotNull UUID uuid) {
|
||||||
return CompletableFuture.runAsync(() -> {
|
return CompletableFuture.runAsync(() -> {
|
||||||
@@ -1036,6 +1062,29 @@ public class MySqlDatabase extends Database {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Integer> 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
|
@Override
|
||||||
public void terminate() {
|
public void terminate() {
|
||||||
if (dataSource != null) {
|
if (dataSource != null) {
|
||||||
|
|||||||
@@ -1011,6 +1011,32 @@ public class SqLiteDatabase extends Database {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Integer> 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
|
@Override
|
||||||
public CompletableFuture<Void> deleteWarp(@NotNull UUID uuid) {
|
public CompletableFuture<Void> deleteWarp(@NotNull UUID uuid) {
|
||||||
return CompletableFuture.runAsync(() -> {
|
return CompletableFuture.runAsync(() -> {
|
||||||
@@ -1036,6 +1062,29 @@ public class SqLiteDatabase extends Database {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Integer> 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
|
@Override
|
||||||
public void terminate() {
|
public void terminate() {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.william278.huskhomes.event;
|
package net.william278.huskhomes.event;
|
||||||
|
|
||||||
import net.william278.huskhomes.player.OnlineUser;
|
import net.william278.huskhomes.player.OnlineUser;
|
||||||
|
import net.william278.huskhomes.player.User;
|
||||||
import net.william278.huskhomes.position.Home;
|
import net.william278.huskhomes.position.Home;
|
||||||
import net.william278.huskhomes.position.Warp;
|
import net.william278.huskhomes.position.Warp;
|
||||||
import net.william278.huskhomes.teleport.Teleport;
|
import net.william278.huskhomes.teleport.Teleport;
|
||||||
@@ -32,4 +33,7 @@ public interface EventDispatcher {
|
|||||||
|
|
||||||
CompletableFuture<IWarpListEvent> dispatchViewWarpListEvent(@NotNull List<Warp> homes, @NotNull OnlineUser user);
|
CompletableFuture<IWarpListEvent> dispatchViewWarpListEvent(@NotNull List<Warp> homes, @NotNull OnlineUser user);
|
||||||
|
|
||||||
|
CompletableFuture<IDeleteAllHomesEvent> dispatchDeleteAllHomesEvent(@NotNull User user);
|
||||||
|
|
||||||
|
CompletableFuture<IDeleteAllWarpsEvent> dispatchDeleteAllWarpsEvent();
|
||||||
}
|
}
|
||||||
@@ -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();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import net.william278.huskhomes.position.Home;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
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 {
|
public interface IHomeDeleteEvent extends CancellableEvent {
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import net.william278.huskhomes.position.Warp;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
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 {
|
public interface IWarpDeleteEvent extends CancellableEvent {
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import de.bluecolored.bluemap.api.BlueMapWorld;
|
|||||||
import de.bluecolored.bluemap.api.markers.MarkerSet;
|
import de.bluecolored.bluemap.api.markers.MarkerSet;
|
||||||
import de.bluecolored.bluemap.api.markers.POIMarker;
|
import de.bluecolored.bluemap.api.markers.POIMarker;
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
|
import net.william278.huskhomes.player.User;
|
||||||
import net.william278.huskhomes.position.Home;
|
import net.william278.huskhomes.position.Home;
|
||||||
import net.william278.huskhomes.position.Warp;
|
import net.william278.huskhomes.position.Warp;
|
||||||
import net.william278.huskhomes.position.World;
|
import net.william278.huskhomes.position.World;
|
||||||
@@ -73,7 +74,7 @@ public class BlueMapHook extends MapHook {
|
|||||||
blueMapAPI -> getBlueMapWorld(blueMapAPI, home.world)).ifPresent(blueMapWorld -> blueMapWorld.getMaps()
|
blueMapAPI -> getBlueMapWorld(blueMapAPI, home.world)).ifPresent(blueMapWorld -> blueMapWorld.getMaps()
|
||||||
.forEach(blueMapMap -> blueMapMap.getMarkerSets()
|
.forEach(blueMapMap -> blueMapMap.getMarkerSets()
|
||||||
.computeIfPresent(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> {
|
.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()
|
POIMarker.toBuilder()
|
||||||
.label("/phome" + home.owner.username + "." + home.meta.name)
|
.label("/phome" + home.owner.username + "." + home.meta.name)
|
||||||
.position((int) home.x, (int) home.y, (int) home.z)
|
.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))
|
BlueMapAPI.getInstance().flatMap(blueMapAPI -> getBlueMapWorld(blueMapAPI, home.world))
|
||||||
.ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(blueMapMap -> blueMapMap.getMarkerSets()
|
.ifPresent(blueMapWorld -> blueMapWorld.getMaps().forEach(blueMapMap -> blueMapMap.getMarkerSets()
|
||||||
.computeIfPresent(blueMapWorld.getId() + ":" + PUBLIC_HOMES_MARKER_SET_ID, (s, markerSet) -> {
|
.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 markerSet;
|
||||||
})));
|
})));
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Void> 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
|
@Override
|
||||||
public CompletableFuture<Void> updateWarp(@NotNull Warp warp) {
|
public CompletableFuture<Void> updateWarp(@NotNull Warp warp) {
|
||||||
if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null);
|
if (!isValidPosition(warp)) return CompletableFuture.completedFuture(null);
|
||||||
@@ -128,6 +142,19 @@ public class BlueMapHook extends MapHook {
|
|||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Void> 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
|
* Get the {@link BlueMapWorld} for a world
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.william278.huskhomes.hook;
|
|||||||
|
|
||||||
import net.william278.huskhomes.HuskHomes;
|
import net.william278.huskhomes.HuskHomes;
|
||||||
import net.william278.huskhomes.HuskHomesException;
|
import net.william278.huskhomes.HuskHomesException;
|
||||||
|
import net.william278.huskhomes.player.User;
|
||||||
import net.william278.huskhomes.position.Home;
|
import net.william278.huskhomes.position.Home;
|
||||||
import net.william278.huskhomes.position.SavedPosition;
|
import net.william278.huskhomes.position.SavedPosition;
|
||||||
import net.william278.huskhomes.position.Warp;
|
import net.william278.huskhomes.position.Warp;
|
||||||
@@ -60,6 +61,14 @@ public abstract class MapHook extends PluginHook {
|
|||||||
*/
|
*/
|
||||||
public abstract CompletableFuture<Void> removeHome(@NotNull Home home);
|
public abstract CompletableFuture<Void> 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<Void> 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
|
* 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<Void> removeWarp(@NotNull Warp warp);
|
public abstract CompletableFuture<Void> 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<Void> clearWarps();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns if the position is valid to be set on this server
|
* Returns if the position is valid to be set on this server
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -152,6 +152,20 @@ public class SavedPositionManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CompletableFuture<Integer> 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
|
* Tries to update an existing home with new metadata
|
||||||
*
|
*
|
||||||
@@ -320,6 +334,18 @@ public class SavedPositionManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CompletableFuture<Integer> 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
|
* Tries to update an existing warp with new metadata
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in new issue
Block a user