Add an option to relocate homes/warps with /sethome and /setwarp with a same-named homes

This commit is contained in:
William committed 2022-10-29 18:02:01 +01:00
1 parent e20dcc4ce4
commit 729cb5870f
3 files changed
+41 -2

No files matched your search

@@ -103,6 +103,9 @@ public class Settings {
@YamlKey("general.permission_restrict_warps")
public boolean permissionRestrictWarps = false;
@YamlKey("general.overwrite_existing_homes_warps")
public boolean overwriteExistingHomesWarps = true;
@YamlKey("general.teleport_warmup_time")
public int teleportWarmupTime = 5;
@@ -49,13 +49,20 @@ public class SavedPositionManager {
*/
private final boolean allowUnicodeDescriptions;
/**
* Whether to automatically relocate existing homes and warps when a user tries to set a home/warp with the same name
*/
private final boolean overwriteExistingHomesWarps;
public SavedPositionManager(@NotNull Database database, @NotNull Cache cache, @NotNull EventDispatcher eventDispatcher,
boolean allowUnicodeNames, boolean allowUnicodeDescriptions) {
boolean allowUnicodeNames, boolean allowUnicodeDescriptions,
boolean overwriteExistingHomesWarps) {
this.database = database;
this.cache = cache;
this.eventDispatcher = eventDispatcher;
this.allowUnicodeNames = allowUnicodeNames;
this.allowUnicodeDescriptions = allowUnicodeDescriptions;
this.overwriteExistingHomesWarps = overwriteExistingHomesWarps;
}
/**
@@ -81,6 +88,7 @@ public class SavedPositionManager {
return database.getHome(homeOwner, homeMeta.name).thenApplyAsync(optionalHome ->
validateMeta(homeMeta).map(resultType -> new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
if (optionalHome.isEmpty()) {
// Handle setting a new home
final Home home = new Home(position, homeMeta, homeOwner);
return eventDispatcher.dispatchHomeSaveEvent(home).thenApplyAsync(event -> {
if (event.isCancelled()) {
@@ -92,6 +100,18 @@ public class SavedPositionManager {
return database.saveHome(home).thenApplyAsync(value ->
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
}).join();
} else if (overwriteExistingHomesWarps) {
// Handle overwriting existing homes if the option is enabled
final Home existingHome = optionalHome.get();
updateHomePosition(existingHome, position).thenApplyAsync(result -> {
if (result) {
if (mapHook != null && existingHome.isPublic) {
mapHook.updateHome(existingHome);
}
return new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(existingHome));
}
return new SaveResult(SaveResult.ResultType.FAILED_EVENT_CANCELLED, Optional.empty());
}).join();
}
return new SaveResult(SaveResult.ResultType.FAILED_DUPLICATE, Optional.empty());
}));
@@ -202,6 +222,7 @@ public class SavedPositionManager {
return false;
}
// Save the home to database and propagate updates to cache and map hook
return database.saveHome(home).thenApplyAsync(ignored -> {
if (home.isPublic) {
cache.publicHomes.putIfAbsent(home.owner.username, new ArrayList<>());
@@ -236,6 +257,7 @@ public class SavedPositionManager {
return database.getWarp(warpMeta.name).thenApplyAsync(optionalWarp ->
validateMeta(warpMeta).map(resultType -> new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
if (optionalWarp.isEmpty()) {
// Handle setting new warps
final Warp warp = new Warp(position, warpMeta);
return eventDispatcher.dispatchWarpSaveEvent(warp).thenApplyAsync(event -> {
if (event.isCancelled()) {
@@ -249,6 +271,18 @@ public class SavedPositionManager {
return database.saveWarp(warp).thenApplyAsync(ignored ->
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
}).join();
} else if (overwriteExistingHomesWarps) {
// Handle overwriting existing warps if the option is enabled
final Warp existingWarp = optionalWarp.get();
updateWarpPosition(existingWarp, position).thenApplyAsync(result -> {
if (result) {
if (mapHook != null) {
mapHook.updateWarp(existingWarp);
}
return new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(existingWarp));
}
return new SaveResult(SaveResult.ResultType.FAILED_EVENT_CANCELLED, Optional.empty());
}).join();
}
return new SaveResult(SaveResult.ResultType.FAILED_DUPLICATE, Optional.empty());
}));
@@ -268,6 +302,8 @@ public class SavedPositionManager {
if (event.isCancelled()) {
return false;
}
// Delete the warp from database and propagate updates to cache and map hook
final Warp warp = event.getWarp();
return database.deleteWarp(warp.uuid).thenApplyAsync(ignored -> {
cache.warps.remove(warp.meta.name);