mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 14:38:53 +00:00
Add an option to relocate homes/warps with /sethome and /setwarp with a same-named homes
This commit is contained in:
1 parent
e20dcc4ce4
commit
729cb5870f
3 files changed
+41
-2
No files matched your search
@@ -167,7 +167,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
|
|||||||
|
|
||||||
// Prepare the home and warp position manager
|
// Prepare the home and warp position manager
|
||||||
this.savedPositionManager = new SavedPositionManager(database, cache, eventDispatcher,
|
this.savedPositionManager = new SavedPositionManager(database, cache, eventDispatcher,
|
||||||
settings.allowUnicodeNames, settings.allowUnicodeDescriptions);
|
settings.allowUnicodeNames, settings.allowUnicodeDescriptions, settings.overwriteExistingHomesWarps);
|
||||||
|
|
||||||
// Initialize the RTP engine with the default normal distribution engine
|
// Initialize the RTP engine with the default normal distribution engine
|
||||||
setRandomTeleportEngine(new NormalDistributionEngine(this));
|
setRandomTeleportEngine(new NormalDistributionEngine(this));
|
||||||
|
|||||||
@@ -103,6 +103,9 @@ public class Settings {
|
|||||||
@YamlKey("general.permission_restrict_warps")
|
@YamlKey("general.permission_restrict_warps")
|
||||||
public boolean permissionRestrictWarps = false;
|
public boolean permissionRestrictWarps = false;
|
||||||
|
|
||||||
|
@YamlKey("general.overwrite_existing_homes_warps")
|
||||||
|
public boolean overwriteExistingHomesWarps = true;
|
||||||
|
|
||||||
@YamlKey("general.teleport_warmup_time")
|
@YamlKey("general.teleport_warmup_time")
|
||||||
public int teleportWarmupTime = 5;
|
public int teleportWarmupTime = 5;
|
||||||
|
|
||||||
|
|||||||
@@ -49,13 +49,20 @@ public class SavedPositionManager {
|
|||||||
*/
|
*/
|
||||||
private final boolean allowUnicodeDescriptions;
|
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,
|
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.database = database;
|
||||||
this.cache = cache;
|
this.cache = cache;
|
||||||
this.eventDispatcher = eventDispatcher;
|
this.eventDispatcher = eventDispatcher;
|
||||||
this.allowUnicodeNames = allowUnicodeNames;
|
this.allowUnicodeNames = allowUnicodeNames;
|
||||||
this.allowUnicodeDescriptions = allowUnicodeDescriptions;
|
this.allowUnicodeDescriptions = allowUnicodeDescriptions;
|
||||||
|
this.overwriteExistingHomesWarps = overwriteExistingHomesWarps;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -81,6 +88,7 @@ public class SavedPositionManager {
|
|||||||
return database.getHome(homeOwner, homeMeta.name).thenApplyAsync(optionalHome ->
|
return database.getHome(homeOwner, homeMeta.name).thenApplyAsync(optionalHome ->
|
||||||
validateMeta(homeMeta).map(resultType -> new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
|
validateMeta(homeMeta).map(resultType -> new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
|
||||||
if (optionalHome.isEmpty()) {
|
if (optionalHome.isEmpty()) {
|
||||||
|
// Handle setting a new home
|
||||||
final Home home = new Home(position, homeMeta, homeOwner);
|
final Home home = new Home(position, homeMeta, homeOwner);
|
||||||
return eventDispatcher.dispatchHomeSaveEvent(home).thenApplyAsync(event -> {
|
return eventDispatcher.dispatchHomeSaveEvent(home).thenApplyAsync(event -> {
|
||||||
if (event.isCancelled()) {
|
if (event.isCancelled()) {
|
||||||
@@ -92,6 +100,18 @@ public class SavedPositionManager {
|
|||||||
return database.saveHome(home).thenApplyAsync(value ->
|
return database.saveHome(home).thenApplyAsync(value ->
|
||||||
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
|
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
|
||||||
}).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());
|
return new SaveResult(SaveResult.ResultType.FAILED_DUPLICATE, Optional.empty());
|
||||||
}));
|
}));
|
||||||
@@ -202,6 +222,7 @@ public class SavedPositionManager {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the home to database and propagate updates to cache and map hook
|
||||||
return database.saveHome(home).thenApplyAsync(ignored -> {
|
return database.saveHome(home).thenApplyAsync(ignored -> {
|
||||||
if (home.isPublic) {
|
if (home.isPublic) {
|
||||||
cache.publicHomes.putIfAbsent(home.owner.username, new ArrayList<>());
|
cache.publicHomes.putIfAbsent(home.owner.username, new ArrayList<>());
|
||||||
@@ -236,6 +257,7 @@ public class SavedPositionManager {
|
|||||||
return database.getWarp(warpMeta.name).thenApplyAsync(optionalWarp ->
|
return database.getWarp(warpMeta.name).thenApplyAsync(optionalWarp ->
|
||||||
validateMeta(warpMeta).map(resultType -> new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
|
validateMeta(warpMeta).map(resultType -> new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
|
||||||
if (optionalWarp.isEmpty()) {
|
if (optionalWarp.isEmpty()) {
|
||||||
|
// Handle setting new warps
|
||||||
final Warp warp = new Warp(position, warpMeta);
|
final Warp warp = new Warp(position, warpMeta);
|
||||||
return eventDispatcher.dispatchWarpSaveEvent(warp).thenApplyAsync(event -> {
|
return eventDispatcher.dispatchWarpSaveEvent(warp).thenApplyAsync(event -> {
|
||||||
if (event.isCancelled()) {
|
if (event.isCancelled()) {
|
||||||
@@ -249,6 +271,18 @@ public class SavedPositionManager {
|
|||||||
return database.saveWarp(warp).thenApplyAsync(ignored ->
|
return database.saveWarp(warp).thenApplyAsync(ignored ->
|
||||||
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
|
new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
|
||||||
}).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());
|
return new SaveResult(SaveResult.ResultType.FAILED_DUPLICATE, Optional.empty());
|
||||||
}));
|
}));
|
||||||
@@ -268,6 +302,8 @@ public class SavedPositionManager {
|
|||||||
if (event.isCancelled()) {
|
if (event.isCancelled()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete the warp from database and propagate updates to cache and map hook
|
||||||
final Warp warp = event.getWarp();
|
final Warp warp = event.getWarp();
|
||||||
return database.deleteWarp(warp.uuid).thenApplyAsync(ignored -> {
|
return database.deleteWarp(warp.uuid).thenApplyAsync(ignored -> {
|
||||||
cache.warps.remove(warp.meta.name);
|
cache.warps.remove(warp.meta.name);
|
||||||
|
|||||||
Reference in new issue
Block a user