mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 22:16:08 +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
+379
-51
No files matched your search
@@ -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<CompletableFuture<Boolean>> 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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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<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));
|
||||
plugin.getLocales().getLocale("delete_all_warps_success", Integer.toString(deleted))
|
||||
.ifPresent(deleter::sendMessage);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -443,6 +443,14 @@ public abstract class Database {
|
||||
*/
|
||||
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.
|
||||
*
|
||||
@@ -451,6 +459,13 @@ public abstract class Database {
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
public CompletableFuture<Void> deleteWarp(@NotNull UUID uuid) {
|
||||
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
|
||||
public void terminate() {
|
||||
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
|
||||
public CompletableFuture<Void> deleteWarp(@NotNull UUID uuid) {
|
||||
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
|
||||
public void terminate() {
|
||||
try {
|
||||
|
||||
@@ -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<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;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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<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
|
||||
public CompletableFuture<Void> 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<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
|
||||
*
|
||||
|
||||
@@ -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<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
|
||||
*
|
||||
@@ -75,6 +84,13 @@ public abstract class MapHook extends PluginHook {
|
||||
*/
|
||||
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
|
||||
*
|
||||
|
||||
@@ -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
|
||||
*
|
||||
@@ -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
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user