> getUserOfflinePosition(@NotNull User user) {
+ return plugin.getDatabase().getOfflinePosition(user);
+ }
+
+ /**
+ * Returns where the user last set their spawn point by right-clicking a bed or respawn anchor
+ *
+ * The optional returned by this method will be empty unless HuskHomes is running cross-server mode
+ * and global respawning is enabled
+ *
+ * @param user The {@link User} to get the respawn position of
+ * @return The user's respawn {@link Position} if they have one, otherwise an empty {@link Optional}
+ * @since 3.0
+ */
+ public CompletableFuture> getUserRespawnPosition(@NotNull User user) {
+ if (!plugin.getSettings().crossServer || plugin.getSettings().globalRespawning) {
+ return CompletableFuture.completedFuture(Optional.empty());
+ }
+ return plugin.getDatabase().getRespawnPosition(user);
+ }
+
+ /**
+ * Save {@link UserData} to the database, updating it if data for the user already exists, or adding new user data if it doesn't.
+ *
+ * @param userData The {@link UserData} to save
+ * @return A {@link CompletableFuture} that will complete when the data has been saved
+ * @since 3.0
+ */
+ public final CompletableFuture saveUserData(@NotNull UserData userData) {
+ return plugin.getDatabase().updateUserData(userData);
+ }
+
+ /**
+ * Get a list of {@link Home}s set by the given user.
+ *
+ * @param user The {@link User} to get the homes of
+ * @return A {@link CompletableFuture} that will complete with a list of {@link Home}s set by the user
+ */
+ public final CompletableFuture> getUserHomes(@NotNull User user) {
+ return plugin.getDatabase().getHomes(user);
+ }
+
+ /**
+ * Get a list of {@link Home}s set by the given user that they have made public
+ *
+ * @param user The {@link User} to get the public homes of
+ * @return A {@link CompletableFuture} that will complete with a list of {@link Home}s set by the user that
+ * they have made public (where {@link Home#isPublic})
+ * @since 3.0
+ */
+ public final CompletableFuture> getUserPublicHomes(@NotNull User user) {
+ return getUserHomes(user).thenApply(homes -> homes.stream()
+ .filter(home -> home.isPublic)
+ .collect(Collectors.toList()));
+ }
+
+ /**
+ * Get a list of homes that have been made public ("public homes")
+ *
+ * @return A {@link CompletableFuture} that will complete with a list of public {@link Home}s
+ * @since 3.0
+ */
+ public final CompletableFuture> getPublicHomes() {
+ return plugin.getDatabase().getPublicHomes();
+ }
+
+ /**
+ * Get a list of {@link Warp}s
+ *
+ * @return A {@link CompletableFuture} that will complete with a list of {@link Warp}s
+ * @since 3.0
+ */
+ public final CompletableFuture> getWarps() {
+ return plugin.getDatabase().getWarps();
+ }
+
+ /**
+ * Attempt to teleport an {@link OnlineUser} to a target {@link Position}
+ *
+ * @param user The {@link OnlineUser} to teleport
+ * @param position The {@link Position} to teleport the user to
+ * @param timedTeleport Whether the teleport should be timed or not (requiring a warmup where they must stand still
+ * for a period of time)
+ * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult} indicating the result of
+ * completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}.
+ * @since 3.0
+ */
+ public final CompletableFuture teleportPlayer(@NotNull OnlineUser user, @NotNull Position position,
+ boolean timedTeleport) {
+ return timedTeleport ? plugin.getTeleportManager().timedTeleport(user, position)
+ : plugin.getTeleportManager().teleport(user, position);
+ }
+
+ /**
+ * Attempt to teleport an {@link OnlineUser} to a target {@link Position}
+ *
+ * @param user The {@link OnlineUser} to teleport
+ * @param position The {@link Position} to teleport the user to
+ * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult} indicating the result of
+ * completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}.
+ * @since 3.0
+ */
+ public final CompletableFuture teleportPlayer(@NotNull OnlineUser user, @NotNull Position position) {
+ return teleportPlayer(user, position, false);
+ }
+
+ /**
+ * Attempt to teleport an {@link OnlineUser} to a randomly generated {@link Position}. The {@link Position} will be
+ * generated by the current {@link RandomTeleportEngine}
+ *
+ * @param user The {@link OnlineUser} to teleport
+ * @param timedTeleport Whether the teleport should be timed or not (requiring a warmup where they must stand still
+ * for a period of time)
+ * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult} indicating the result of
+ * completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}.
+ * @since 3.0
+ */
+ public final CompletableFuture randomlyTeleportPlayer(@NotNull OnlineUser user, boolean timedTeleport) {
+ return CompletableFuture.supplyAsync(() -> plugin.getRandomTeleportEngine().getRandomPosition(user.getPosition())
+ .thenApply(position -> {
+ if (position.isPresent()) {
+ return teleportPlayer(user, position.get(), timedTeleport).join();
+ } else {
+ return TeleportResult.CANCELLED;
+ }
+ }).join());
+ }
+
+ /**
+ * Attempt to teleport an {@link OnlineUser} to a randomly generated {@link Position}. The {@link Position} will be
+ * generated by the current {@link RandomTeleportEngine}
+ *
+ * @param user The {@link OnlineUser} to teleport
+ * @return A {@link CompletableFuture} that will complete with a {@link TeleportResult} indicating the result of
+ * completing the teleport. If the teleport was successful, the {@link TeleportResult#successful} will be {@code true}.
+ * @since 3.0
+ */
+ public final CompletableFuture randomlyTeleportPlayer(@NotNull OnlineUser user) {
+ return randomlyTeleportPlayer(user, false);
+ }
+
+ /**
+ * Set the {@link RandomTeleportEngine} to use for processing random teleports. The engine will be used to process all
+ * random teleports, including both {@code /rtp} command executions and API ({@link #randomlyTeleportPlayer(OnlineUser)})
+ * calls.
+ *
+ * @param randomTeleportEngine the {@link RandomTeleportEngine} to use to process random teleports
+ * @see RandomTeleportEngine
+ * @since 3.0
+ */
+ public final void setRandomTeleportEngine(@NotNull RandomTeleportEngine randomTeleportEngine) {
+ plugin.setRandomTeleportEngine(randomTeleportEngine);
+ }
+
+}
diff --git a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java
index af27a1e6..4d778456 100644
--- a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java
+++ b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java
@@ -72,7 +72,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
}
// Get a random position and teleport
- plugin.getRtpEngine().getRandomPosition(userPosition)
+ plugin.getRandomTeleportEngine().getRandomPosition(userPosition)
.thenAccept(position -> {
if (position.isEmpty()) {
plugin.getLocales().getLocale("error_rtp_randomization_timeout")
@@ -107,7 +107,7 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
}
plugin.getLoggingAdapter().log(Level.INFO, "Finding a random position for " + foundUser.get().username + "...");
- plugin.getRtpEngine().getRandomPosition(foundUser.get().getPosition()).thenAccept(position -> {
+ plugin.getRandomTeleportEngine().getRandomPosition(foundUser.get().getPosition()).thenAccept(position -> {
if (position.isEmpty()) {
plugin.getLoggingAdapter().log(Level.WARNING, "Failed to teleport " + foundUser.get().username + " to a random position; randomization timed out!");
return;
diff --git a/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java b/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java
index 941f28d9..8c6d0c1e 100644
--- a/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java
+++ b/common/src/main/java/net/william278/huskhomes/command/SetSpawnCommand.java
@@ -19,25 +19,31 @@ public class SetSpawnCommand extends CommandBase {
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
- CompletableFuture.runAsync(() -> {
+ CompletableFuture.supplyAsync(() -> {
if (args.length > 0) {
plugin.getLocales().getLocale("error_invalid_syntax", "/setspawn")
.ifPresent(onlineUser::sendMessage);
- return;
+ return false;
}
final Position position = onlineUser.getPosition();
if (plugin.getSettings().crossServer && plugin.getSettings().globalSpawn) {
final Optional warp = plugin.getDatabase().getWarp(plugin.getSettings().globalSpawnName).join();
if (warp.isPresent()) {
- plugin.getSavedPositionManager().updateWarpPosition(warp.get(), position).join();
+ return plugin.getSavedPositionManager().updateWarpPosition(warp.get(), position).join();
} else {
- plugin.getSavedPositionManager().setWarp(new PositionMeta(plugin.getSettings().globalSpawnName,
+ return plugin.getSavedPositionManager().setWarp(new PositionMeta(plugin.getSettings().globalSpawnName,
plugin.getLocales().getRawLocale("spawn_warp_default_description")
- .orElse("")), position).join();
+ .orElse("")), position).join().resultType().successful;
}
} else {
plugin.setServerSpawn(position);
+ return true;
}
- }).thenRun(() -> plugin.getLocales().getLocale("set_spawn_success").ifPresent(onlineUser::sendMessage));
+ }).thenAccept(succeeded -> {
+ if (succeeded) {
+ plugin.getLocales().getLocale("set_spawn_success")
+ .ifPresent(onlineUser::sendMessage);
+ }
+ });
}
}
diff --git a/common/src/main/java/net/william278/huskhomes/event/CancellableEvent.java b/common/src/main/java/net/william278/huskhomes/event/CancellableEvent.java
new file mode 100644
index 00000000..f548a479
--- /dev/null
+++ b/common/src/main/java/net/william278/huskhomes/event/CancellableEvent.java
@@ -0,0 +1,22 @@
+package net.william278.huskhomes.event;
+
+/**
+ * Base interface for a cancellable event
+ */
+public interface CancellableEvent {
+
+ /**
+ * Set whether the event should be cancelled
+ *
+ * @param cancelled {@code true} if the event should be cancelled
+ */
+ void setCancelled(boolean cancelled);
+
+ /**
+ * Check whether the event is cancelled
+ *
+ * @return {@code true} if the event is cancelled
+ */
+ boolean isCancelled();
+
+}
diff --git a/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java b/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java
new file mode 100644
index 00000000..b31b14f4
--- /dev/null
+++ b/common/src/main/java/net/william278/huskhomes/event/EventDispatcher.java
@@ -0,0 +1,25 @@
+package net.william278.huskhomes.event;
+
+import net.william278.huskhomes.position.Home;
+import net.william278.huskhomes.position.Warp;
+import net.william278.huskhomes.teleport.Teleport;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * An abstract dispatcher of events
+ */
+public interface EventDispatcher {
+
+ CompletableFuture dispatchTeleportEvent(@NotNull Teleport teleport);
+
+ CompletableFuture dispatchHomeSaveEvent(@NotNull Home home);
+
+ CompletableFuture dispatchHomeDeleteEvent(@NotNull Home home);
+
+ CompletableFuture dispatchWarpSaveEvent(@NotNull Warp warp);
+
+ CompletableFuture dispatchWarpDeleteEvent(@NotNull Warp warp);
+
+}
diff --git a/common/src/main/java/net/william278/huskhomes/event/IHomeDeleteEvent.java b/common/src/main/java/net/william278/huskhomes/event/IHomeDeleteEvent.java
new file mode 100644
index 00000000..5d7243b1
--- /dev/null
+++ b/common/src/main/java/net/william278/huskhomes/event/IHomeDeleteEvent.java
@@ -0,0 +1,19 @@
+package net.william278.huskhomes.event;
+
+import net.william278.huskhomes.position.Home;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Representation of an event that fires when a home is deleted
+ */
+public interface IHomeDeleteEvent extends CancellableEvent {
+
+ /**
+ * Get the home being deleted
+ *
+ * @return the {@link Home} being deleted
+ */
+ @NotNull
+ Home getHome();
+
+}
diff --git a/common/src/main/java/net/william278/huskhomes/event/IHomeSaveEvent.java b/common/src/main/java/net/william278/huskhomes/event/IHomeSaveEvent.java
new file mode 100644
index 00000000..cd05f7d7
--- /dev/null
+++ b/common/src/main/java/net/william278/huskhomes/event/IHomeSaveEvent.java
@@ -0,0 +1,19 @@
+package net.william278.huskhomes.event;
+
+import net.william278.huskhomes.position.Home;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Representation of an event that fires when a home is created or updated
+ */
+public interface IHomeSaveEvent extends CancellableEvent {
+
+ /**
+ * Get the home being created or updated
+ *
+ * @return the {@link Home} being saved
+ */
+ @NotNull
+ Home getHome();
+
+}
diff --git a/common/src/main/java/net/william278/huskhomes/event/ITeleportEvent.java b/common/src/main/java/net/william278/huskhomes/event/ITeleportEvent.java
new file mode 100644
index 00000000..081a2645
--- /dev/null
+++ b/common/src/main/java/net/william278/huskhomes/event/ITeleportEvent.java
@@ -0,0 +1,19 @@
+package net.william278.huskhomes.event;
+
+import net.william278.huskhomes.teleport.Teleport;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Representation of an event that fires when a teleport is about to occur
+ */
+public interface ITeleportEvent {
+
+ /**
+ * Get the teleport being carried out
+ *
+ * @return the {@link Teleport} being carried out
+ */
+ @NotNull
+ Teleport getTeleport();
+
+}
diff --git a/common/src/main/java/net/william278/huskhomes/event/IWarpDeleteEvent.java b/common/src/main/java/net/william278/huskhomes/event/IWarpDeleteEvent.java
new file mode 100644
index 00000000..f02a6dc0
--- /dev/null
+++ b/common/src/main/java/net/william278/huskhomes/event/IWarpDeleteEvent.java
@@ -0,0 +1,19 @@
+package net.william278.huskhomes.event;
+
+import net.william278.huskhomes.position.Warp;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Representation of an event that fires when a warp is deleted
+ */
+public interface IWarpDeleteEvent extends CancellableEvent {
+
+ /**
+ * Get the warp being deleted
+ *
+ * @return the {@link Warp} being deleted
+ */
+ @NotNull
+ Warp getWarp();
+
+}
diff --git a/common/src/main/java/net/william278/huskhomes/event/IWarpSaveEvent.java b/common/src/main/java/net/william278/huskhomes/event/IWarpSaveEvent.java
new file mode 100644
index 00000000..43324eb0
--- /dev/null
+++ b/common/src/main/java/net/william278/huskhomes/event/IWarpSaveEvent.java
@@ -0,0 +1,19 @@
+package net.william278.huskhomes.event;
+
+import net.william278.huskhomes.position.Warp;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Representation of an event that fires when a warp is created or updated
+ */
+public interface IWarpSaveEvent extends CancellableEvent {
+
+ /**
+ * Get the warp being created or updated
+ *
+ * @return the {@link Warp} being saved
+ */
+ @NotNull
+ Warp getWarp();
+
+}
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 439182ac..44fd632f 100644
--- a/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java
+++ b/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java
@@ -2,6 +2,7 @@ package net.william278.huskhomes.position;
import net.william278.huskhomes.Cache;
import net.william278.huskhomes.database.Database;
+import net.william278.huskhomes.event.EventDispatcher;
import net.william278.huskhomes.hook.MapHook;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.util.RegexUtil;
@@ -27,6 +28,11 @@ public class SavedPositionManager {
*/
private final Cache cache;
+ /**
+ * The {@link EventDispatcher} used to dispatch save and delete events
+ */
+ private final EventDispatcher eventDispatcher;
+
/**
* The {@link MapHook} used to update the map when a home or warp is set
*/
@@ -43,10 +49,11 @@ public class SavedPositionManager {
*/
private final boolean allowUnicodeDescriptions;
- public SavedPositionManager(@NotNull Database database, @NotNull Cache cache,
+ public SavedPositionManager(@NotNull Database database, @NotNull Cache cache, @NotNull EventDispatcher eventDispatcher,
boolean allowUnicodeNames, boolean allowUnicodeDescriptions) {
this.database = database;
this.cache = cache;
+ this.eventDispatcher = eventDispatcher;
this.allowUnicodeNames = allowUnicodeNames;
this.allowUnicodeDescriptions = allowUnicodeDescriptions;
}
@@ -75,11 +82,16 @@ public class SavedPositionManager {
validateMeta(homeMeta).map(resultType -> new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
if (optionalHome.isEmpty()) {
final Home home = new Home(position, homeMeta, homeOwner);
- cache.homes.putIfAbsent(home.owner.uuid, new ArrayList<>());
- cache.homes.get(home.owner.uuid).add(home.meta.name);
- cache.privateHomeLists.remove(home.owner.uuid);
- return database.setHome(home).thenApply(value ->
- new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
+ return eventDispatcher.dispatchHomeSaveEvent(home).thenApply(event -> {
+ if (event.isCancelled()) {
+ return new SaveResult(SaveResult.ResultType.FAILED_EVENT_CANCELLED, Optional.empty());
+ }
+ cache.homes.putIfAbsent(home.owner.uuid, new ArrayList<>());
+ cache.homes.get(home.owner.uuid).add(home.meta.name);
+ cache.privateHomeLists.remove(home.owner.uuid);
+ return database.setHome(home).thenApply(value ->
+ new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
+ }).join();
}
return new SaveResult(SaveResult.ResultType.FAILED_DUPLICATE, Optional.empty());
}));
@@ -96,18 +108,23 @@ public class SavedPositionManager {
public CompletableFuture deleteHome(@NotNull User homeOwner, @NotNull String homeName) {
return database.getHome(homeOwner, homeName).thenApply(optionalHome -> {
if (optionalHome.isPresent()) {
- final Home home = optionalHome.get();
- return database.deleteHome(home.uuid).thenApply(ignored -> {
- cache.homes.computeIfPresent(home.owner.uuid, (ownerUUID, homeNames) -> {
- homeNames.remove(home.meta.name);
- return homeNames;
- });
- cache.privateHomeLists.remove(home.owner.uuid);
- cache.publicHomeLists.clear();
- if (mapHook != null) {
- mapHook.removeHome(home);
+ return eventDispatcher.dispatchHomeDeleteEvent(optionalHome.get()).thenApply(event -> {
+ if (event.isCancelled()) {
+ return false;
}
- return true;
+ final Home home = event.getHome();
+ return database.deleteHome(home.uuid).thenApply(ignored -> {
+ cache.homes.computeIfPresent(home.owner.uuid, (ownerUUID, homeNames) -> {
+ homeNames.remove(home.meta.name);
+ return homeNames;
+ });
+ cache.privateHomeLists.remove(home.owner.uuid);
+ cache.publicHomeLists.clear();
+ if (mapHook != null) {
+ mapHook.removeHome(home);
+ }
+ return true;
+ }).join();
}).join();
}
return false;
@@ -148,17 +165,22 @@ public class SavedPositionManager {
return validateMeta(newHomeMeta).map(resultType ->
new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
home.meta = newHomeMeta;
- if (!existingHomeName.equals(newHomeMeta.name)) {
- cache.homes.get(home.owner.uuid).remove(existingHomeName);
- cache.homes.get(home.owner.uuid).add(newHomeMeta.name);
- }
- cache.privateHomeLists.remove(home.owner.uuid);
- cache.publicHomeLists.clear();
- if (home.isPublic && mapHook != null) {
- mapHook.updateHome(home);
- }
- return database.setHome(home).thenApply(value ->
- new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
+ return eventDispatcher.dispatchHomeSaveEvent(home).thenApply(event -> {
+ if (event.isCancelled()) {
+ return new SaveResult(SaveResult.ResultType.FAILED_EVENT_CANCELLED, Optional.empty());
+ }
+ if (!existingHomeName.equals(newHomeMeta.name)) {
+ cache.homes.get(home.owner.uuid).remove(existingHomeName);
+ cache.homes.get(home.owner.uuid).add(newHomeMeta.name);
+ }
+ cache.privateHomeLists.remove(home.owner.uuid);
+ cache.publicHomeLists.clear();
+ if (home.isPublic && mapHook != null) {
+ mapHook.updateHome(home);
+ }
+ return database.setHome(home).thenApply(value ->
+ new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(home))).join();
+ }).join();
});
});
}
@@ -197,10 +219,15 @@ public class SavedPositionManager {
home.yaw = newPosition.yaw;
home.world = newPosition.world;
home.server = newPosition.server;
- if (home.isPublic && mapHook != null) {
- mapHook.updateHome(home);
- }
- return database.setHome(home).thenApply(ignored -> true);
+ return eventDispatcher.dispatchHomeSaveEvent(home).thenApply(event -> {
+ if (event.isCancelled()) {
+ return false;
+ }
+ if (home.isPublic && mapHook != null) {
+ mapHook.updateHome(home);
+ }
+ return database.setHome(home).thenApply(ignored -> true).join();
+ });
}
/**
@@ -231,24 +258,30 @@ public class SavedPositionManager {
*/
public CompletableFuture updateHomePrivacy(@NotNull Home home, final boolean isHomePublic) {
home.isPublic = isHomePublic;
- return database.setHome(home).thenApply(ignored -> {
- if (isHomePublic) {
- cache.publicHomes.putIfAbsent(home.owner.username, new ArrayList<>());
- cache.publicHomes.get(home.owner.username).add(home.meta.name);
- if (mapHook != null) {
- mapHook.updateHome(home);
- }
- } else {
- if (cache.publicHomes.containsKey(home.owner.username)) {
- cache.publicHomes.get(home.owner.username).remove(home.meta.name);
- }
- if (mapHook != null) {
- mapHook.removeHome(home);
- }
+ return eventDispatcher.dispatchHomeSaveEvent(home).thenApply(event -> {
+ if (event.isCancelled()) {
+ return false;
}
- cache.privateHomeLists.remove(home.owner.uuid);
- cache.publicHomeLists.clear();
- return true;
+
+ return database.setHome(home).thenApply(ignored -> {
+ if (isHomePublic) {
+ cache.publicHomes.putIfAbsent(home.owner.username, new ArrayList<>());
+ cache.publicHomes.get(home.owner.username).add(home.meta.name);
+ if (mapHook != null) {
+ mapHook.updateHome(home);
+ }
+ } else {
+ if (cache.publicHomes.containsKey(home.owner.username)) {
+ cache.publicHomes.get(home.owner.username).remove(home.meta.name);
+ }
+ if (mapHook != null) {
+ mapHook.removeHome(home);
+ }
+ }
+ cache.privateHomeLists.remove(home.owner.uuid);
+ cache.publicHomeLists.clear();
+ return true;
+ }).join();
});
}
@@ -265,13 +298,18 @@ public class SavedPositionManager {
validateMeta(warpMeta).map(resultType -> new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
if (optionalWarp.isEmpty()) {
final Warp warp = new Warp(position, warpMeta);
- cache.warps.add(warp.meta.name);
- cache.warpLists.clear();
- if (mapHook != null) {
- mapHook.updateWarp(warp);
- }
- return database.setWarp(warp).thenApply(ignored ->
- new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
+ return eventDispatcher.dispatchWarpSaveEvent(warp).thenApply(event -> {
+ if (event.isCancelled()) {
+ return new SaveResult(SaveResult.ResultType.FAILED_EVENT_CANCELLED, Optional.empty());
+ }
+ cache.warps.add(warp.meta.name);
+ cache.warpLists.clear();
+ if (mapHook != null) {
+ mapHook.updateWarp(warp);
+ }
+ return database.setWarp(warp).thenApply(ignored ->
+ new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
+ }).join();
}
return new SaveResult(SaveResult.ResultType.FAILED_DUPLICATE, Optional.empty());
}));
@@ -287,14 +325,19 @@ public class SavedPositionManager {
public CompletableFuture deleteWarp(@NotNull String warpName) {
return database.getWarp(warpName).thenApply(optionalWarp -> {
if (optionalWarp.isPresent()) {
- final Warp warp = optionalWarp.get();
- return database.deleteWarp(warp.uuid).thenApply(ignored -> {
- cache.warps.remove(warp.meta.name);
- cache.warpLists.clear();
- if (mapHook != null) {
- mapHook.removeWarp(warp);
+ return eventDispatcher.dispatchWarpDeleteEvent(optionalWarp.get()).thenApply(event -> {
+ if (event.isCancelled()) {
+ return false;
}
- return true;
+ final Warp warp = event.getWarp();
+ return database.deleteWarp(warp.uuid).thenApply(ignored -> {
+ cache.warps.remove(warp.meta.name);
+ cache.warpLists.clear();
+ if (mapHook != null) {
+ mapHook.removeWarp(warp);
+ }
+ return true;
+ }).join();
}).join();
}
return false;
@@ -334,16 +377,21 @@ public class SavedPositionManager {
return validateMeta(newWarpMeta).map(resultType ->
new SaveResult(resultType, Optional.empty())).orElseGet(() -> {
warp.meta = newWarpMeta;
- if (!existingWarpName.equals(newWarpMeta.name)) {
- cache.warps.remove(existingWarpName);
- cache.warps.add(newWarpMeta.name);
- }
- cache.warpLists.clear();
- if (mapHook != null) {
- mapHook.updateWarp(warp);
- }
- return database.setWarp(warp).thenApply(value ->
- new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
+ return eventDispatcher.dispatchWarpSaveEvent(warp).thenApply(event -> {
+ if (event.isCancelled()) {
+ return new SaveResult(SaveResult.ResultType.FAILED_EVENT_CANCELLED, Optional.empty());
+ }
+ if (!existingWarpName.equals(newWarpMeta.name)) {
+ cache.warps.remove(existingWarpName);
+ cache.warps.add(newWarpMeta.name);
+ }
+ cache.warpLists.clear();
+ if (mapHook != null) {
+ mapHook.updateWarp(warp);
+ }
+ return database.setWarp(warp).thenApply(value ->
+ new SaveResult(SaveResult.ResultType.SUCCESS, Optional.of(warp))).join();
+ }).join();
});
});
}
@@ -381,10 +429,15 @@ public class SavedPositionManager {
warp.yaw = newPosition.yaw;
warp.world = newPosition.world;
warp.server = newPosition.server;
- if (mapHook != null) {
- mapHook.updateWarp(warp);
- }
- return database.setWarp(warp).thenApply(ignored -> true);
+ return eventDispatcher.dispatchWarpSaveEvent(warp).thenApply(event -> {
+ if (event.isCancelled()) {
+ return false;
+ }
+ if (mapHook != null) {
+ mapHook.updateWarp(warp);
+ }
+ return database.setWarp(warp).thenApply(ignored -> true).join();
+ });
}
/**
@@ -464,6 +517,11 @@ public class SavedPositionManager {
*/
FAILED_DESCRIPTION_CHARACTERS(false),
+ /**
+ * The position was not set or updated; the save event was cancelled by a plugin
+ */
+ FAILED_EVENT_CANCELLED(false),
+
/**
* The position was not set or updated; an exception occurred
*/
diff --git a/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java b/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java
index f2536ce2..67c9fca1 100644
--- a/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java
+++ b/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java
@@ -9,7 +9,10 @@ import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
-public class NormalDistributionEngine extends RtpEngine {
+/**
+ * A random teleport engine that uses a Gaussian normal distribution to generate random positions.
+ */
+public class NormalDistributionEngine extends RandomTeleportEngine {
private final HuskHomes plugin;
private final float mean;
diff --git a/common/src/main/java/net/william278/huskhomes/random/RtpEngine.java b/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java
similarity index 94%
rename from common/src/main/java/net/william278/huskhomes/random/RtpEngine.java
rename to common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java
index 0a46cf6e..8cb8fb21 100644
--- a/common/src/main/java/net/william278/huskhomes/random/RtpEngine.java
+++ b/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java
@@ -15,7 +15,7 @@ import java.util.stream.Collectors;
/**
* An engine for random teleportation, that generates and caches random points
*/
-public abstract class RtpEngine {
+public abstract class RandomTeleportEngine {
@NotNull
protected final String name;
@@ -27,7 +27,7 @@ public abstract class RtpEngine {
@NotNull
private final Map> cachedRandomLocations;
- protected RtpEngine(@NotNull HuskHomes implementor, @NotNull String name) {
+ protected RandomTeleportEngine(@NotNull HuskHomes implementor, @NotNull String name) {
this.name = name;
this.radius = implementor.getSettings().rtpRadius;
this.spawnRadius = implementor.getSettings().rtpSpawnRadius;
@@ -40,7 +40,7 @@ public abstract class RtpEngine {
}
//todo better API for this?
- protected RtpEngine(@NotNull String name, int radius, int spawnRadius, int cacheSize, @NotNull List worlds) {
+ protected RandomTeleportEngine(@NotNull String name, int radius, int spawnRadius, int cacheSize, @NotNull List worlds) {
this.name = name;
this.radius = radius;
this.spawnRadius = spawnRadius;
diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java
index fba0951f..e83c95f8 100644
--- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java
+++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java
@@ -349,6 +349,9 @@ public class TeleportManager {
@NotNull TeleportType teleportType) {
final Teleport teleport = new Teleport(onlineUser, position, teleportType);
+ // Call the teleport event
+ plugin.getEventDispatcher().dispatchTeleportEvent(teleport);
+
// Update the player's last position
if (!plugin.getSettings().backCommandSaveOnTeleportEvent) {
plugin.getDatabase().setLastPosition(onlineUser, onlineUser.getPosition());
diff --git a/images/command-list.png b/images/command-list.png
new file mode 100644
index 00000000..114cf7c5
Binary files /dev/null and b/images/command-list.png differ
diff --git a/images/edit-home-menu.png b/images/edit-home-menu.png
index 3dd7ca5e..c3e064a5 100644
Binary files a/images/edit-home-menu.png and b/images/edit-home-menu.png differ