feat: add restricted worlds for /back

also adds docs for this
This commit is contained in:
William278 committed 2025-02-26 23:45:25 +00:00
1 parent 0e975232c0
commit f7528ef95d
10 files changed
+149 -48

No files matched your search

@@ -124,11 +124,11 @@ public final class Settings {
private int maxHomes = 10; private int maxHomes = 10;
@Comment("The maximum public homes a user can create. " @Comment("The maximum public homes a user can create. "
+ "Override with the huskhomes.max_public_homes.<number> permission.") + "Override with the huskhomes.max_public_homes.<number> permission.")
private int maxPublicHomes = 10; private int maxPublicHomes = 10;
@Comment("Whether permission limits (i.e. huskhomes.max_homes.<number>) should stack " @Comment("Whether permission limits (i.e. huskhomes.max_homes.<number>) should stack "
+ "if the user inherits multiple nodes.") + "if the user inherits multiple nodes.")
private boolean stackPermissionLimits = false; private boolean stackPermissionLimits = false;
@Comment("Whether users require a permission (huskhomes.warp.<warp_name>) to use warps") @Comment("Whether users require a permission (huskhomes.warp.<warp_name>) to use warps")
@@ -153,14 +153,14 @@ public final class Settings {
private int teleportRequestExpiryTime = 60; private int teleportRequestExpiryTime = 60;
@Comment("Whether /tpahere should use the location of the sender when sent. " @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; private boolean strictTpaHereRequests = true;
@Comment("How many items should be displayed per-page in chat menu lists") @Comment("How many items should be displayed per-page in chat menu lists")
private int listItemsPerPage = 12; private int listItemsPerPage = 12;
@Comment("Whether the user should always be put back at the /spawn point when they die " @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; private boolean alwaysRespawnAtSpawn = false;
@Comment("Whether teleportation should be carried out async (ensuring chunks load before teleporting)") @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; private boolean caseInsensitive = false;
@Comment("Whether home and warp names should be restricted to a regex filter." @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; private boolean restrict = true;
@Comment("Regex which home and warp names must match. Names have a max length of 16 characters") @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) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class DescriptionSettings { public static class DescriptionSettings {
@Comment("Whether home/warp descriptions should be restricted to a regex filter. " @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; private boolean restrict = false;
@Comment("Regex which home and warp descriptions must match. " @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"; 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)") @Comment("Whether /back should work with other plugins that use the PlayerTeleportEvent (can conflict)")
private boolean saveOnTeleportEvent = false; 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<String> 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") @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. " @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(); private GlobalSpawnSettings globalSpawn = new GlobalSpawnSettings();
@Getter @Getter
@@ -300,7 +313,7 @@ public final class Settings {
} }
@Comment("Whether player respawn positions should work cross-server. " @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; private boolean globalRespawning = false;
} }
@@ -428,7 +441,7 @@ public final class Settings {
.anyMatch(disabled -> { .anyMatch(disabled -> {
final String command = (disabled.startsWith("/") ? disabled.substring(1) : disabled); final String command = (disabled.startsWith("/") ? disabled.substring(1) : disabled);
return command.equalsIgnoreCase(type.getName()) return command.equalsIgnoreCase(type.getName())
|| type.getAliases().stream().anyMatch(alias -> alias.equalsIgnoreCase(command)); || type.getAliases().stream().anyMatch(alias -> alias.equalsIgnoreCase(command));
}); });
} }
@@ -20,7 +20,6 @@
package net.william278.huskhomes.hook; package net.william278.huskhomes.hook;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.importer.Importer; import net.william278.huskhomes.importer.Importer;
@@ -24,17 +24,18 @@ import lombok.AccessLevel;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.command.BackCommand; import net.william278.huskhomes.command.BackCommand;
import net.william278.huskhomes.command.Command;
import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.network.Broker; import net.william278.huskhomes.network.Broker;
import net.william278.huskhomes.network.Message; import net.william278.huskhomes.network.Message;
import net.william278.huskhomes.network.Payload; import net.william278.huskhomes.network.Payload;
import net.william278.huskhomes.position.Position; import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.World;
import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TeleportBuilder; import net.william278.huskhomes.teleport.TeleportBuilder;
import net.william278.huskhomes.teleport.TeleportationException; import net.william278.huskhomes.teleport.TeleportationException;
import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.User; import net.william278.huskhomes.user.User;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
@@ -68,8 +69,8 @@ public abstract class EventListener {
// Synchronize the global player list // Synchronize the global player list
plugin.runSyncDelayed(() -> this.updateUserList( plugin.runSyncDelayed(() -> this.updateUserList(
onlineUser, onlineUser,
plugin.getOnlineUsers().stream().map(u -> (User) u).toList()), plugin.getOnlineUsers().stream().map(u -> (User) u).toList()),
onlineUser, onlineUser,
40L 40L
); );
@@ -108,7 +109,7 @@ public abstract class EventListener {
online.removeInvulnerabilityIfPermitted(); online.removeInvulnerabilityIfPermitted();
plugin.runAsync(() -> { plugin.runAsync(() -> {
// Set offline position // Set offline position
plugin.getDatabase().setOfflinePosition(online, online.getPosition()); plugin.getDatabase().setOfflinePosition(online, online.getPosition());
// Remove this user's home cache // Remove this user's home cache
@@ -166,7 +167,7 @@ public abstract class EventListener {
if (bedPosition.isEmpty()) { if (bedPosition.isEmpty()) {
plugin.getSpawn().ifPresent(spawn -> { plugin.getSpawn().ifPresent(spawn -> {
if (plugin.getSettings().getCrossServer().isEnabled() if (plugin.getSettings().getCrossServer().isEnabled()
&& !spawn.getServer().equals(plugin.getServerName())) { && !spawn.getServer().equals(plugin.getServerName())) {
plugin.runSyncDelayed(() -> Teleport.builder(plugin) plugin.runSyncDelayed(() -> Teleport.builder(plugin)
.teleporter(teleporter) .teleporter(teleporter)
.target(spawn) .target(spawn)
@@ -220,11 +221,24 @@ public abstract class EventListener {
*/ */
protected final void handlePlayerDeath(@NotNull OnlineUser onlineUser) { protected final void handlePlayerDeath(@NotNull OnlineUser onlineUser) {
final Position lastPosition = onlineUser.getPosition(); 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(() -> { plugin.runAsync(() -> {
if (plugin.getSettings().getGeneral().getBackCommand().isReturnByDeath() && plugin.getCommand(BackCommand.class) plugin.getDatabase().setLastPosition(onlineUser, lastPosition);
.map(Command::getPermission).map(onlineUser::hasPermission).orElse(false)) { plugin.getLocales().getLocale("return_by_death_notification")
plugin.getDatabase().setLastPosition(onlineUser, lastPosition); .ifPresent(onlineUser::sendMessage);
}
}); });
} }
@@ -234,26 +248,15 @@ public abstract class EventListener {
* @param onlineUser the respawning {@link OnlineUser} * @param onlineUser the respawning {@link OnlineUser}
*/ */
protected final void handlePlayerRespawn(@NotNull OnlineUser onlineUser) { protected final void handlePlayerRespawn(@NotNull OnlineUser onlineUser) {
plugin.runAsync(() -> { final Settings.CrossServerSettings crossServer = plugin.getSettings().getCrossServer();
// Display the return by death via /back notification if (!crossServer.isEnabled() || !crossServer.isGlobalRespawning()) {
final boolean canReturnByDeath = plugin.getCommand(BackCommand.class) return;
.map(command -> onlineUser.hasPermission(command.getPermission()) }
&& onlineUser.hasPermission(command.getPermission("death"))) plugin.runAsync(() -> this.respawnGlobally(onlineUser));
.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);
}
});
} }
// Respawn a player to where they should be // Respawn a player to where they should be
@Blocking
private void respawnGlobally(@NotNull OnlineUser onlineUser) { private void respawnGlobally(@NotNull OnlineUser onlineUser) {
final TeleportBuilder builder = Teleport.builder(plugin) final TeleportBuilder builder = Teleport.builder(plugin)
.teleporter(onlineUser) .teleporter(onlineUser)
@@ -289,12 +292,13 @@ public abstract class EventListener {
* @param sourcePosition the source {@link Position} they came from * @param sourcePosition the source {@link Position} they came from
*/ */
protected final void handlePlayerTeleport(@NotNull OnlineUser onlineUser, @NotNull Position sourcePosition) { 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; return;
} }
plugin.runAsync(() -> plugin.getDatabase().getUser(onlineUser.getUuid()) // Set the player's last position
.ifPresent(data -> plugin.getDatabase().setLastPosition(data.getUser(), sourcePosition))); 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) { protected final void handlePlayerUpdateSpawnPoint(@NotNull OnlineUser onlineUser, @NotNull Position position) {
if (!plugin.getSettings().getGeneral().isAlwaysRespawnAtSpawn() if (!plugin.getSettings().getGeneral().isAlwaysRespawnAtSpawn()
&& plugin.getSettings().getCrossServer().isEnabled() && plugin.getSettings().getCrossServer().isEnabled()
&& plugin.getSettings().getCrossServer().isGlobalRespawning()) { && plugin.getSettings().getCrossServer().isGlobalRespawning()) {
plugin.getDatabase().setRespawnPosition(onlineUser, position); plugin.getDatabase().setRespawnPosition(onlineUser, position);
} }
} }
@@ -20,7 +20,6 @@
package net.william278.huskhomes.position; package net.william278.huskhomes.position;
import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.OnlineUser;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.UUID; import java.util.UUID;
@@ -67,7 +67,7 @@ public class Teleport implements Completable {
this.async = plugin.getSettings().getGeneral().isTeleportAsync(); this.async = plugin.getSettings().getGeneral().isTeleportAsync();
this.updateLastPosition = updateLastPosition && plugin.getCommand(BackCommand.class) this.updateLastPosition = updateLastPosition && plugin.getCommand(BackCommand.class)
.map(command -> executor.hasPermission(command.getPermission()) .map(command -> executor.hasPermission(command.getPermission())
&& executor.hasPermission(command.getPermission("previous"))) && executor.hasPermission(command.getPermission("previous")))
.orElse(false); .orElse(false);
} }
@@ -97,7 +97,7 @@ public class Teleport implements Completable {
if (localTarget.isPresent()) { if (localTarget.isPresent()) {
fireEvent((event) -> { fireEvent((event) -> {
performTransactions(); performTransactions();
if (updateLastPosition) { if (updateLastPosition && canReturnToWorld(teleporter)) {
plugin.getDatabase().setLastPosition(teleporter, teleporter.getPosition()); plugin.getDatabase().setLastPosition(teleporter, teleporter.getPosition());
} }
teleporter.teleportLocally(localTarget.get().getPosition(), async); teleporter.teleportLocally(localTarget.get().getPosition(), async);
@@ -123,13 +123,13 @@ public class Teleport implements Completable {
fireEvent((event) -> { fireEvent((event) -> {
performTransactions(); performTransactions();
if (updateLastPosition) { if (updateLastPosition && canReturnToWorld(teleporter)) {
plugin.getDatabase().setLastPosition(teleporter, teleporter.getPosition()); plugin.getDatabase().setLastPosition(teleporter, teleporter.getPosition());
} }
final Position target = (Position) this.target; final Position target = (Position) this.target;
if (!plugin.getSettings().getCrossServer().isEnabled() if (!plugin.getSettings().getCrossServer().isEnabled()
|| target.getServer().equals(plugin.getServerName())) { || target.getServer().equals(plugin.getServerName())) {
teleporter.teleportLocally(target, async); teleporter.teleportLocally(target, async);
this.displayTeleportingComplete(teleporter); this.displayTeleportingComplete(teleporter);
teleporter.handleInvulnerability(); 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 @NotNull
private Optional<OnlineUser> resolveLocalTeleporter() throws TeleportationException { private Optional<OnlineUser> resolveLocalTeleporter() throws TeleportationException {
if (this.teleporter instanceof Username username) { if (this.teleporter instanceof Username username) {
+78
View File
@@ -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.
<details>
<summary>Back Command &ndash; config.yml</summary>
```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: []
```
</details>
## 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" &ndash; where a user can return to where they most recently died with /back &ndash; first enable `return_by_death` under `config.yml`
<details>
<summary>Return By Death &ndash; config.yml</summary>
```yaml
back_command:
# Whether /back should work to teleport the user to where they died
return_by_death: true
```
</details>
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).
<details>
<summary>Restricted Worlds &ndash; config.yml</summary>
```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: []
```
</details>
### 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`.
<details>
<summary>Teleport Event &ndash; config.yml</summary>
```yaml
back_command:
# Whether /back should work with other plugins that use the PlayerTeleportEvent (can conflict)
save_on_teleport_event: false
```
</details>
+3
View File
@@ -105,6 +105,9 @@ general:
return_by_death: true return_by_death: true
# Whether /back should work with other plugins that use the PlayerTeleportEvent (can conflict) # Whether /back should work with other plugins that use the PlayerTeleportEvent (can conflict)
save_on_teleport_event: false 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 # Settings for sound effects
sound_effects: sound_effects:
# Whether to play sound effects # Whether to play sound effects
+1
View File
@@ -15,6 +15,7 @@ Welcome to the plugin documentation for HuskHomes v4.x+, the powerful & intuitiv
* 🗺️ [[Map Hooks]] * 🗺️ [[Map Hooks]]
* 💵 [[Economy Hook]] * 💵 [[Economy Hook]]
* ⚠️ [[Strict Tpahere]] * ⚠️ [[Strict Tpahere]]
* 🔙 [[Back Command]]
* 🚫 [[Restricted Warps]] * 🚫 [[Restricted Warps]]
* ⏰ [[Cooldowns]] * ⏰ [[Cooldowns]]
* 🌎 [[Global Spawn]] * 🌎 [[Global Spawn]]
+1
View File
@@ -12,6 +12,7 @@
* 🗺️ [[Map Hooks]] * 🗺️ [[Map Hooks]]
* 💵 [[Economy Hook]] * 💵 [[Economy Hook]]
* ⚠️ [[Strict Tpahere]] * ⚠️ [[Strict Tpahere]]
* 🔙 [[Back Command]]
* 🚫 [[Restricted Warps]] * 🚫 [[Restricted Warps]]
* ⏰ [[Cooldowns]] * ⏰ [[Cooldowns]]
* 🌎 [[Global Spawn]] * 🌎 [[Global Spawn]]
@@ -29,7 +29,6 @@ import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView; import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.william278.huskhomes.FabricHuskHomes; import net.william278.huskhomes.FabricHuskHomes;
import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Location;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;