diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportationException.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportationException.java
index 8ebe75ae..dcf3cab3 100644
--- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportationException.java
+++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportationException.java
@@ -25,23 +25,15 @@ import org.jetbrains.annotations.NotNull;
public class TeleportationException extends IllegalStateException {
- public TeleportationException(@NotNull Type error) {
- super("Error during teleport operation: " + error.name());
- }
+ private final Type type;
- public enum Type {
- TELEPORTER_NOT_FOUND,
- TARGET_NOT_FOUND,
- ALREADY_WARMING_UP,
- ECONOMY_ACTION_FAILED,
- WARMUP_ALREADY_MOVING,
- WORLD_NOT_FOUND,
- ILLEGAL_TARGET_COORDINATES,
- CANNOT_TELEPORT_TO_SELF
+ public TeleportationException(@NotNull Type type) {
+ super("Error executing teleport: " + type.name());
+ this.type = type;
}
public void displayMessage(@NotNull CommandUser user, @NotNull HuskHomes plugin, @NotNull String... args) {
- switch (Type.valueOf(getMessage().split(": ")[1])) {
+ switch (type) {
case TELEPORTER_NOT_FOUND, TARGET_NOT_FOUND -> plugin.getLocales()
.getLocale("error_player_not_found", args)
.ifPresent(user::sendMessage);
@@ -62,4 +54,25 @@ public class TeleportationException extends IllegalStateException {
.ifPresent(user::sendMessage);
}
}
+
+ @NotNull
+ @SuppressWarnings("unused")
+ public Type getType() {
+ return type;
+ }
+
+ /**
+ * The types of teleportation errors
+ */
+ public enum Type {
+ TELEPORTER_NOT_FOUND,
+ TARGET_NOT_FOUND,
+ ALREADY_WARMING_UP,
+ ECONOMY_ACTION_FAILED,
+ WARMUP_ALREADY_MOVING,
+ WORLD_NOT_FOUND,
+ ILLEGAL_TARGET_COORDINATES,
+ CANNOT_TELEPORT_TO_SELF
+ }
+
}
diff --git a/common/src/main/java/net/william278/huskhomes/util/ValidationException.java b/common/src/main/java/net/william278/huskhomes/util/ValidationException.java
index 9cbf422c..b777470f 100644
--- a/common/src/main/java/net/william278/huskhomes/util/ValidationException.java
+++ b/common/src/main/java/net/william278/huskhomes/util/ValidationException.java
@@ -26,15 +26,15 @@ import org.jetbrains.annotations.NotNull;
public class ValidationException extends IllegalArgumentException {
- private final Type error;
+ private final Type type;
- public ValidationException(@NotNull ValidationException.Type error) {
- super("Error validating position: " + error.name());
- this.error = error;
+ public ValidationException(@NotNull ValidationException.Type type) {
+ super("Error validating SavedPosition: " + type.name());
+ this.type = type;
}
public void dispatchHomeError(@NotNull CommandUser viewer, boolean other, @NotNull HuskHomes plugin, @NotNull String... args) {
- switch (error) {
+ switch (type) {
case NOT_FOUND -> plugin.getLocales()
.getLocale(other ? "error_home_invalid_other" : "error_home_invalid", args)
.ifPresent(viewer::sendMessage);
@@ -44,6 +44,9 @@ public class ValidationException extends IllegalArgumentException {
case NAME_INVALID -> plugin.getLocales()
.getLocale("error_home_name_characters", args)
.ifPresent(viewer::sendMessage);
+ case DESCRIPTION_INVALID -> plugin.getLocales()
+ .getLocale("error_home_description_characters", args)
+ .ifPresent(viewer::sendMessage);
case NOT_ENOUGH_HOME_SLOTS, REACHED_MAX_HOMES -> plugin.getLocales()
.getLocale("error_set_home_maximum_homes", Integer.toString(plugin.getManager().homes()
.getMaxHomes(viewer instanceof OnlineUser user ? user : null)))
@@ -52,14 +55,11 @@ public class ValidationException extends IllegalArgumentException {
.getLocale("error_edit_home_maximum_public_homes", Integer.toString(plugin.getManager().homes()
.getMaxPublicHomes(viewer instanceof OnlineUser user ? user : null)))
.ifPresent(viewer::sendMessage);
- case DESCRIPTION_INVALID -> plugin.getLocales()
- .getLocale("error_home_description_characters", args)
- .ifPresent(viewer::sendMessage);
}
}
public void dispatchWarpError(@NotNull CommandUser viewer, @NotNull HuskHomes plugin, @NotNull String... args) {
- switch (error) {
+ switch (type) {
case NOT_FOUND -> plugin.getLocales()
.getLocale("error_warp_invalid", args)
.ifPresent(viewer::sendMessage);
@@ -75,6 +75,15 @@ public class ValidationException extends IllegalArgumentException {
}
}
+ @NotNull
+ @SuppressWarnings("unused")
+ public Type getType() {
+ return type;
+ }
+
+ /**
+ * The type of validation error
+ */
public enum Type {
NOT_FOUND,
NAME_TAKEN,
diff --git a/docs/API-Events.md b/docs/API-Events.md
index 82b718a8..0ce4b8d2 100644
--- a/docs/API-Events.md
+++ b/docs/API-Events.md
@@ -21,4 +21,19 @@ HuskHomes provides a number of API events your plugin can listen to when certain
| `TeleportBackEvent` | 4.1 | ✅ | Called when a player teleports to their last position (`/back`)‡ |
† If the player uses `/delhome all` or `/delwarp all` to delete all their homes or all the warps, a single `DeleteAllHomesEvent` or `DeleteAllWarpsEvent` is fired instead.
-‡ Called on the server the player *is teleported from*; not necessarily where the executor of the teleport is.
\ No newline at end of file
+‡ Called on the server the player *is teleported from*; not necessarily where the executor of the teleport is.
+
+## Events on Sponge & Fabric
+Sponge, which has a similar Event api as Bukkit, has equivalent events for all of the above, prefixed with `Sponge` (so HomeCreateEvent on Bukkit is SpongeHomeCreateEvent on Sponge).
+
+Fabric uses callbacks for handling events. HuskHomes provides callback equivalents for all the above events. For instance, to handle what to do when someone creates a home, you can register a callback to handle what to do as follows:
+
+HuskHomes and Fabric callbacks
+
+```java
+HomeCreateCallback.EVENT.register((player, home) -> {
+ // Do something with the player and home
+ return ActionResult.SUCCESS; // Return an appropriate ActionResult
+});.
+```
+
\ No newline at end of file
diff --git a/docs/API-Examples.md b/docs/API-Examples.md
index b57a9e2f..6bdf3ca8 100644
--- a/docs/API-Examples.md
+++ b/docs/API-Examples.md
@@ -1,7 +1,9 @@
+> **Please Note:** The HuskHomes API itself is only available on Bukkit, but Fabric and Sponge do support [API events](api-events).
+
The HuskHomes API provides methods and classes for:
* getting, creating and updating `Home`s and `Warp`s
* getting information about `SavedUser` data
-* building and executing `Teleport`s, including cross-server teleports
+* building and executing (timed) `Teleport`s, including cross-server teleports
* provide custom `RandomTeleportEngine`s for extending the `/rtp` command's functionality.
In addition, [[API Events]] are available for handling when certain actions take place.
@@ -71,16 +73,18 @@ public class HuskHomesAPIHook {
### The CompletableFuture
A number of methods from the HuskHomesAPI return asynchronously-executing [CompletableFutures](https://www.baeldung.com/java-completablefuture) to ensure the database queries they rely on do not block the main server thread. When a future has finished executing, you can accept the result through a `#thenAccept()`.
-You should never call `#join()` on futures returned from the HuskHomesAPI as futures are processed on server asynchronous tasks, which could lead to thread deadlock and crash your server if you attempt to lock the main thread to process them.
+> **Warning:** You should never call `#join()` on futures returned from the HuskHomesAPI as futures are processed on server asynchronous tasks, which could lead to thread deadlock and crash your server if you attempt to lock the main thread to process them.
## Getting a player's homes
-To get a list of a player's homes, you can call
+To get a list of a player's homes, you can call `#getUserHomes(user)` on the API instance. This method requires a `User` object, which can be constructed from a UUID and username, or adapted from an online player using `#adaptUser(player)`. The method returns a `CompletableFuture>`, which can be accepted with `#thenAccept()`.
+
+Note that all the documentation for `Home`s also applies to `Warps` (e.g. `HuskHomesAPI#getWarps` to get all the warps). Since warps are globally owned, these methods do not require an owning `User`.
Printing a home list to console
```java
-public class HuskSyncAPIHook {
+public class HuskHomesAPIHook {
private final HuskHomesAPI huskHomesAPI;
@@ -89,6 +93,7 @@ public class HuskSyncAPIHook {
// Use this to adapt an online player to an OnlineUser, which extends User (accepted by getUserHomes).
// To get the homes of an offline user, use: User.of(uuid, username);
OnlineUser user = huskHomesAPI.adaptUser(Bukkit.getPlayer(uuid));
+
// A lot of HuskHomes' API methods return as futures which execute asynchronously.
huskHomesAPI.getUserHomes(user).thenAccept(homeList -> { // Use #thenAccept(data) to run after the future has executed with data
for (Home home : homeList) {
@@ -96,31 +101,70 @@ public class HuskSyncAPIHook {
System.out.println(home.meta.name); // It's best to use your plugin logger, but this is just an example.
}
});
+
+ // You can also get a specific home by name using #getUserHome(user, name)
+ huskHomesAPI.getUserHome(user, "example").thenAccept(optionalHome -> {
+ // #getUserHome returns an Optional wrapper, so we need to run #ifPresent() first and call #get() to retrieve it if it exists
+ if (optionalHome.isPresent()) {
+ System.out.println("Found " + user.getUsername() + "'s home: " optionalHome.get().getName());
+ } else {
+ System.out.println("Home not found");
+ }
+ });
}
}
```
+### Creating a home
+To create a home, you can call `#createHome(owner, name, position)` on the API instance. This method requires a user who owns the home, a valid name, and a Position.
+
+
+Creating a home
+
+```java
+public class HuskHomesAPIHook {
+
+ private final HuskHomesAPI huskHomesAPI;
+
+ // This method creates a home with the name "example" at the player's current location
+ public void createHome(Player owner) {
+ // Use this to adapt an online player to an OnlineUser, which extends User (accepted by createHome).
+ OnlineUser onlineUser = huskHomesAPI.adaptUser(player);
+
+ // We can get an OnlineUser's Position with #getPosition, which we can pass here to createHome
+ try {
+ huskHomesAPI.createHome(onlineUser, "example", onlineUser.getPosition());
+ } catch (ValidationException e) {
+ // Homes will be validated, and if validation fails a ValidationException will be thrown.
+ // This can happen if the user has too many homes, or if its metadata is invalid (name, description, etc)
+ // You should catch ValidationExceptions, determine what caused it (#getType) and handle it appropriately.
+ owner.sendMessage(ChatColor.RED + "Failed to create example home: " + e.getType());
+ }
+ }
+}
+```
+
+
## Creating Teleports
-The API provides a method for getting a `TeleportBuilder`, which can be used to build a `Teleport` (with toTeleport) or `TimedTeleport` (with toTimedTeleport; a teleport that requires the user to stand still for a period of time first). Teleports can be cross-server.
+The API provides a method for getting a `TeleportBuilder`, which can be used to build a `Teleport` (with `#toTeleport`) or `TimedTeleport` (with toTimedTeleport; a teleport that requires the user to stand still for a period of time first). Teleports can be cross-server.
Building a teleport
```java
-public class HuskSyncAPIHook {
+public class HuskHomesAPIHook {
private final HuskHomesAPI huskHomesAPI;
// This teleports a player to 128, 64, 128 on the server "server"
public void teleportPlayer(Player player) {
- // Use this to adapt an online player to an OnlineUser, which extends User (accepted by getUserHomes).
OnlineUser onlineUser = huskHomesAPI.adaptUser(player);
- // The TeleportBuilder accepts a class that (extends/is a) Position. This can be a Home, Warp or constructed Position.
- // --> Note that the World object needs the name and UID of the world.
- // --> The UID will be used if the world can't be found by name. You can just pass it a random UUID if you don't have it.
+ // The TeleportBuilder accepts a class that extends Target. This can be a Username or a Position (or a Home/Warp, which extends Position)
+ // * Note that the World object needs the name and UID of the world.
+ // * The UID will be used if the world can't be found by name. You can just pass it a random UUID if you don't have it.
Position position = Position.at(
128, 64, 128,
World.from("world", UUID.randomUUID()), "server"
@@ -134,7 +178,38 @@ public class HuskSyncAPIHook {
.toTimedTeleport()
.execute(); // #execute() can throw a TeleportationException
} catch(TeleportationException e) {
- e.printStackTrace();
+ e.printStackTrace(); // This exception will contain the reason why the teleport failed, so you can handle it gracefully.
+ }
+ }
+
+}
+```
+
+
+### Timed Teleports
+Timed teleports are teleports that require the user to stand still for a period of time before they are executed, so that players don't instantly teleport out of combat or dangerous situations. They can be created by calling `#toTimedTeleport()` on a `TeleportBuilder`. The time the player must stand still for is determined through the warmup time value set in the plugin config.
+
+
+Building a timed teleport
+
+```java
+public class HuskHomesAPIHook {
+
+ private final HuskHomesAPI huskHomesAPI;
+
+ // This performs a timed teleport to tp a player to another online player with the username "William278"
+ public void teleportPlayer(Player player) {
+ OnlineUser onlineUser = huskHomesAPI.adaptUser(player);
+ Target targetUsername = Target.username("William278"); // Get a target by a username, who can be online on this server/a server on the network (cross-server teleport).
+
+ try {
+ huskHomesAPI.teleportBuilder()
+ .teleporter(onlineUser)
+ .target(targetUsername)
+ .toTimedTeleport()
+ .execute(); // A timed teleport will throw a TeleportationException if the player moves/takes damage during the warmup, or if the target is not found.
+ } catch(TeleportationException e) {
+ e.printStackTrace(); // Note that the TimedTeleport will catch internal exceptions when executing the resultant Teleport (e.g. if the teleport is to an illegal position).
}
}
diff --git a/docs/API.md b/docs/API.md
index 355bfa8a..f37c5838 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -1,9 +1,19 @@
-
-
The HuskHomes API provides methods for getting, editing and updating user homes and warps, as well as functionality for building and executing (cross-server) teleports.
-The API is distributed via [JitPack](https://jitpack.io/#net.william278/HuskHomes2). Please note that HuskHomes API v1, v2 and v3 are not compatible.
-(Some) javadocs are also available to view on Jitpack [here](https://javadoc.jitpack.io/net/william278/HuskHomes2/latest/javadoc/).
+The API is distributed via [JitPack](https://jitpack.io/#net.william278/HuskHomes2), where you can also [view the JavaDocs](https://javadoc.jitpack.io/net/william278/HuskHomes2/latest/javadoc/).
+
+## Compatibility
+
+
+The HuskHomes API shares version numbering with the plugin itself for consistency and convenience. Please note minor and patch plugin releases may make API additions and deprecations, but will not introduce breaking changes without notice.
+
+| API Version | HuskHomes Versions | Supported |
+|:-----------:|:--------------------:|:---------:|
+| v4.x | _v4.0—Current_ | ✅ |
+| v3.x | _v3.0—v3.2.1_ | ❌ |
+| v2.x | _v2.0—v2.11.2_ | ❌ |
+| v1.x | _v1.5—v1.5.11_ | ❌ |
+
## Table of contents
1. Adding the API to your project
diff --git a/docs/Plan-Hook.md b/docs/Plan-Hook.md
index 1fd1a72b..50164898 100644
--- a/docs/Plan-Hook.md
+++ b/docs/Plan-Hook.md
@@ -3,7 +3,7 @@ HuskHomes supports displaying statistics about user sethomes on your [Player Ana

## Requirements
-- HuskSync v2.0+
+- HuskHomes v2.0+
- Plan v5.4.1690+
## Setup
diff --git a/gradle.properties b/gradle.properties
index 64675b3e..ad5f02ac 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -3,7 +3,7 @@ javaVersion=16
org.gradle.jvmargs='-Dfile.encoding=UTF-8'
org.gradle.daemon=true
-plugin_version=4.3
+plugin_version=4.3.1
plugin_archive=huskhomes
plugin_description=A powerful, intuitive and flexible teleportation suite