diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 806537ed..119584cf 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -13,6 +13,8 @@ import net.william278.huskhomes.config.Spawn; import net.william278.huskhomes.database.Database; import net.william278.huskhomes.database.MySqlDatabase; import net.william278.huskhomes.database.SqLiteDatabase; +import net.william278.huskhomes.event.BukkitEventDispatcher; +import net.william278.huskhomes.event.EventDispatcher; import net.william278.huskhomes.hook.*; import net.william278.huskhomes.listener.BukkitEventListener; import net.william278.huskhomes.listener.EventListener; @@ -26,7 +28,7 @@ import net.william278.huskhomes.position.SavedPositionManager; import net.william278.huskhomes.position.Server; import net.william278.huskhomes.position.World; import net.william278.huskhomes.random.NormalDistributionEngine; -import net.william278.huskhomes.random.RtpEngine; +import net.william278.huskhomes.random.RandomTeleportEngine; import net.william278.huskhomes.request.RequestManager; import net.william278.huskhomes.teleport.TeleportManager; import net.william278.huskhomes.util.*; @@ -64,8 +66,9 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { private RequestManager requestManager; private SavedPositionManager savedPositionManager; private EventListener eventListener; - private RtpEngine rtpEngine; + private RandomTeleportEngine randomTeleportEngine; private Spawn serverSpawn; + private EventDispatcher eventDispatcher; private Set pluginHooks; private List registeredCommands; @@ -143,13 +146,15 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { // Prepare the request manager this.requestManager = new RequestManager(this); + // Prepare the event dispatcher + this.eventDispatcher = new BukkitEventDispatcher(this); + // Prepare the home and warp position manager - this.savedPositionManager = new SavedPositionManager(database, cache, + this.savedPositionManager = new SavedPositionManager(database, cache, eventDispatcher, settings.allowUnicodeNames, settings.allowUnicodeDescriptions); // Initialize the RTP engine with the default normal distribution engine - this.rtpEngine = new NormalDistributionEngine(this); - rtpEngine.initialize(); + setRandomTeleportEngine(new NormalDistributionEngine(this)); // Register plugin hooks (Economy, Maps, Plan) this.pluginHooks = new HashSet<>(); @@ -328,8 +333,19 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { } @Override - public @NotNull RtpEngine getRtpEngine() { - return rtpEngine; + public @NotNull RandomTeleportEngine getRandomTeleportEngine() { + return randomTeleportEngine; + } + + @Override + public void setRandomTeleportEngine(@NotNull RandomTeleportEngine randomTeleportEngine) { + this.randomTeleportEngine = randomTeleportEngine; + this.randomTeleportEngine.initialize(); + } + + @Override + public @NotNull EventDispatcher getEventDispatcher() { + return eventDispatcher; } @Override diff --git a/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java b/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java new file mode 100644 index 00000000..c4762c36 --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java @@ -0,0 +1,119 @@ +package net.william278.huskhomes.api; + +import net.william278.huskhomes.BukkitHuskHomes; +import net.william278.huskhomes.player.BukkitPlayer; +import net.william278.huskhomes.player.OnlineUser; +import net.william278.huskhomes.player.User; +import net.william278.huskhomes.position.Location; +import net.william278.huskhomes.position.Position; +import net.william278.huskhomes.position.Server; +import net.william278.huskhomes.util.BukkitAdapter; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Objects; + +/** + * The HuskHomes API implementation for the Bukkit platform, providing methods to access player data, homes, warps + * & process teleports + *

+ * Retrieve an instance of the API class via {@link #getInstance()}. + */ +@SuppressWarnings("unused") +public class HuskHomesAPI extends BaseHuskHomesAPI { + + /** + * (Internal use only) - Instance of the API class + */ + private static final HuskHomesAPI INSTANCE = new HuskHomesAPI(); + + /** + * (Internal use only) - Constructor, instantiating the API + */ + private HuskHomesAPI() { + super(BukkitHuskHomes.getInstance()); + } + + /** + * Entrypoint to the HuskHomes API - returns an instance of the API + * + * @return instance of the HuskHomes API + */ + public static HuskHomesAPI getInstance() { + return INSTANCE; + } + + /** + * Returns an {@link OnlineUser} instance for the given bukkit {@link Player}. + * + * @param player the bukkit player to get the {@link User} instance for + * @return the {@link OnlineUser} instance for the given bukkit {@link Player} + * @since 3.0 + */ + @NotNull + public OnlineUser adaptUser(@NotNull Player player) { + return BukkitPlayer.adapt(player); + } + + /** + * Returns the bukkit {@link Player} being represented by the given {@link OnlineUser}. + * + * @param user {@link OnlineUser} to get the bukkit player from + * @return the bukkit {@link Player} being represented by the given {@link OnlineUser} + * @since 3.0 + */ + @NotNull + public Player getPlayer(@NotNull OnlineUser user) { + return ((BukkitPlayer) user).getPlayer(); + } + + /** + * Returns the bukkit {@link Location} being represented by the given {@link Position}. + * + * @param position the {@link Position} to get the bukkit location from + * @return the bukkit {@link Location} being represented by the given {@link Position} + * @since 3.0 + */ + @Nullable + public org.bukkit.Location getLocation(@NotNull Position position) { + return BukkitAdapter.adaptLocation(position).orElse(null); + } + + /** + * Returns a {@link Location} instance for the given bukkit {@link Location}. + * + * @param location the bukkit location to get the {@link Location} instance for + * @return the {@link Location} instance for the given bukkit {@link Location} + * @since 3.0 + */ + @Nullable + public Location adaptLocation(@NotNull org.bukkit.Location location) { + return BukkitAdapter.adaptLocation(location).orElse(null); + } + + /** + * Returns a {@link Position} instance for the given bukkit {@link Location} on the given {@link Server}. + * + * @param location the bukkit location to get the {@link Position} instance for + * @param server the {@link Server} the position is on + * @see Position#server + * @since 3.0 + */ + @Nullable + public Position adaptPosition(@NotNull org.bukkit.Location location, @NotNull Server server) { + return new Position(Objects.requireNonNull(adaptLocation(location)), server); + } + + /** + * Get this {@link Server}. + * + * @param requester a player; needed to send a plugin message requesting the server if it has not been cached + * @return the {@link Server} + */ + @NotNull + public Server getServer(@NotNull OnlineUser requester) { + return plugin.getServer(requester); + } + +} diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java new file mode 100644 index 00000000..19103f19 --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java @@ -0,0 +1,75 @@ +package net.william278.huskhomes.event; + +import net.william278.huskhomes.BukkitHuskHomes; +import net.william278.huskhomes.position.Home; +import net.william278.huskhomes.position.Warp; +import net.william278.huskhomes.teleport.Teleport; +import org.bukkit.Bukkit; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.CompletableFuture; + +public class BukkitEventDispatcher implements EventDispatcher { + + private final BukkitHuskHomes plugin; + + public BukkitEventDispatcher(@NotNull BukkitHuskHomes implementor) { + this.plugin = implementor; + } + + @Override + public CompletableFuture dispatchTeleportEvent(@NotNull Teleport teleport) { + final CompletableFuture dispatchFuture = new CompletableFuture<>(); + Bukkit.getScheduler().runTask(plugin, () -> { + final TeleportEvent event = new TeleportEvent(teleport); + Bukkit.getPluginManager().callEvent(event); + dispatchFuture.complete(event); + }); + return dispatchFuture; + } + + @Override + public CompletableFuture dispatchHomeSaveEvent(@NotNull Home home) { + final CompletableFuture dispatchFuture = new CompletableFuture<>(); + Bukkit.getScheduler().runTask(plugin, () -> { + final HomeSaveEvent event = new HomeSaveEvent(home); + Bukkit.getPluginManager().callEvent(event); + dispatchFuture.complete(event); + }); + return dispatchFuture; + } + + @Override + public CompletableFuture dispatchHomeDeleteEvent(@NotNull Home home) { + final CompletableFuture dispatchFuture = new CompletableFuture<>(); + Bukkit.getScheduler().runTask(plugin, () -> { + final HomeDeleteEvent event = new HomeDeleteEvent(home); + Bukkit.getPluginManager().callEvent(event); + dispatchFuture.complete(event); + }); + return dispatchFuture; + } + + @Override + public CompletableFuture dispatchWarpSaveEvent(@NotNull Warp warp) { + final CompletableFuture dispatchFuture = new CompletableFuture<>(); + Bukkit.getScheduler().runTask(plugin, () -> { + final WarpSaveEvent event = new WarpSaveEvent(warp); + Bukkit.getPluginManager().callEvent(event); + dispatchFuture.complete(event); + }); + return dispatchFuture; + } + + @Override + public CompletableFuture dispatchWarpDeleteEvent(@NotNull Warp warp) { + final CompletableFuture dispatchFuture = new CompletableFuture<>(); + Bukkit.getScheduler().runTask(plugin, () -> { + final WarpDeleteEvent event = new WarpDeleteEvent(warp); + Bukkit.getPluginManager().callEvent(event); + dispatchFuture.complete(event); + }); + return dispatchFuture; + } + +} diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/HomeDeleteEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/HomeDeleteEvent.java new file mode 100644 index 00000000..3f7f3b7a --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/HomeDeleteEvent.java @@ -0,0 +1,45 @@ +package net.william278.huskhomes.event; + +import net.william278.huskhomes.position.Home; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class HomeDeleteEvent extends Event implements IHomeDeleteEvent, Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + @NotNull + private final Home home; + + private boolean cancelled; + + public HomeDeleteEvent(@NotNull Home home) { + this.home = home; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + 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 Home getHome() { + return home; + } +} diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/HomeSaveEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/HomeSaveEvent.java new file mode 100644 index 00000000..08991f41 --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/HomeSaveEvent.java @@ -0,0 +1,45 @@ +package net.william278.huskhomes.event; + +import net.william278.huskhomes.position.Home; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class HomeSaveEvent extends Event implements IHomeSaveEvent, Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + @NotNull + private final Home home; + + private boolean cancelled; + + public HomeSaveEvent(@NotNull Home home) { + this.home = home; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + 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 Home getHome() { + return home; + } +} diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/TeleportEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/TeleportEvent.java new file mode 100644 index 00000000..e24a57ea --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/TeleportEvent.java @@ -0,0 +1,33 @@ +package net.william278.huskhomes.event; + +import net.william278.huskhomes.teleport.Teleport; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class TeleportEvent extends Event implements ITeleportEvent { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + @NotNull + private final Teleport teleport; + + public TeleportEvent(@NotNull Teleport teleport) { + this.teleport = teleport; + } + + @Override + public @NotNull Teleport getTeleport() { + return teleport; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } + +} diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/WarpDeleteEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/WarpDeleteEvent.java new file mode 100644 index 00000000..682b7654 --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/WarpDeleteEvent.java @@ -0,0 +1,45 @@ +package net.william278.huskhomes.event; + +import net.william278.huskhomes.position.Warp; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class WarpDeleteEvent extends Event implements IWarpDeleteEvent, Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + @NotNull + private final Warp warp; + + private boolean cancelled; + + public WarpDeleteEvent(@NotNull Warp warp) { + this.warp = warp; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + 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 Warp getWarp() { + return warp; + } +} diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/WarpSaveEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/WarpSaveEvent.java new file mode 100644 index 00000000..286ce420 --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/WarpSaveEvent.java @@ -0,0 +1,45 @@ +package net.william278.huskhomes.event; + +import net.william278.huskhomes.position.Warp; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class WarpSaveEvent extends Event implements IWarpSaveEvent, Cancellable { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + @NotNull + private final Warp warp; + + private boolean cancelled; + + public WarpSaveEvent(@NotNull Warp warp) { + this.warp = warp; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + 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 Warp getWarp() { + return warp; + } +} diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index 2971e026..af051e22 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -7,6 +7,7 @@ import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.config.Spawn; import net.william278.huskhomes.database.Database; +import net.william278.huskhomes.event.EventDispatcher; import net.william278.huskhomes.hook.EconomyHook; import net.william278.huskhomes.hook.MapHook; import net.william278.huskhomes.hook.PluginHook; @@ -16,7 +17,7 @@ import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.SavedPositionManager; import net.william278.huskhomes.position.Server; import net.william278.huskhomes.position.World; -import net.william278.huskhomes.random.RtpEngine; +import net.william278.huskhomes.random.RandomTeleportEngine; import net.william278.huskhomes.request.RequestManager; import net.william278.huskhomes.teleport.TeleportManager; import net.william278.huskhomes.util.Logger; @@ -132,12 +133,27 @@ public interface HuskHomes { NetworkMessenger getNetworkMessenger(); /** - * The {@link RtpEngine} that manages random teleports + * The {@link RandomTeleportEngine} that manages random teleports * - * @return the {@link RtpEngine} implementation + * @return the {@link RandomTeleportEngine} implementation */ @NotNull - RtpEngine getRtpEngine(); + RandomTeleportEngine getRandomTeleportEngine(); + + /** + * Set the {@link RandomTeleportEngine} to use and initialize it, calling {@link RandomTeleportEngine#initialize()} + * + * @param randomTeleportEngine the {@link RandomTeleportEngine} to use + */ + void setRandomTeleportEngine(@NotNull RandomTeleportEngine randomTeleportEngine); + + /** + * The {@link EventDispatcher} that dispatches API events + * + * @return the {@link EventDispatcher} implementation + */ + @NotNull + EventDispatcher getEventDispatcher(); /** * The {@link Spawn} location of this server diff --git a/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java b/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java new file mode 100644 index 00000000..2998d34b --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java @@ -0,0 +1,234 @@ +package net.william278.huskhomes.api; + +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.player.OnlineUser; +import net.william278.huskhomes.player.User; +import net.william278.huskhomes.player.UserData; +import net.william278.huskhomes.position.Home; +import net.william278.huskhomes.position.Position; +import net.william278.huskhomes.position.Warp; +import net.william278.huskhomes.teleport.TeleportResult; +import net.william278.huskhomes.random.RandomTeleportEngine; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; + +/** + * The base implementation of the HuskHomes API, containing cross-platform API calls. + *

+ * This class should not be used directly, but rather through platform-specific extending API classes. + */ +@SuppressWarnings("unused") +public abstract class BaseHuskHomesAPI { + + /** + * (Internal use only) - Instance of the implementing plugin. + */ + protected final HuskHomes plugin; + + /** + * (Internal use only) - Constructor, instantiating the base API class + */ + protected BaseHuskHomesAPI(@NotNull HuskHomes plugin) { + this.plugin = plugin; + } + + /** + * Returns saved {@link UserData} for the given player's account {@link UUID}, if they exist. + * + * @param uuid The {@link UUID} of the user to get data for. + * @return The {@link UserData} of the user. + * @since 3.0 + */ + public final CompletableFuture> getUserData(@NotNull UUID uuid) { + return plugin.getDatabase().getUserData(uuid); + } + + /** + * Returns saved {@link UserData} for the given player's username (case-insensitive), if they exist. + * + * @param username The username of the user to get data for. + * @return The {@link UserData} of the user. + * @since 3.0 + */ + public final CompletableFuture> getUserData(@NotNull String username) { + return plugin.getDatabase().getUserDataByName(username); + } + + /** + * Returns the last position, as used in the {@code /back} command, for this user + * + * @param user The {@link User} to get the last position for + * @return The user's last {@link Position}, if there is one + * @since 3.0 + */ + public CompletableFuture> getUserLastPosition(@NotNull User user) { + return plugin.getDatabase().getLastPosition(user); + } + + /** + * Returns where the user last disconnected from a server + * + * @param user The {@link User} to get the last disconnect position for + * @return The user's offline {@link Position}, if there is one + * @since 3.0 + */ + public CompletableFuture> 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