diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 2389d5ed..1ab99fe1 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -22,7 +22,7 @@ import net.william278.huskhomes.network.Broker; import net.william278.huskhomes.network.PluginMessageBroker; import net.william278.huskhomes.network.RedisBroker; import net.william278.huskhomes.position.Location; -import net.william278.huskhomes.position.Server; +import net.william278.huskhomes.config.Server; import net.william278.huskhomes.position.World; import net.william278.huskhomes.random.NormalDistributionEngine; import net.william278.huskhomes.random.RandomTeleportEngine; @@ -341,7 +341,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask final int y = snapshot.getHighestBlockYAt(x, z); final Material blockType = snapshot.getBlockType(chunkX, y, chunkZ); if (!isBlockUnsafe(blockType.getKey().toString())) { - return new Location((location.getX() + dX) + 0.5d, y + 1.25d, (location.getZ() + dZ) + 0.5d, location.getWorld()); + return Location.at((location.getX() + dX) + 0.5d, y + 1.25d, (location.getZ() + dZ) + 0.5d, location.getWorld()); } } } diff --git a/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java b/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java index 1537a7b7..95d6153a 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java +++ b/bukkit/src/main/java/net/william278/huskhomes/api/HuskHomesAPI.java @@ -3,7 +3,7 @@ package net.william278.huskhomes.api; import net.william278.huskhomes.BukkitHuskHomes; import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Position; -import net.william278.huskhomes.position.Server; +import net.william278.huskhomes.config.Server; import net.william278.huskhomes.user.BukkitUser; import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.user.User; @@ -26,7 +26,7 @@ public class HuskHomesAPI extends BaseHuskHomesAPI { /** * (Internal use only) - Instance of the API class */ - private static final HuskHomesAPI INSTANCE = new HuskHomesAPI(); + private static HuskHomesAPI instance; /** * (Internal use only) - Constructor, instantiating the API @@ -40,8 +40,9 @@ public class HuskHomesAPI extends BaseHuskHomesAPI { * * @return instance of the HuskHomes API */ + @NotNull public static HuskHomesAPI getInstance() { - return INSTANCE; + return instance == null ? instance = new HuskHomesAPI() : instance; } /** @@ -103,18 +104,19 @@ public class HuskHomesAPI extends BaseHuskHomesAPI { */ @NotNull public Position adaptPosition(@NotNull org.bukkit.Location location, @NotNull String server) { - return new Position(Objects.requireNonNull(adaptLocation(location)), server); + return Position.at(Objects.requireNonNull(adaptLocation(location)), server); } /** * Returns a {@link Position} instance for the given bukkit {@link Location} on the server the plugin is running on. + * * @param location the bukkit location to get the {@link Position} instance for * @return the {@link Position} instance for the given bukkit {@link Location} on the server the plugin is running on * @since 4.0 */ @NotNull public Position adaptPosition(@NotNull org.bukkit.Location location) { - return new Position(Objects.requireNonNull(adaptLocation(location)), getServer()); + return Position.at(Objects.requireNonNull(adaptLocation(location)), getServer()); } /** diff --git a/bukkit/src/main/java/net/william278/huskhomes/hook/RedisEconomyHook.java b/bukkit/src/main/java/net/william278/huskhomes/hook/RedisEconomyHook.java index 6be1be58..478c8c8b 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/hook/RedisEconomyHook.java +++ b/bukkit/src/main/java/net/william278/huskhomes/hook/RedisEconomyHook.java @@ -6,8 +6,8 @@ import org.jetbrains.annotations.NotNull; public class RedisEconomyHook extends VaultEconomyHook { - public RedisEconomyHook(@NotNull HuskHomes implementor) { - super(implementor); + public RedisEconomyHook(@NotNull HuskHomes plugin) { + super(plugin); } @Override diff --git a/bukkit/src/main/java/net/william278/huskhomes/hook/VaultEconomyHook.java b/bukkit/src/main/java/net/william278/huskhomes/hook/VaultEconomyHook.java index b2e4db04..eb2fa23c 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/hook/VaultEconomyHook.java +++ b/bukkit/src/main/java/net/william278/huskhomes/hook/VaultEconomyHook.java @@ -16,8 +16,8 @@ public class VaultEconomyHook extends EconomyHook { protected Economy economy; - public VaultEconomyHook(@NotNull HuskHomes implementor) { - super(implementor, "Vault (Economy)"); + public VaultEconomyHook(@NotNull HuskHomes plugin) { + super(plugin, "Vault (Economy)"); } @Override diff --git a/bukkit/src/main/java/net/william278/huskhomes/listener/BukkitEventListener.java b/bukkit/src/main/java/net/william278/huskhomes/listener/BukkitEventListener.java index bfc4207d..3c813862 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/listener/BukkitEventListener.java +++ b/bukkit/src/main/java/net/william278/huskhomes/listener/BukkitEventListener.java @@ -55,7 +55,7 @@ public class BukkitEventListener extends EventListener implements Listener { final BukkitUser bukkitUser = BukkitUser.adapt(player); BukkitAdapter.adaptLocation(event.getFrom()).ifPresent(sourceLocation -> - handlePlayerTeleport(bukkitUser, new Position(sourceLocation, plugin.getServerName()))); + handlePlayerTeleport(bukkitUser, Position.at(sourceLocation, plugin.getServerName()))); } //todo When defining paper-plugin.yml files gets merged, use the PlayerSetSpawnEvent in the paper module @@ -73,7 +73,7 @@ public class BukkitEventListener extends EventListener implements Listener { // Update the player's respawn location BukkitAdapter.adaptLocation(location).ifPresent(adaptedLocation -> { final OnlineUser onlineUser = BukkitUser.adapt(event.getPlayer()); - super.handlePlayerUpdateSpawnPoint(onlineUser, new Position( + super.handlePlayerUpdateSpawnPoint(onlineUser, Position.at( adaptedLocation.getX(), adaptedLocation.getY(), adaptedLocation.getZ(), adaptedLocation.getYaw(), adaptedLocation.getPitch(), adaptedLocation.getWorld(), plugin.getServerName())); diff --git a/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java b/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java index d4abe5e2..438be6c9 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java +++ b/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java @@ -45,7 +45,7 @@ public class BukkitUser extends OnlineUser { @Override public Position getPosition() { - return new Position(BukkitAdapter.adaptLocation(player.getLocation()) + return Position.at(BukkitAdapter.adaptLocation(player.getLocation()) .orElseThrow(() -> new IllegalStateException("Failed to get the position of a BukkitPlayer (null)")), plugin.getServerName()); @@ -54,7 +54,7 @@ public class BukkitUser extends OnlineUser { @Override public Optional getBedSpawnPosition() { return Optional.ofNullable(player.getBedSpawnLocation()).flatMap(BukkitAdapter::adaptLocation) - .map(location -> new Position(location, plugin.getServerName())); + .map(location -> Position.at(location, plugin.getServerName())); } @Override diff --git a/bukkit/src/main/java/net/william278/huskhomes/util/BukkitAdapter.java b/bukkit/src/main/java/net/william278/huskhomes/util/BukkitAdapter.java index 5ea2f2f5..9f8e6dac 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/util/BukkitAdapter.java +++ b/bukkit/src/main/java/net/william278/huskhomes/util/BukkitAdapter.java @@ -1,6 +1,7 @@ package net.william278.huskhomes.util; import net.william278.huskhomes.position.Location; +import net.william278.huskhomes.position.World; import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -38,21 +39,21 @@ public final class BukkitAdapter { */ public static Optional adaptLocation(@NotNull org.bukkit.Location location) { if (location.getWorld() == null) return Optional.empty(); - return Optional.of(new Location(location.getX(), location.getY(), location.getZ(), + return Optional.of(Location.at(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), adaptWorld(location.getWorld()).orElse(new net.william278.huskhomes.position.World()))); } - public static Optional adaptWorld(@Nullable org.bukkit.World world) { + public static Optional adaptWorld(@Nullable org.bukkit.World world) { if (world == null) { return Optional.empty(); } - return Optional.of(new net.william278.huskhomes.position.World(world.getName(), world.getUID(), + return Optional.of(World.from(world.getName(), world.getUID(), switch (world.getEnvironment()) { - case NORMAL -> net.william278.huskhomes.position.World.Environment.OVERWORLD; - case NETHER -> net.william278.huskhomes.position.World.Environment.NETHER; - case THE_END -> net.william278.huskhomes.position.World.Environment.THE_END; - case CUSTOM -> net.william278.huskhomes.position.World.Environment.CUSTOM; + case NORMAL -> World.Environment.OVERWORLD; + case NETHER -> World.Environment.NETHER; + case THE_END -> World.Environment.THE_END; + case CUSTOM -> World.Environment.CUSTOM; }) ); } diff --git a/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java b/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java index e0d3bb20..f8b7de24 100644 --- a/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java +++ b/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java @@ -271,9 +271,9 @@ public class BukkitPluginTests { @Order(4) public void testWarpRelocation(@NotNull String name, @NotNull Position position) { final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); - final Position newPosition = new Position(position.getX() + 10, position.getY() + 10, position.getZ() + 10, + final Position newPosition = Position.at(position.getX() + 10, position.getY() + 10, position.getZ() + 10, 0, 0, world, plugin.getServerName()); - plugin.getManager().warps().relocateWarp(name, newPosition); + plugin.getManager().warps().setWarpPosition(name, newPosition); final Optional warpPosition = plugin.getDatabase().getWarp(name).map(warp -> warp); Assertions.assertTrue(warpPosition.isPresent()); @@ -288,7 +288,7 @@ public class BukkitPluginTests { @Order(5) public void testWarpOverwrite(@NotNull String name, @NotNull Position position) { final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); - final Position newPosition = new Position(position.getX() + 10, position.getY() + 10, position.getZ() + 10, + final Position newPosition = Position.at(position.getX() + 10, position.getY() + 10, position.getZ() + 10, 0, 0, world, plugin.getServerName()); plugin.getManager().warps().createWarp(name, newPosition, true); @@ -327,7 +327,7 @@ public class BukkitPluginTests { @NotNull public static Stream provideWarpData() { final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); - final Position position = new Position(0, 0, 0, 0, 0, world, plugin.getServerName()); + final Position position = Position.at(0, 0, 0, 0, 0, world, plugin.getServerName()); return WARP_NAMES.stream() .flatMap(name -> Stream.of(Arguments.of(name, position))); } @@ -415,7 +415,7 @@ public class BukkitPluginTests { @Order(4) public void testHomeRelocation(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) { final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); - final Position newPosition = new Position(position.getX() + 10, position.getY() + 10, position.getZ() + 10, + final Position newPosition = Position.at(position.getX() + 10, position.getY() + 10, position.getZ() + 10, 0, 0, world, plugin.getServerName()); plugin.getManager().homes().setHomePosition(owner, name, newPosition); Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent()); @@ -434,7 +434,7 @@ public class BukkitPluginTests { @Order(5) public void testHomeOverwrite(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) { final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); - final Position newPosition = new Position(position.getX() + 10, position.getY() + 10, position.getZ() + 10, + final Position newPosition = Position.at(position.getX() + 10, position.getY() + 10, position.getZ() + 10, 0, 0, world, plugin.getServerName()); plugin.getManager().homes().setHomePosition(owner, name, newPosition); Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent()); @@ -507,7 +507,7 @@ public class BukkitPluginTests { @NotNull public static Stream provideHomeData() { final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow(); - final Position position = new Position(0, 0, 0, 0, 0, world, plugin.getServerName()); + final Position position = Position.at(0, 0, 0, 0, 0, world, plugin.getServerName()); return HOME_NAMES.stream() .flatMap(name -> Stream.of(Arguments.of(homeOwner, name, position))); } diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index b96433e3..7b2eec4b 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -18,7 +18,7 @@ import net.william278.huskhomes.manager.Manager; import net.william278.huskhomes.network.Broker; import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Position; -import net.william278.huskhomes.position.Server; +import net.william278.huskhomes.config.Server; import net.william278.huskhomes.position.World; import net.william278.huskhomes.random.RandomTeleportEngine; import net.william278.huskhomes.user.ConsoleUser; diff --git a/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java b/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java index 2b856fec..a19555ac 100644 --- a/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java +++ b/common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java @@ -18,6 +18,7 @@ import java.util.List; import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; import java.util.stream.Collectors; /** @@ -123,6 +124,40 @@ public abstract class BaseHuskHomesAPI { plugin.runAsync(() -> plugin.getDatabase().updateUserData(savedUser)); } + /** + * Edit {@link SavedUser} data for a given player by username, if they exist. + * + * @param username The username of the user to edit data for. + * @param editor A {@link Consumer} that will be passed the {@link SavedUser} to edit + * @since 4.0 + */ + public final void editUserData(@NotNull String username, @NotNull Consumer editor) { + plugin.runAsync(() -> { + Optional savedUser = plugin.getDatabase().getUserDataByName(username); + if (savedUser.isPresent()) { + editor.accept(savedUser.get()); + plugin.getDatabase().updateUserData(savedUser.get()); + } + }); + } + + /** + * Edit {@link SavedUser} data for the given player's account {@link UUID}, if they exist. + * + * @param uuid The {@link UUID} of the user to edit data for. + * @param editor A {@link Consumer} that will be passed the {@link SavedUser} to edit + * @since 4.0 + */ + public final void editUserData(@NotNull UUID uuid, @NotNull Consumer editor) { + plugin.runAsync(() -> { + Optional savedUser = plugin.getDatabase().getUserData(uuid); + if (savedUser.isPresent()) { + editor.accept(savedUser.get()); + plugin.getDatabase().updateUserData(savedUser.get()); + } + }); + } + /** * Get a list of {@link Home}s set by the given user. * @@ -142,11 +177,22 @@ public abstract class BaseHuskHomesAPI { * @since 3.0 */ public final CompletableFuture> getUserPublicHomes(@NotNull User user) { - return getUserHomes(user).thenApply(homes -> homes.stream() + return plugin.supplyAsync(() -> plugin.getDatabase().getHomes(user).stream() .filter(Home::isPublic) .collect(Collectors.toList())); } + /** + * Get a list of public homes local to this server + * + * @param user The {@link User} to get the local public homes of + * @return A {@link CompletableFuture} that will complete with a list of public {@link Home}s local to the server + * @since 4.0 + */ + public final CompletableFuture> getLocalPublicHomes(@NotNull User user) { + return plugin.supplyAsync(() -> plugin.getDatabase().getLocalPublicHomes(plugin)); + } + /** * Get a list of homes that have been made public ("public homes") * @@ -169,6 +215,7 @@ public abstract class BaseHuskHomesAPI { return plugin.supplyAsync(() -> plugin.getDatabase().getHome(user, homeName)); } + /** * Get a {@link Home} from the database by its' unique id * @@ -179,6 +226,141 @@ public abstract class BaseHuskHomesAPI { return plugin.supplyAsync(() -> plugin.getDatabase().getHome(homeUuid)); } + /** + * Create a home for a given user with the specified name and position + * + * @param owner The {@link User} to create the home for + * @param name The name of the home + * @param position The {@link Position} of the home + * @since 4.0 + */ + public void createHome(@NotNull User owner, @NotNull String name, @NotNull Position position) { + plugin.runAsync(() -> plugin.getManager().homes().createHome(owner, name, position, false, false)); + } + + /** + * Delete a home for a given user with the specified name + * + * @param owner The {@link User} to delete the home for + * @param name The name of the home + * @since 4.0 + */ + public final void deleteHome(@NotNull User owner, @NotNull String name) { + plugin.runAsync(() -> plugin.getManager().homes().deleteHome(owner, name)); + } + + /** + * Delete a home + * + * @param home The {@link Home} to delete + * @since 4.0 + */ + public final void deleteHome(@NotNull Home home) { + plugin.runAsync(() -> plugin.getManager().homes().deleteHome(home)); + } + + /** + * Rename a home for a given user + * + * @param owner The {@link User} to rename the home for + * @param oldName The name of the home to rename + * @param newName The new name of the home + * @since 4.0 + */ + public final void renameHome(@NotNull User owner, @NotNull String oldName, @NotNull String newName) { + plugin.runAsync(() -> plugin.getManager().homes().setHomeName(owner, oldName, newName)); + } + + /** + * Rename a home + * + * @param home The {@link Home} to rename + * @param newName The new name of the home + * @since 4.0 + */ + public final void renameHome(@NotNull Home home, @NotNull String newName) { + plugin.runAsync(() -> plugin.getManager().homes().setHomeName(home, newName)); + } + + /** + * Set the privacy of a home + * + * @param owner The {@link User} to set the privacy of the home for + * @param name The name of the home + * @param isPublic Whether the home should be public or not + * @since 4.0 + */ + public final void setHomePrivacy(@NotNull User owner, @NotNull String name, boolean isPublic) { + plugin.runAsync(() -> plugin.getManager().homes().setHomePrivacy(owner, name, isPublic)); + } + + /** + * Set the privacy of a home + * + * @param home The {@link Home} to set the privacy of + * @param isPublic Whether the home should be public or not + * @since 4.0 + */ + public final void setHomePrivacy(@NotNull Home home, boolean isPublic) { + plugin.runAsync(() -> plugin.getManager().homes().setHomePrivacy(home, isPublic)); + } + + /** + * Set the description of a home + * + * @param owner The {@link User} to set the description of the home for + * @param name The name of the home + * @param description The description of the home + * @since 4.0 + */ + public final void setHomeDescription(@NotNull User owner, @NotNull String name, @NotNull String description) { + plugin.runAsync(() -> plugin.getManager().homes().setHomeDescription(owner, name, description)); + } + + /** + * Set the description of a home + * + * @param home The {@link Home} to set the description of + * @param description The description of the home + * @since 4.0 + */ + public final void setHomeDescription(@NotNull Home home, @NotNull String description) { + plugin.runAsync(() -> plugin.getManager().homes().setHomeDescription(home, description)); + } + + /** + * Relocate a home for a given user + * + * @param owner The {@link User} to relocate the home for + * @param name The name of the home + * @param position The new {@link Position} of the home + * @since 4.0 + */ + public final void relocateHome(@NotNull User owner, @NotNull String name, @NotNull Position position) { + plugin.runAsync(() -> plugin.getManager().homes().setHomePosition(owner, name, position)); + } + + /** + * Relocate a home + * + * @param home The {@link Home} to relocate + * @param position The new {@link Position} of the home + * @since 4.0 + */ + public final void relocateHome(@NotNull Home home, @NotNull Position position) { + plugin.runAsync(() -> plugin.getManager().homes().setHomePosition(home, position)); + } + + /** + * Get a list of {@link Warp}s local to this server + * + * @return A {@link CompletableFuture} that will complete with a list of {@link Warp}s local to the server + * @since 4.0 + */ + public final CompletableFuture> getLocalWarps() { + return plugin.supplyAsync(() -> plugin.getDatabase().getLocalWarps(plugin)); + } + /** * Get a list of {@link Warp}s * @@ -210,6 +392,95 @@ public abstract class BaseHuskHomesAPI { return plugin.supplyAsync(() -> plugin.getDatabase().getWarp(warpUuid)); } + /** + * Create a new {@link Warp} with the given name and {@link Position} + * + * @param name The name of the warp + * @param position The {@link Position} of the warp + * @since 4.0 + */ + public final void createWarp(@NotNull String name, @NotNull Position position) { + plugin.runAsync(() -> plugin.getManager().warps().createWarp(name, position)); + } + + /** + * Delete a {@link Warp} with the given name + * + * @param name The name of the warp to delete + */ + public final void deleteWarp(@NotNull String name) { + plugin.runAsync(() -> plugin.getManager().warps().deleteWarp(name)); + } + + /** + * Delete a {@link Warp} + * + * @param warp The {@link Warp} to delete + */ + public final void deleteWarp(@NotNull Warp warp) { + plugin.runAsync(() -> plugin.getManager().warps().deleteWarp(warp)); + } + + /** + * Rename a {@link Warp} by name + * + * @param oldName The name of the warp to rename + * @param newName The new name of the warp + */ + public final void renameWarp(@NotNull String oldName, @NotNull String newName) { + plugin.runAsync(() -> plugin.getManager().warps().setWarpName(oldName, newName)); + } + + /** + * Rename a {@link Warp} + * + * @param warp The {@link Warp} to rename + * @param newName The new name of the warp + */ + public final void renameWarp(@NotNull Warp warp, @NotNull String newName) { + plugin.runAsync(() -> plugin.getManager().warps().setWarpName(warp, newName)); + } + + /** + * Set the description of a {@link Warp} by name + * + * @param name The name of the warp to set the description of + * @param description The new description of the warp + */ + public final void setWarpDescription(@NotNull String name, @NotNull String description) { + plugin.runAsync(() -> plugin.getManager().warps().setWarpDescription(name, description)); + } + + /** + * Set the description of a {@link Warp} + * + * @param warp The {@link Warp} to set the description of + * @param description The new description of the warp + */ + public final void setWarpDescription(@NotNull Warp warp, @NotNull String description) { + plugin.runAsync(() -> plugin.getManager().warps().setWarpDescription(warp, description)); + } + + /** + * Set the {@link Position} of a {@link Warp} by name + * + * @param name The name of the warp to set the {@link Position} of + * @param position The new {@link Position} of the warp + */ + public final void relocateWarp(@NotNull String name, @NotNull Position position) { + plugin.runAsync(() -> plugin.getManager().warps().setWarpPosition(name, position)); + } + + /** + * Set the {@link Position} of a {@link Warp} + * + * @param warp The {@link Warp} to set the {@link Position} of + * @param position The new {@link Position} of the warp + */ + public final void relocateWarp(@NotNull Warp warp, @NotNull Position position) { + plugin.runAsync(() -> plugin.getManager().warps().setWarpPosition(warp, position)); + } + /** * Get the canonical {@link Position} of the spawn point. Note that if cross-server and global spawn * are enabled in the config, this may not return a position on this server. @@ -278,7 +549,7 @@ public abstract class BaseHuskHomesAPI { * @param rtpArgs Arguments that will be passed to the implementing {@link RandomTeleportEngine} * @since 3.0 */ - public final void randomlyTeleportPlayer(@NotNull OnlineUser user, final boolean timedTeleport, @NotNull String... rtpArgs) { + public final void randomlyTeleportPlayer(@NotNull OnlineUser user, boolean timedTeleport, @NotNull String... rtpArgs) { plugin.runAsync(() -> { final Optional position = plugin.getRandomTeleportEngine() .getRandomPosition(user.getPosition().getWorld(), rtpArgs); diff --git a/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java index a62bc8d1..f97ff04b 100644 --- a/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/DelHomeCommand.java @@ -11,8 +11,8 @@ import java.util.List; public class DelHomeCommand extends SavedPositionCommand { - public DelHomeCommand(@NotNull HuskHomes implementor) { - super("delhome", List.of(), Home.class, List.of(), implementor); + public DelHomeCommand(@NotNull HuskHomes plugin) { + super("delhome", List.of(), Home.class, List.of(), plugin); } @Override diff --git a/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java index 9cd4987c..62acc215 100644 --- a/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/DelWarpCommand.java @@ -10,8 +10,8 @@ import java.util.List; public class DelWarpCommand extends SavedPositionCommand { - public DelWarpCommand(@NotNull HuskHomes implementor) { - super("delwarp", List.of(), Warp.class, List.of(), implementor); + public DelWarpCommand(@NotNull HuskHomes plugin) { + super("delwarp", List.of(), Warp.class, List.of(), plugin); } @Override diff --git a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java index a57aacaa..69c505af 100644 --- a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java @@ -88,7 +88,7 @@ public class EditHomeCommand extends SavedPositionCommand { private void setHomeDescription(@NotNull CommandUser executor, @NotNull Home home, boolean ownerEditing, @NotNull String[] args) { final String oldDescription = home.getMeta().getDescription(); - final Optional optionalDescription = parseGreedyString(args, 1); + final Optional optionalDescription = parseGreedyArguments(args); if (optionalDescription.isEmpty()) { plugin.getLocales().getLocale("error_invalid_syntax", "/edithome " + home.getName() + " description ") diff --git a/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java b/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java index 83a12392..b0c167aa 100644 --- a/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/EditWarpCommand.java @@ -67,7 +67,7 @@ public class EditWarpCommand extends SavedPositionCommand { } private void setWarpDescription(@NotNull CommandUser executor, @NotNull Warp warp, @NotNull String[] args) { - final Optional optionalDescription = parseGreedyString(args, 1); + final Optional optionalDescription = parseGreedyArguments(args); if (optionalDescription.isEmpty()) { plugin.getLocales().getLocale("error_invalid_syntax", "/editwarp " + warp.getName() + " description ") @@ -99,7 +99,7 @@ public class EditWarpCommand extends SavedPositionCommand { warp.update(user.getPosition()); plugin.fireEvent(plugin.getWarpEditEvent(warp, executor), (event) -> { try { - plugin.getManager().warps().relocateWarp(warp, warp); + plugin.getManager().warps().setWarpPosition(warp, warp); } catch (ValidationException e) { e.dispatchWarpError(executor, plugin, warp.getName()); return; diff --git a/common/src/main/java/net/william278/huskhomes/command/Node.java b/common/src/main/java/net/william278/huskhomes/command/Node.java index 19fe17a9..335dfbb0 100644 --- a/common/src/main/java/net/william278/huskhomes/command/Node.java +++ b/common/src/main/java/net/william278/huskhomes/command/Node.java @@ -114,15 +114,15 @@ public abstract class Node implements Executable { return Optional.of(plugin.getWorlds().stream() .filter(world -> world.getName().equalsIgnoreCase(worldName)) .findFirst() - .orElse(new World(worldName, new UUID(0, 0), environment))); + .orElse(World.from(worldName, new UUID(0, 0), environment))); } return Optional.empty(); } - protected Optional parseGreedyString(@NotNull String[] args, int startIndex) { - if (args.length > startIndex) { + protected Optional parseGreedyArguments(@NotNull String[] args) { + if (args.length > 1) { final StringJoiner sentence = new StringJoiner(" "); - for (int i = startIndex; i < args.length; i++) { + for (int i = 1; i < args.length; i++) { sentence.add(args[i]); } return Optional.of(sentence.toString().trim()); @@ -132,7 +132,7 @@ public abstract class Node implements Executable { protected Position getBasePosition(@NotNull CommandUser executor) { return executor instanceof OnlineUser user ? user.getPosition() : plugin.getSpawn() - .orElse(new Position(0, 0, 0, 0, 0, plugin.getWorlds().get(0), plugin.getServerName())); + .orElse(Position.at(0, 0, 0, 0, 0, plugin.getWorlds().get(0), plugin.getServerName())); } protected Optional parsePositionArgs(@NotNull Position basePosition, @NotNull String[] args, int startIndex) { @@ -153,7 +153,7 @@ public abstract class Node implements Executable { final Optional yaw = parseFloatArg(args, startIndex + angleStartIndex); final Optional pitch = parseFloatArg(args, startIndex + angleStartIndex + 1); - return Optional.of(new Position(x.get(), y.get(), z.get(), + return Optional.of(Position.at(x.get(), y.get(), z.get(), yaw.orElse(basePosition.getYaw()), pitch.orElse(basePosition.getPitch()), world.orElse(basePosition.getWorld()), server.orElse(basePosition.getServer()))); } 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 4ec6581f..dd4dae68 100644 --- a/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/RtpCommand.java @@ -64,13 +64,14 @@ public class RtpCommand extends Command { .orElseThrow(() -> new IllegalStateException("No user data found for " + teleporter.getUsername())); final Instant currentTime = Instant.now(); if (executor.equals(teleporter) && !currentTime.isAfter(user.getRtpCooldown()) && - !executor.hasPermission(getPermission("bypass_cooldown"))) { + !executor.hasPermission(getPermission("bypass_cooldown"))) { plugin.getLocales().getLocale("error_rtp_cooldown", Long.toString(currentTime.until(user.getRtpCooldown(), ChronoUnit.MINUTES) + 1)) .ifPresent(executor::sendMessage); return; } + // Generate a random position plugin.getLocales().getLocale("teleporting_random_generation") .ifPresent(teleporter::sendMessage); final Optional position = plugin.getRandomTeleportEngine() diff --git a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java index 72041dbe..990ca681 100644 --- a/common/src/main/java/net/william278/huskhomes/command/TpCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/TpCommand.java @@ -14,8 +14,8 @@ import java.util.stream.Collectors; public class TpCommand extends Command implements TabProvider { - protected TpCommand(@NotNull HuskHomes implementor) { - super("tp", List.of("tpo"), "[] [target]", implementor); + protected TpCommand(@NotNull HuskHomes plugin) { + super("tp", List.of("tpo"), "[] [target]", plugin); setOperatorCommand(true); } diff --git a/common/src/main/java/net/william278/huskhomes/config/Locales.java b/common/src/main/java/net/william278/huskhomes/config/Locales.java index 259ca5dc..48980c85 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Locales.java +++ b/common/src/main/java/net/william278/huskhomes/config/Locales.java @@ -172,4 +172,14 @@ public class Locales { .setSpaceBeforeFooter(false); } + /** + * Represents where a locale should be displayed + */ + public enum DisplaySlot { + CHAT, + ACTION_BAR, + SUBTITLE, + TITLE + } + } diff --git a/common/src/main/java/net/william278/huskhomes/position/Server.java b/common/src/main/java/net/william278/huskhomes/config/Server.java similarity index 97% rename from common/src/main/java/net/william278/huskhomes/position/Server.java rename to common/src/main/java/net/william278/huskhomes/config/Server.java index 3fb3fbc4..0f6fd298 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Server.java +++ b/common/src/main/java/net/william278/huskhomes/config/Server.java @@ -1,4 +1,4 @@ -package net.william278.huskhomes.position; +package net.william278.huskhomes.config; import net.william278.annotaml.YamlFile; import net.william278.annotaml.YamlKey; diff --git a/common/src/main/java/net/william278/huskhomes/config/Settings.java b/common/src/main/java/net/william278/huskhomes/config/Settings.java index 1e1f9770..f43794b5 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Settings.java +++ b/common/src/main/java/net/william278/huskhomes/config/Settings.java @@ -100,7 +100,7 @@ public class Settings { private int maxPublicHomes = 10; @YamlKey("general.stack_permission_limits") - private boolean stackPermissionLimits = true; + private boolean stackPermissionLimits = false; @YamlKey("general.permission_restrict_warps") private boolean permissionRestrictWarps = false; @@ -112,7 +112,7 @@ public class Settings { private int teleportWarmupTime = 5; @YamlKey("general.teleport_warmup_display") - private MessageDisplayType teleportWarmupDisplay = MessageDisplayType.ACTION_BAR; + private Locales.DisplaySlot teleportWarmupDisplay = Locales.DisplaySlot.ACTION_BAR; @YamlKey("general.teleport_request_expiry_time") private int teleportRequestExpiryTime = 60; @@ -351,7 +351,7 @@ public class Settings { return teleportWarmupTime; } - public MessageDisplayType getTeleportWarmupDisplay() { + public Locales.DisplaySlot getTeleportWarmupDisplay() { return teleportWarmupDisplay; } @@ -503,16 +503,6 @@ public class Settings { return brigadierTabCompletion; } - /** - * Represents where a chat message should display - */ - public enum MessageDisplayType { - MESSAGE, - ACTION_BAR, - SUBTITLE, - TITLE - } - /** * Represents the sound effect to play when an action happens */ diff --git a/common/src/main/java/net/william278/huskhomes/config/Spawn.java b/common/src/main/java/net/william278/huskhomes/config/Spawn.java index 429de197..21adb202 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Spawn.java +++ b/common/src/main/java/net/william278/huskhomes/config/Spawn.java @@ -37,7 +37,7 @@ public class Spawn { */ @NotNull public Position getPosition(@NotNull String server) { - return new Position(x, y, z, yaw, pitch, new World(worldName, UUID.fromString(worldUuid)), server); + return Position.at(x, y, z, yaw, pitch, World.from(worldName, UUID.fromString(worldUuid)), server); } /** diff --git a/common/src/main/java/net/william278/huskhomes/database/Database.java b/common/src/main/java/net/william278/huskhomes/database/Database.java index 7932633f..39c04096 100644 --- a/common/src/main/java/net/william278/huskhomes/database/Database.java +++ b/common/src/main/java/net/william278/huskhomes/database/Database.java @@ -1,6 +1,7 @@ package net.william278.huskhomes.database; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.config.Server; import net.william278.huskhomes.position.*; import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.user.OnlineUser; @@ -86,7 +87,7 @@ public abstract class Database { * (Internal use only) - Updates position data * * @param positionId ID of the position to update - * @param position the new position + * @param position the Position.at * @param connection SQL connection * @throws SQLException if an SQL exception occurs doing this */ diff --git a/common/src/main/java/net/william278/huskhomes/database/MySqlDatabase.java b/common/src/main/java/net/william278/huskhomes/database/MySqlDatabase.java index 7764c82a..c3c7c0e9 100644 --- a/common/src/main/java/net/william278/huskhomes/database/MySqlDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/database/MySqlDatabase.java @@ -302,15 +302,15 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { - userHomes.add(new Home(resultSet.getDouble("x"), + userHomes.add(Home.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -338,15 +338,15 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { - warps.add(new Warp(resultSet.getDouble("x"), + warps.add(Warp.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -374,15 +374,15 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { - userHomes.add(new Home(resultSet.getDouble("x"), + userHomes.add(Home.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -414,15 +414,15 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Home(resultSet.getDouble("x"), + return Optional.of(Home.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -451,15 +451,15 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Home(resultSet.getDouble("x"), + return Optional.of(Home.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -488,15 +488,15 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Warp(resultSet.getDouble("x"), + return Optional.of(Warp.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -522,15 +522,15 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Warp(resultSet.getDouble("x"), + return Optional.of(Warp.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -557,12 +557,12 @@ public class MySqlDatabase extends Database { if (resultSet.next()) { return Optional.of(Teleport.builder(plugin) .teleporter(onlineUser) - .target(new Position(resultSet.getDouble("x"), + .target(Position.at(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"))) .type(Teleport.Type.getTeleportType(resultSet.getInt("type")) @@ -640,12 +640,12 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Position(resultSet.getDouble("x"), + return Optional.of(Position.at(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"))); } @@ -699,12 +699,12 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Position(resultSet.getDouble("x"), + return Optional.of(Position.at(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"))); } @@ -757,12 +757,12 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Position(resultSet.getDouble("x"), + return Optional.of(Position.at(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"))); } diff --git a/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java b/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java index 358090b4..be9b34d8 100644 --- a/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java @@ -38,9 +38,9 @@ public class SqLiteDatabase extends Database { private Connection connection; - public SqLiteDatabase(@NotNull HuskHomes implementor) { - super(implementor); - this.databaseFile = new File(implementor.getDataFolder(), DATABASE_FILE_NAME); + public SqLiteDatabase(@NotNull HuskHomes plugin) { + super(plugin); + this.databaseFile = new File(plugin.getDataFolder(), DATABASE_FILE_NAME); } private Connection getConnection() throws SQLException { @@ -310,15 +310,15 @@ public class SqLiteDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { - userHomes.add(new Home(resultSet.getDouble("x"), + userHomes.add(Home.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -346,15 +346,15 @@ public class SqLiteDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { - warps.add(new Warp(resultSet.getDouble("x"), + warps.add(Warp.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -382,15 +382,15 @@ public class SqLiteDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { - userHomes.add(new Home(resultSet.getDouble("x"), + userHomes.add(Home.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -422,15 +422,15 @@ public class SqLiteDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Home(resultSet.getDouble("x"), + return Optional.of(Home.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -459,15 +459,15 @@ public class SqLiteDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Home(resultSet.getDouble("x"), + return Optional.of(Home.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -496,15 +496,15 @@ public class SqLiteDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Warp(resultSet.getDouble("x"), + return Optional.of(Warp.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -530,15 +530,15 @@ public class SqLiteDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Warp(resultSet.getDouble("x"), + return Optional.of(Warp.from(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"), - new PositionMeta(resultSet.getString("name"), + PositionMeta.from(resultSet.getString("name"), resultSet.getString("description"), resultSet.getTimestamp("timestamp").toInstant(), resultSet.getString("tags")), @@ -565,12 +565,12 @@ public class SqLiteDatabase extends Database { if (resultSet.next()) { return Optional.of(Teleport.builder(plugin) .teleporter(onlineUser) - .target(new Position(resultSet.getDouble("x"), + .target(Position.at(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"))) .type(Teleport.Type.getTeleportType(resultSet.getInt("type")) @@ -648,12 +648,12 @@ public class SqLiteDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Position(resultSet.getDouble("x"), + return Optional.of(Position.at(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"))); } @@ -705,12 +705,12 @@ public class SqLiteDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Position(resultSet.getDouble("x"), + return Optional.of(Position.at(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"))); } @@ -760,12 +760,12 @@ public class SqLiteDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new Position(resultSet.getDouble("x"), + return Optional.of(Position.at(resultSet.getDouble("x"), resultSet.getDouble("y"), resultSet.getDouble("z"), resultSet.getFloat("yaw"), resultSet.getFloat("pitch"), - new World(resultSet.getString("world_name"), + World.from(resultSet.getString("world_name"), UUID.fromString(resultSet.getString("world_uuid"))), resultSet.getString("server_name"))); } diff --git a/common/src/main/java/net/william278/huskhomes/hook/EconomyHook.java b/common/src/main/java/net/william278/huskhomes/hook/EconomyHook.java index 8617f9a4..45cd4041 100644 --- a/common/src/main/java/net/william278/huskhomes/hook/EconomyHook.java +++ b/common/src/main/java/net/william278/huskhomes/hook/EconomyHook.java @@ -9,8 +9,8 @@ import org.jetbrains.annotations.NotNull; */ public abstract class EconomyHook extends Hook { - protected EconomyHook(@NotNull HuskHomes implementor, @NotNull String hookName) { - super(implementor, hookName); + protected EconomyHook(@NotNull HuskHomes plugin, @NotNull String hookName) { + super(plugin, hookName); } /** diff --git a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java index f4710ed7..a4a0f5e9 100644 --- a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java +++ b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java @@ -22,8 +22,8 @@ public class EventListener { @NotNull protected final HuskHomes plugin; - protected EventListener(@NotNull HuskHomes implementor) { - this.plugin = implementor; + protected EventListener(@NotNull HuskHomes plugin) { + this.plugin = plugin; } /** @@ -89,11 +89,18 @@ public class EventListener { if (teleport.getType() == Teleport.Type.RESPAWN) { final Optional bedPosition = teleporter.getBedSpawnPosition(); if (bedPosition.isEmpty()) { - plugin.getLocalCachedSpawn() - .map(spawn -> spawn.getPosition(plugin.getServerName())) - .ifPresent(position -> teleporter.teleportLocally(position, - plugin.getSettings().isAsynchronousTeleports())); - teleporter.sendTranslatableMessage("block.minecraft.spawn.not_valid"); + plugin.getSpawn().ifPresent(spawn -> { + if (plugin.getSettings().isCrossServer() && !spawn.getServer().equals(plugin.getServerName())) { + plugin.runLater(() -> Teleport.builder(plugin) + .teleporter(teleporter) + .target(spawn) + .updateLastPosition(false) + .toTeleport().execute(), 40L); + } else { + teleporter.teleportLocally(spawn, plugin.getSettings().isAsynchronousTeleports()); + } + teleporter.sendTranslatableMessage("block.minecraft.spawn.not_valid"); + }); } else { teleporter.teleportLocally(bedPosition.get(), plugin.getSettings().isAsynchronousTeleports()); } @@ -104,8 +111,7 @@ public class EventListener { teleporter.teleportLocally((Position) teleport.getTarget(), plugin.getSettings().isAsynchronousTeleports()); plugin.getDatabase().setCurrentTeleport(teleporter, null); - plugin.getLocales().getLocale("teleporting_complete") - .ifPresent(teleporter::sendMessage); + teleport.displayTeleportingComplete(teleporter); }); } @@ -146,7 +152,7 @@ public class EventListener { protected final void handlePlayerDeath(@NotNull OnlineUser onlineUser) { // Set the player's last position to where they died if (plugin.getSettings().isBackCommandReturnByDeath() - && onlineUser.hasPermission(Permission.COMMAND_BACK_RETURN_BY_DEATH.node)) { + && onlineUser.hasPermission(Permission.COMMAND_BACK_RETURN_BY_DEATH.node)) { plugin.getDatabase().setLastPosition(onlineUser, onlineUser.getPosition()); } } @@ -160,8 +166,8 @@ public class EventListener { plugin.runAsync(() -> { // Display the return by death via /back notification if (plugin.getSettings().isBackCommandReturnByDeath() - && onlineUser.hasPermission(Permission.COMMAND_BACK.node) - && onlineUser.hasPermission(Permission.COMMAND_BACK_RETURN_BY_DEATH.node)) { + && onlineUser.hasPermission(Permission.COMMAND_BACK.node) + && onlineUser.hasPermission(Permission.COMMAND_BACK_RETURN_BY_DEATH.node)) { plugin.getLocales().getLocale("return_by_death_notification") .ifPresent(onlineUser::sendMessage); } diff --git a/common/src/main/java/net/william278/huskhomes/manager/HomesManager.java b/common/src/main/java/net/william278/huskhomes/manager/HomesManager.java index 56a7efa6..2ac52cd3 100644 --- a/common/src/main/java/net/william278/huskhomes/manager/HomesManager.java +++ b/common/src/main/java/net/william278/huskhomes/manager/HomesManager.java @@ -150,7 +150,7 @@ public class HomesManager { existing.update(position); return existing; }) - .orElse(new Home(position, new PositionMeta(name, ""), owner)); + .orElse(Home.from(position, PositionMeta.create(name, ""), owner)); plugin.getDatabase().saveHome(home); this.cacheHome(home, true); } diff --git a/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java b/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java index 76fae83c..a9ac6501 100644 --- a/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java +++ b/common/src/main/java/net/william278/huskhomes/manager/WarpsManager.java @@ -91,7 +91,7 @@ public class WarpsManager { existing.setPitch(position.getPitch()); return existing; }) - .orElse(new Warp(position, new PositionMeta(name, ""))); + .orElse(Warp.from(position, PositionMeta.create(name, ""))); plugin.getDatabase().saveWarp(warp); this.cacheWarp(warp, true); } @@ -122,16 +122,16 @@ public class WarpsManager { return deleted; } - public void relocateWarp(@NotNull String name, @NotNull Position position) throws ValidationException { + public void setWarpPosition(@NotNull String name, @NotNull Position position) throws ValidationException { final Optional optionalWarp = plugin.getDatabase().getWarp(name); if (optionalWarp.isEmpty()) { throw new ValidationException(ValidationException.Type.NOT_FOUND); } - this.relocateWarp(optionalWarp.get(), position); + this.setWarpPosition(optionalWarp.get(), position); } - public void relocateWarp(@NotNull Warp warp, @NotNull Position position) { + public void setWarpPosition(@NotNull Warp warp, @NotNull Position position) { warp.update(position); plugin.getDatabase().saveWarp(warp); this.cacheWarp(warp, true); diff --git a/common/src/main/java/net/william278/huskhomes/position/Home.java b/common/src/main/java/net/william278/huskhomes/position/Home.java index 54b7a4df..143fd418 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Home.java +++ b/common/src/main/java/net/william278/huskhomes/position/Home.java @@ -13,19 +13,30 @@ public class Home extends SavedPosition { private final User owner; private boolean isPublic; - public Home(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server, - @NotNull PositionMeta positionMeta, @NotNull UUID uuid, @NotNull User owner, boolean isPublic) { + private Home(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server, + @NotNull PositionMeta positionMeta, @NotNull UUID uuid, @NotNull User owner, boolean isPublic) { super(x, y, z, yaw, pitch, world, server, positionMeta, uuid); this.owner = owner; this.setPublic(isPublic); } - public Home(@NotNull Position position, @NotNull PositionMeta meta, @NotNull User owner) { + private Home(@NotNull Position position, @NotNull PositionMeta meta, @NotNull User owner) { super(position, meta); this.owner = owner; this.setPublic(false); } + @NotNull + public static Home from(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server, + @NotNull PositionMeta positionMeta, @NotNull UUID uuid, @NotNull User owner, boolean isPublic) { + return new Home(x, y, z, yaw, pitch, world, server, positionMeta, uuid, owner, isPublic); + } + + @NotNull + public static Home from(@NotNull Position position, @NotNull PositionMeta meta, @NotNull User owner) { + return new Home(position, meta, owner); + } + /** * The {@link User} who owns this home */ diff --git a/common/src/main/java/net/william278/huskhomes/position/Location.java b/common/src/main/java/net/william278/huskhomes/position/Location.java index 3837a190..0a92c1fc 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Location.java +++ b/common/src/main/java/net/william278/huskhomes/position/Location.java @@ -1,30 +1,26 @@ package net.william278.huskhomes.position; +import com.google.gson.annotations.Expose; import org.jetbrains.annotations.NotNull; /** * Represents a local position on this server */ public class Location { - + @Expose private double x; + @Expose private double y; + @Expose private double z; + @Expose private float yaw; + @Expose private float pitch; + @Expose private World world; - public Location(double x, double y, double z, @NotNull World world) { - this.setX(x); - this.setY(y); - this.setZ(z); - this.setYaw(0); - this.setPitch(0); - this.setWorld(world); - } - - public Location(double x, double y, double z, float yaw, float pitch, - @NotNull World world) { + protected Location(double x, double y, double z, float yaw, float pitch, @NotNull World world) { this.setX(x); this.setY(y); this.setZ(z); @@ -36,6 +32,16 @@ public class Location { public Location() { } + @NotNull + public static Location at(double x, double y, double z, float yaw, float pitch, @NotNull World world) { + return new Location(x, y, z, yaw, pitch, world); + } + + @NotNull + public static Location at(double x, double y, double z, @NotNull World world) { + return Location.at(x, y, z, 0, 0, world); + } + /** * Update the position to match that of another position * diff --git a/common/src/main/java/net/william278/huskhomes/position/Position.java b/common/src/main/java/net/william278/huskhomes/position/Position.java index 49709257..c2b7e2c8 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Position.java +++ b/common/src/main/java/net/william278/huskhomes/position/Position.java @@ -1,5 +1,6 @@ package net.william278.huskhomes.position; +import com.google.gson.annotations.Expose; import net.william278.huskhomes.teleport.Target; import org.jetbrains.annotations.NotNull; @@ -8,15 +9,15 @@ import org.jetbrains.annotations.NotNull; */ public class Position extends Location implements Target { + @Expose private String server; - public Position(double x, double y, double z, float yaw, float pitch, - @NotNull World world, @NotNull String server) { + protected Position(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server) { super(x, y, z, yaw, pitch, world); this.setServer(server); } - public Position(@NotNull Location location, @NotNull String server) { + protected Position(@NotNull Location location, @NotNull String server) { super(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), location.getWorld()); this.setServer(server); } @@ -25,6 +26,21 @@ public class Position extends Location implements Target { private Position() { } + @NotNull + public static Position at(double x, double y, double z, float yaw, float pitch, @NotNull World world, @NotNull String server) { + return new Position(x, y, z, yaw, pitch, world, server); + } + + @NotNull + public static Position at(double x, double y, double z, @NotNull World world, @NotNull String server) { + return Position.at(x, y, z, 0, 0, world, server); + } + + @NotNull + public static Position at(@NotNull Location location, @NotNull String server) { + return new Position(location, server); + } + @Override public void update(@NotNull Position newPosition) { super.update(newPosition); diff --git a/common/src/main/java/net/william278/huskhomes/position/PositionMeta.java b/common/src/main/java/net/william278/huskhomes/position/PositionMeta.java index f719c26d..1d6559c4 100644 --- a/common/src/main/java/net/william278/huskhomes/position/PositionMeta.java +++ b/common/src/main/java/net/william278/huskhomes/position/PositionMeta.java @@ -8,7 +8,6 @@ import org.jetbrains.annotations.Nullable; import java.time.Instant; import java.util.HashMap; import java.util.Map; -import java.util.Optional; /** * Represents metadata about a {@link Position}, used in {@link SavedPosition} implementations @@ -16,24 +15,27 @@ import java.util.Optional; public class PositionMeta { private String name; - @Nullable private String description; private Map tags; private Instant creationTime; - public PositionMeta(@NotNull String name, @NotNull String description, @NotNull Instant creationTime, - @Nullable String serializedTags) { + private PositionMeta(@NotNull String name, @NotNull String description, + @NotNull Instant creationTime, @Nullable String serializedTags) { this.setName(name); this.setDescription(description); this.setCreationTime(creationTime); this.setTags(deserializeTags(serializedTags)); } - public PositionMeta(@NotNull String name, @NotNull String description) { - this.setName(name); - this.setDescription(description); - this.setCreationTime(Instant.now()); - this.setTags(new HashMap<>()); + @NotNull + public static PositionMeta from(@NotNull String name, @NotNull String description, + @NotNull Instant creationTime, @Nullable String serializedTags) { + return new PositionMeta(name, description, creationTime, serializedTags); + } + + @NotNull + public static PositionMeta create(@NotNull String name, @NotNull String description) { + return PositionMeta.from(name, description, Instant.now(), ""); } /** @@ -42,6 +44,7 @@ public class PositionMeta { * @param serializedTags The JSON string to deserialize * @return The deserialized {@link Map} */ + @NotNull private static Map deserializeTags(@Nullable String serializedTags) { try { if (serializedTags == null || serializedTags.isBlank()) { @@ -89,7 +92,7 @@ public class PositionMeta { */ @NotNull public String getDescription() { - return Optional.ofNullable(description).orElse(""); + return description; } public void setDescription(@Nullable String description) { diff --git a/common/src/main/java/net/william278/huskhomes/position/SavedPosition.java b/common/src/main/java/net/william278/huskhomes/position/SavedPosition.java index a290be7a..4736ba79 100644 --- a/common/src/main/java/net/william278/huskhomes/position/SavedPosition.java +++ b/common/src/main/java/net/william278/huskhomes/position/SavedPosition.java @@ -12,9 +12,8 @@ public abstract class SavedPosition extends Position implements Comparable + * Will return {@link Environment#OVERWORLD} if the environment is null */ @NotNull public Environment getEnvironment() { 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 7a498d59..7c3f079b 100644 --- a/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java +++ b/common/src/main/java/net/william278/huskhomes/random/NormalDistributionEngine.java @@ -13,19 +13,19 @@ import java.util.concurrent.CompletableFuture; /** * A random teleport engine that uses a Gaussian normal distribution to generate random positions. */ -public class NormalDistributionEngine extends RandomTeleportEngine { +public final class NormalDistributionEngine extends RandomTeleportEngine { - protected final int radius; - protected final int spawnRadius; + private final int radius; + private final int spawnRadius; private final float mean; private final float standardDeviation; - public NormalDistributionEngine(@NotNull HuskHomes implementor) { - super(implementor, "Normal Distribution"); - this.radius = implementor.getSettings().getRtpRadius(); - this.spawnRadius = implementor.getSettings().getRtpSpawnRadius(); - this.mean = implementor.getSettings().getRtpDistributionMean(); - this.standardDeviation = implementor.getSettings().getRtpDistributionStandardDeviation(); + public NormalDistributionEngine(@NotNull HuskHomes plugin) { + super(plugin, "Normal Distribution"); + this.radius = plugin.getSettings().getRtpRadius(); + this.spawnRadius = plugin.getSettings().getRtpSpawnRadius(); + this.mean = plugin.getSettings().getRtpDistributionMean(); + this.standardDeviation = plugin.getSettings().getRtpDistributionStandardDeviation(); } /** @@ -36,7 +36,7 @@ public class NormalDistributionEngine extends RandomTeleportEngine { * @return A generated location */ @NotNull - protected static Location generateLocation(@NotNull Location origin, float mean, float standardDeviation, + public static Location generateLocation(@NotNull Location origin, float mean, float standardDeviation, float spawnRadius, float maxRadius) { // Generate random values final float radius = getDistributedRadius(mean, standardDeviation, spawnRadius, maxRadius); @@ -46,7 +46,12 @@ public class NormalDistributionEngine extends RandomTeleportEngine { final float z = (float) (radius * Math.cos(angle)); final float x = (float) (radius * Math.sin(angle)); - return new Location(Math.round(origin.getX()) + x, 128d, Math.round(origin.getZ()) + z, origin.getWorld()); + return Location.at( + Math.round(origin.getX()) + x, + 128d, + Math.round(origin.getZ()) + z, + origin.getWorld() + ); } /** @@ -57,7 +62,7 @@ public class NormalDistributionEngine extends RandomTeleportEngine { */ private CompletableFuture> generateSafeLocation(@NotNull World world) { return plugin.resolveSafeGroundLocation(generateLocation( - getOrigin(world), mean, standardDeviation, spawnRadius, radius)); + getCenterPoint(world), mean, standardDeviation, spawnRadius, radius)); } /** @@ -90,10 +95,11 @@ public class NormalDistributionEngine extends RandomTeleportEngine { int attempts = 0; while (location.isEmpty()) { location = generateSafeLocation(world).join(); - if (attempts > randomTimeout) { + if (attempts >= maxAttempts) { return Optional.empty(); } + attempts++; } - return location.map(resolved -> new Position(resolved, plugin.getServerName())); + return location.map(resolved -> Position.at(resolved, plugin.getServerName())); } } diff --git a/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java b/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java index 468afec1..dfd3770c 100644 --- a/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java +++ b/common/src/main/java/net/william278/huskhomes/random/RandomTeleportEngine.java @@ -12,19 +12,9 @@ import java.util.Optional; */ public abstract class RandomTeleportEngine { - @NotNull protected final HuskHomes plugin; - - /** - * The name of the random teleport engine - */ - @NotNull public final String name; - - /** - * How many attempts to allow {@link #getRandomPosition} lookups before timing out - */ - public long randomTimeout = 8; + public long maxAttempts = 12; /** * Constructor for a random teleport engine @@ -37,15 +27,21 @@ public abstract class RandomTeleportEngine { this.name = name; } + @NotNull + public final String getName() { + return name; + } + /** * Get the origin position (spawn) of this server * * @return The origin position */ - protected final Position getOrigin(@NotNull World world) { + @NotNull + protected Position getCenterPoint(@NotNull World world) { return plugin.getLocalCachedSpawn() .map(s -> s.getPosition(plugin.getServerName())) - .orElse(new Position(0d, 128d, 0d, 0f, 0f, + .orElse(Position.at(0d, 128d, 0d, 0f, 0f, world, plugin.getServerName())); } @@ -56,7 +52,7 @@ public abstract class RandomTeleportEngine { * @param world The world to find a random position in * @param args The arguments to pass to the random teleport engine * @return The position, optionally, which will be empty if the random teleport engine timed out after a - * {@link #randomTimeout configured number of attempts} + * {@link #maxAttempts configured number of attempts} */ public abstract Optional getRandomPosition(@NotNull World world, @NotNull String[] args); diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java index 8b48bdbe..bf82e3c2 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java @@ -1,6 +1,7 @@ package net.william278.huskhomes.teleport; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.event.ITeleportEvent; import net.william278.huskhomes.hook.EconomyHook; import net.william278.huskhomes.network.Message; @@ -85,8 +86,7 @@ public class Teleport { fireEvent((event) -> { executeEconomyActions(); teleporter.teleportLocally(localTarget.get().getPosition(), async); - plugin.getLocales().getLocale("teleporting_complete") - .ifPresent(teleporter::sendMessage); + this.displayTeleportingComplete(teleporter); }); return; } @@ -98,8 +98,7 @@ public class Teleport { .type(Message.Type.TELEPORT_TO_NETWORKED_POSITION) .target(username.name()) .build().send(plugin.getMessenger(), executor); - plugin.getLocales().getLocale("teleporting_complete") - .ifPresent(teleporter::sendMessage); + this.displayTeleportingComplete(teleporter); }); return; } @@ -113,8 +112,7 @@ public class Teleport { final Position target = (Position) this.target; if (!plugin.getSettings().isCrossServer() || target.getServer().equals(plugin.getServerName())) { teleporter.teleportLocally(target, async); - plugin.getLocales().getLocale("teleporting_complete") - .ifPresent(teleporter::sendMessage); + this.displayTeleportingComplete(teleporter); return; } @@ -131,6 +129,13 @@ public class Teleport { return Optional.of((OnlineUser) this.teleporter); } + public void displayTeleportingComplete(@NotNull OnlineUser teleporter) { + plugin.getLocales().getLocale("teleporting_complete") + .ifPresent(teleporter::sendMessage); + plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_CANCELLED) + .ifPresent(teleporter::playSound); + } + // Fire the teleport event private void fireEvent(@NotNull Consumer afterFired) { plugin.fireEvent(plugin.getTeleportEvent(this), afterFired); diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java b/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java index 4f0567ed..37866137 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TimedTeleport.java @@ -132,12 +132,7 @@ public class TimedTeleport extends Teleport { } private void sendStatusMessage(@NotNull MineDown message) { - switch (plugin.getSettings().getTeleportWarmupDisplay()) { - case ACTION_BAR -> teleporter.sendActionBar(message); - case SUBTITLE -> teleporter.sendTitle(message, true); - case TITLE -> teleporter.sendTitle(message, false); - case MESSAGE -> teleporter.sendMessage(message); - } + teleporter.sendMessage(message, plugin.getSettings().getTeleportWarmupDisplay()); } private boolean hasTeleporterMoved() { diff --git a/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java b/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java index d45e8db7..ff616e43 100644 --- a/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java +++ b/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java @@ -8,6 +8,7 @@ import net.kyori.adventure.key.Key; import net.kyori.adventure.sound.Sound; import net.kyori.adventure.text.Component; import net.kyori.adventure.title.TitlePart; +import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.teleport.Teleportable; @@ -98,6 +99,24 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs .replace().toComponent()); } + /** + * Dispatch a MineDown-formatted message to this player + * + * @param mineDown the parsed {@link MineDown} to send + * @param slot the {@link Locales.DisplaySlot} to send the message to + */ + public final void sendMessage(@NotNull MineDown mineDown, @NotNull Locales.DisplaySlot slot) { + if (slot == Locales.DisplaySlot.CHAT) { + sendMessage(mineDown); + } else if (slot == Locales.DisplaySlot.ACTION_BAR) { + sendActionBar(mineDown); + } else if (slot == Locales.DisplaySlot.TITLE) { + sendTitle(mineDown, false); + } else if (slot == Locales.DisplaySlot.SUBTITLE) { + sendTitle(mineDown, true); + } + } + /** * Dispatch a Minecraft translatable keyed-message to this player * @@ -144,6 +163,7 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs /** * Send a plugin message to the user + * * @param channel channel to send it on * @param message byte array of message data */ diff --git a/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java b/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java index 221f72f1..5dfdaaa5 100644 --- a/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java +++ b/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java @@ -28,8 +28,8 @@ public class MessageSerializationTests { .type(Message.Type.TELEPORT_REQUEST) .target("TestTarget") .payload(Payload.withPosition( - new Position(63.25, 127.43, -32, 180f, -94.3f, - new World("TestWorld", UUID.randomUUID()), "TestServer"))) + Position.at(63.25, 127.43, -32, 180f, -94.3f, + World.from("TestWorld", UUID.randomUUID()), "TestServer"))) .build(), Message.builder() .type(Message.Type.TELEPORT_TO_NETWORKED_USER) @@ -45,8 +45,8 @@ public class MessageSerializationTests { .type(Message.Type.TELEPORT_TO_POSITION) .target("TestTarget") .payload(Payload.withPosition( - new Position(63.25, 127.43, -32, 180f, -94.3f, - new World("TestWorld", UUID.randomUUID()), "TestServer"))) + Position.at(63.25, 127.43, -32, 180f, -94.3f, + World.from("TestWorld", UUID.randomUUID()), "TestServer"))) .build() ); diff --git a/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java b/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java index da2a909b..475bec7b 100644 --- a/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java +++ b/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java @@ -86,7 +86,7 @@ public class NormalDistributionTests { final List locations = new ArrayList<>(); for (int i = 0; i < amount; i++) { locations.add(NormalDistributionEngine.generateLocation( - new Location(0, 0, 0, 0, 0, new World("TestWorld", UUID.randomUUID())), + Location.at(0, 0, 0, 0, 0, World.from("TestWorld", UUID.randomUUID())), MEAN, STANDARD_DEVIATION, SPAWN_RADIUS, MAX_RADIUS)); } return locations;