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 20f592e3..2ecca1df 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Settings.java +++ b/common/src/main/java/net/william278/huskhomes/config/Settings.java @@ -124,11 +124,11 @@ public final class Settings { private int maxHomes = 10; @Comment("The maximum public homes a user can create. " - + "Override with the huskhomes.max_public_homes. permission.") + + "Override with the huskhomes.max_public_homes. permission.") private int maxPublicHomes = 10; @Comment("Whether permission limits (i.e. huskhomes.max_homes.) should stack " - + "if the user inherits multiple nodes.") + + "if the user inherits multiple nodes.") private boolean stackPermissionLimits = false; @Comment("Whether users require a permission (huskhomes.warp.) to use warps") @@ -153,14 +153,14 @@ public final class Settings { private int teleportRequestExpiryTime = 60; @Comment("Whether /tpahere should use the location of the sender when sent. " - + "Docs: https://william278.net/docs/huskhomes/strict-tpahere/") + + "Docs: https://william278.net/docs/huskhomes/strict-tpahere/") private boolean strictTpaHereRequests = true; @Comment("How many items should be displayed per-page in chat menu lists") private int listItemsPerPage = 12; @Comment("Whether the user should always be put back at the /spawn point when they die " - + "(ignores beds/respawn anchors)") + + "(ignores beds/respawn anchors)") private boolean alwaysRespawnAtSpawn = false; @Comment("Whether teleportation should be carried out async (ensuring chunks load before teleporting)") @@ -180,7 +180,7 @@ public final class Settings { private boolean caseInsensitive = false; @Comment("Whether home and warp names should be restricted to a regex filter." - + "Set this to false to allow full UTF-8 names (i.e. allow /home δ½ ε₯½).") + + "Set this to false to allow full UTF-8 names (i.e. allow /home δ½ ε₯½).") private boolean restrict = true; @Comment("Regex which home and warp names must match. Names have a max length of 16 characters") @@ -195,11 +195,11 @@ public final class Settings { @NoArgsConstructor(access = AccessLevel.PRIVATE) public static class DescriptionSettings { @Comment("Whether home/warp descriptions should be restricted to a regex filter. " - + "Set this to true to restrict UTF-8 usage.") + + "Set this to true to restrict UTF-8 usage.") private boolean restrict = false; @Comment("Regex which home and warp descriptions must match. " - + "A hard max length of 256 characters is enforced") + + "A hard max length of 256 characters is enforced") private String regex = "\\A\\p{ASCII}*\\z"; } @@ -215,6 +215,19 @@ public final class Settings { @Comment("Whether /back should work with other plugins that use the PlayerTeleportEvent (can conflict)") private boolean saveOnTeleportEvent = false; + + @Comment({"List of world names where the /back command cannot RETURN the player to. ", + "A user's last position won't be updated if they die or teleport from these worlds, but they still " + + "will be able to use the command while IN the world."}) + private List restrictedWorlds = new ArrayList<>(); + + public boolean canReturnToWorld(@NotNull World world) { + final String name = world.getName(); + final String formattedName = name.replace("minecraft:", ""); + return restrictedWorlds.stream() + .map(n -> n.replace("minecraft:", "")) + .anyMatch(n -> n.equalsIgnoreCase(formattedName)); + } } @Comment("Settings for sound effects") @@ -286,7 +299,7 @@ public final class Settings { } @Comment("Define a single global /spawn for your network via a warp. " - + "Docs: https://william278.net/docs/huskhomes/global-spawn/") + + "Docs: https://william278.net/docs/huskhomes/global-spawn/") private GlobalSpawnSettings globalSpawn = new GlobalSpawnSettings(); @Getter @@ -300,7 +313,7 @@ public final class Settings { } @Comment("Whether player respawn positions should work cross-server. " - + "Docs: https://william278.net/docs/huskhomes/global-respawning/") + + "Docs: https://william278.net/docs/huskhomes/global-respawning/") private boolean globalRespawning = false; } @@ -428,7 +441,7 @@ public final class Settings { .anyMatch(disabled -> { final String command = (disabled.startsWith("/") ? disabled.substring(1) : disabled); return command.equalsIgnoreCase(type.getName()) - || type.getAliases().stream().anyMatch(alias -> alias.equalsIgnoreCase(command)); + || type.getAliases().stream().anyMatch(alias -> alias.equalsIgnoreCase(command)); }); } diff --git a/common/src/main/java/net/william278/huskhomes/hook/HookProvider.java b/common/src/main/java/net/william278/huskhomes/hook/HookProvider.java index efc891dd..1c76db77 100644 --- a/common/src/main/java/net/william278/huskhomes/hook/HookProvider.java +++ b/common/src/main/java/net/william278/huskhomes/hook/HookProvider.java @@ -20,7 +20,6 @@ package net.william278.huskhomes.hook; import com.google.common.collect.Lists; -import com.google.common.collect.Sets; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.importer.Importer; 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 ffdc45b2..70c43c48 100644 --- a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java +++ b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java @@ -24,17 +24,18 @@ import lombok.AccessLevel; import lombok.AllArgsConstructor; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.command.BackCommand; -import net.william278.huskhomes.command.Command; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.network.Broker; import net.william278.huskhomes.network.Message; import net.william278.huskhomes.network.Payload; import net.william278.huskhomes.position.Position; +import net.william278.huskhomes.position.World; import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.teleport.TeleportBuilder; import net.william278.huskhomes.teleport.TeleportationException; import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.user.User; +import org.jetbrains.annotations.Blocking; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -68,8 +69,8 @@ public abstract class EventListener { // Synchronize the global player list plugin.runSyncDelayed(() -> this.updateUserList( - onlineUser, - plugin.getOnlineUsers().stream().map(u -> (User) u).toList()), + onlineUser, + plugin.getOnlineUsers().stream().map(u -> (User) u).toList()), onlineUser, 40L ); @@ -108,7 +109,7 @@ public abstract class EventListener { online.removeInvulnerabilityIfPermitted(); plugin.runAsync(() -> { - // Set offline position + // Set offline position plugin.getDatabase().setOfflinePosition(online, online.getPosition()); // Remove this user's home cache @@ -166,7 +167,7 @@ public abstract class EventListener { if (bedPosition.isEmpty()) { plugin.getSpawn().ifPresent(spawn -> { if (plugin.getSettings().getCrossServer().isEnabled() - && !spawn.getServer().equals(plugin.getServerName())) { + && !spawn.getServer().equals(plugin.getServerName())) { plugin.runSyncDelayed(() -> Teleport.builder(plugin) .teleporter(teleporter) .target(spawn) @@ -220,11 +221,24 @@ public abstract class EventListener { */ protected final void handlePlayerDeath(@NotNull OnlineUser onlineUser) { final Position lastPosition = onlineUser.getPosition(); + final World lastWorld = lastPosition.getWorld(); + + // Check the user can use /back to return here + final BackCommand command = plugin.getCommand(BackCommand.class).orElse(null); + if (command == null || !onlineUser.hasPermission(command.getPermission()) || + !onlineUser.hasPermission(command.getPermission("back"))) { + return; + } + final Settings.GeneralSettings.BackCommandSettings back = plugin.getSettings().getGeneral().getBackCommand(); + if (!back.isReturnByDeath() || !back.canReturnToWorld(lastWorld)) { + return; + } + + // Update the player's last position plugin.runAsync(() -> { - if (plugin.getSettings().getGeneral().getBackCommand().isReturnByDeath() && plugin.getCommand(BackCommand.class) - .map(Command::getPermission).map(onlineUser::hasPermission).orElse(false)) { - plugin.getDatabase().setLastPosition(onlineUser, lastPosition); - } + plugin.getDatabase().setLastPosition(onlineUser, lastPosition); + plugin.getLocales().getLocale("return_by_death_notification") + .ifPresent(onlineUser::sendMessage); }); } @@ -234,26 +248,15 @@ public abstract class EventListener { * @param onlineUser the respawning {@link OnlineUser} */ protected final void handlePlayerRespawn(@NotNull OnlineUser onlineUser) { - plugin.runAsync(() -> { - // Display the return by death via /back notification - final boolean canReturnByDeath = plugin.getCommand(BackCommand.class) - .map(command -> onlineUser.hasPermission(command.getPermission()) - && onlineUser.hasPermission(command.getPermission("death"))) - .orElse(false); - if (plugin.getSettings().getGeneral().getBackCommand().isReturnByDeath() && canReturnByDeath) { - plugin.getLocales().getLocale("return_by_death_notification") - .ifPresent(onlineUser::sendMessage); - } - - // Respawn the player via the global respawn system - final Settings.CrossServerSettings crossServer = plugin.getSettings().getCrossServer(); - if (crossServer.isEnabled() && crossServer.isGlobalRespawning()) { - this.respawnGlobally(onlineUser); - } - }); + final Settings.CrossServerSettings crossServer = plugin.getSettings().getCrossServer(); + if (!crossServer.isEnabled() || !crossServer.isGlobalRespawning()) { + return; + } + plugin.runAsync(() -> this.respawnGlobally(onlineUser)); } // Respawn a player to where they should be + @Blocking private void respawnGlobally(@NotNull OnlineUser onlineUser) { final TeleportBuilder builder = Teleport.builder(plugin) .teleporter(onlineUser) @@ -289,12 +292,13 @@ public abstract class EventListener { * @param sourcePosition the source {@link Position} they came from */ protected final void handlePlayerTeleport(@NotNull OnlineUser onlineUser, @NotNull Position sourcePosition) { - if (!plugin.getSettings().getGeneral().getBackCommand().isSaveOnTeleportEvent()) { + final Settings.GeneralSettings.BackCommandSettings back = plugin.getSettings().getGeneral().getBackCommand(); + if (!back.isSaveOnTeleportEvent() || back.canReturnToWorld(sourcePosition.getWorld())) { return; } - plugin.runAsync(() -> plugin.getDatabase().getUser(onlineUser.getUuid()) - .ifPresent(data -> plugin.getDatabase().setLastPosition(data.getUser(), sourcePosition))); + // Set the player's last position + plugin.runAsync(() -> plugin.getDatabase().setLastPosition(onlineUser, sourcePosition)); } /** @@ -305,8 +309,8 @@ public abstract class EventListener { */ protected final void handlePlayerUpdateSpawnPoint(@NotNull OnlineUser onlineUser, @NotNull Position position) { if (!plugin.getSettings().getGeneral().isAlwaysRespawnAtSpawn() - && plugin.getSettings().getCrossServer().isEnabled() - && plugin.getSettings().getCrossServer().isGlobalRespawning()) { + && plugin.getSettings().getCrossServer().isEnabled() + && plugin.getSettings().getCrossServer().isGlobalRespawning()) { plugin.getDatabase().setRespawnPosition(onlineUser, position); } } diff --git a/common/src/main/java/net/william278/huskhomes/position/Warp.java b/common/src/main/java/net/william278/huskhomes/position/Warp.java index 7d356297..5b3ca2e8 100644 --- a/common/src/main/java/net/william278/huskhomes/position/Warp.java +++ b/common/src/main/java/net/william278/huskhomes/position/Warp.java @@ -20,7 +20,6 @@ package net.william278.huskhomes.position; import net.william278.huskhomes.user.CommandUser; -import net.william278.huskhomes.user.OnlineUser; import org.jetbrains.annotations.NotNull; import java.util.UUID; 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 167510f5..452d6033 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java @@ -67,7 +67,7 @@ public class Teleport implements Completable { this.async = plugin.getSettings().getGeneral().isTeleportAsync(); this.updateLastPosition = updateLastPosition && plugin.getCommand(BackCommand.class) .map(command -> executor.hasPermission(command.getPermission()) - && executor.hasPermission(command.getPermission("previous"))) + && executor.hasPermission(command.getPermission("previous"))) .orElse(false); } @@ -97,7 +97,7 @@ public class Teleport implements Completable { if (localTarget.isPresent()) { fireEvent((event) -> { performTransactions(); - if (updateLastPosition) { + if (updateLastPosition && canReturnToWorld(teleporter)) { plugin.getDatabase().setLastPosition(teleporter, teleporter.getPosition()); } teleporter.teleportLocally(localTarget.get().getPosition(), async); @@ -123,13 +123,13 @@ public class Teleport implements Completable { fireEvent((event) -> { performTransactions(); - if (updateLastPosition) { + if (updateLastPosition && canReturnToWorld(teleporter)) { plugin.getDatabase().setLastPosition(teleporter, teleporter.getPosition()); } final Position target = (Position) this.target; if (!plugin.getSettings().getCrossServer().isEnabled() - || target.getServer().equals(plugin.getServerName())) { + || target.getServer().equals(plugin.getServerName())) { teleporter.teleportLocally(target, async); this.displayTeleportingComplete(teleporter); teleporter.handleInvulnerability(); @@ -166,6 +166,10 @@ public class Teleport implements Completable { })); } + private boolean canReturnToWorld(@NotNull OnlineUser user) { + return plugin.getSettings().getGeneral().getBackCommand().canReturnToWorld(user.getPosition().getWorld()); + } + @NotNull private Optional resolveLocalTeleporter() throws TeleportationException { if (this.teleporter instanceof Username username) { diff --git a/docs/Back-Command.md b/docs/Back-Command.md new file mode 100644 index 00000000..54b6ebd6 --- /dev/null +++ b/docs/Back-Command.md @@ -0,0 +1,78 @@ +The /back [command](commands) lets users teleport to a position they were at previously. This includes: + +* Where the player was before they last used another HuskHomes teleportation command (on by default) +* Where the player was before they last died (off by default, requires permission) +* Where the player was before they last used another plugin's teleport command (off by default) + +## Configuration +Through the config file and permissions, you can choose which last positions to let users return to. + +
+Back Command – config.yml + +```yaml +# Settings for the /back command +back_command: + # Whether /back should work to teleport the user to where they died + return_by_death: true + # Whether /back should work with other plugins that use the PlayerTeleportEvent (can conflict) + save_on_teleport_event: false + # List of world names where the /back command cannot RETURN the player to. + # A user's last position won't be updated if they die or teleport from these worlds, but they still will be able to use the command while IN the world + restricted_worlds: [] +``` +
+ +## Restricting /back +There are several options for restricting the use of `/back`. + +### Letting users return to a previous position +Being able to return to where a user last performed a HuskHomes teleportation command from is the default functionality of `/back`. + +To disable this, give the player the `huskhomes.command.back.previous` permission node, set to the value `false`. + +### Letting users "return by death" +To enable "return by death" – where a user can return to where they most recently died with /back – first enable `return_by_death` under `config.yml` + +
+Return By Death – config.yml + +```yaml +back_command: + # Whether /back should work to teleport the user to where they died + return_by_death: true +``` +
+ +Then, grant the player the `huskhomes.command.back.death` permission node. + +### Disabling using /back to return to certain worlds +Add world names to the `restricted_worlds` list under `back_command` in the config file and the plugin will no longer save last positions within the listed worlds. + +This will stop the plugin from letting users _return_ to certain specific worlds. It will _not_, however, prevent users from using the `/back` command _in_ these worlds. To stop users from using `/back` in specific worlds, use [LuckPerms Contextual Permissions](https://luckperms.net/wiki/Context). + +
+Restricted Worlds – config.yml + +```yaml +back_command: + # List of world names where the /back command cannot RETURN the player to. + # A user's last position won't be updated if they die or teleport from these worlds, but they still will be able to use the command while IN the world + restricted_worlds: [] +``` +
+ +### Returning from other plugins' teleports +> **Warning:** This may cause conflicts with the behaviour of some plugins, please take caution when enabling this if you have lots of plugins installed! + +To use `/back` to return to where other plugins' teleported a user from, enable `save_on_teleport_event`. + +
+Teleport Event – config.yml + +```yaml +back_command: + # Whether /back should work with other plugins that use the PlayerTeleportEvent (can conflict) + save_on_teleport_event: false +``` +
\ No newline at end of file diff --git a/docs/Config-Files.md b/docs/Config-Files.md index ad05ab32..8540d0dc 100644 --- a/docs/Config-Files.md +++ b/docs/Config-Files.md @@ -105,6 +105,9 @@ general: return_by_death: true # Whether /back should work with other plugins that use the PlayerTeleportEvent (can conflict) save_on_teleport_event: false + # List of world names where the /back command cannot RETURN the player to. + # A user's last position won't be updated if they die or teleport from these worlds, but they still will be able to use the command while IN the world + restricted_worlds: [] # Settings for sound effects sound_effects: # Whether to play sound effects diff --git a/docs/Home.md b/docs/Home.md index cb6d6f80..41ea2a48 100644 --- a/docs/Home.md +++ b/docs/Home.md @@ -15,6 +15,7 @@ Welcome to the plugin documentation for HuskHomes v4.x+, the powerful & intuitiv * πŸ—ΊοΈ [[Map Hooks]] * πŸ’΅ [[Economy Hook]] * ⚠️ [[Strict Tpahere]] +* πŸ”™ [[Back Command]] * 🚫 [[Restricted Warps]] * ⏰ [[Cooldowns]] * 🌎 [[Global Spawn]] diff --git a/docs/_Sidebar.md b/docs/_Sidebar.md index 807ce15c..4f64efc7 100644 --- a/docs/_Sidebar.md +++ b/docs/_Sidebar.md @@ -12,6 +12,7 @@ * πŸ—ΊοΈ [[Map Hooks]] * πŸ’΅ [[Economy Hook]] * ⚠️ [[Strict Tpahere]] +* πŸ”™ [[Back Command]] * 🚫 [[Restricted Warps]] * ⏰ [[Cooldowns]] * 🌎 [[Global Spawn]] diff --git a/fabric/src/main/java/net/william278/huskhomes/util/FabricSavePositionProvider.java b/fabric/src/main/java/net/william278/huskhomes/util/FabricSavePositionProvider.java index 97199f94..625ef181 100644 --- a/fabric/src/main/java/net/william278/huskhomes/util/FabricSavePositionProvider.java +++ b/fabric/src/main/java/net/william278/huskhomes/util/FabricSavePositionProvider.java @@ -29,7 +29,6 @@ import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.BlockView; -import net.minecraft.world.World; import net.william278.huskhomes.FabricHuskHomes; import net.william278.huskhomes.position.Location; import org.jetbrains.annotations.NotNull;