diff --git a/src/main/java/world/bentobox/warps/Warp.java b/src/main/java/world/bentobox/warps/Warp.java index 63a6ed1..e48f2a1 100644 --- a/src/main/java/world/bentobox/warps/Warp.java +++ b/src/main/java/world/bentobox/warps/Warp.java @@ -17,6 +17,7 @@ import world.bentobox.bentobox.api.flags.clicklisteners.CycleClick; import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.util.Util; import world.bentobox.level.Level; +import world.bentobox.warps.commands.ToggleWarpCommand; import world.bentobox.warps.commands.WarpCommand; import world.bentobox.warps.commands.WarpsCommand; import world.bentobox.warps.config.Settings; @@ -100,6 +101,7 @@ public class Warp extends Addon { // Load the master warp and warps command new WarpCommand(this); new WarpsCommand(this); + new ToggleWarpCommand(this); } } @@ -140,6 +142,7 @@ public class Warp extends Addon { new WarpCommand(this, gameModeAddon.getPlayerCommand().get()); new WarpsCommand(this, gameModeAddon.getPlayerCommand().get()); + new ToggleWarpCommand(this, gameModeAddon.getPlayerCommand().get()); this.hooked = true; } }); diff --git a/src/main/java/world/bentobox/warps/commands/ToggleWarpCommand.java b/src/main/java/world/bentobox/warps/commands/ToggleWarpCommand.java new file mode 100644 index 0000000..a4823ad --- /dev/null +++ b/src/main/java/world/bentobox/warps/commands/ToggleWarpCommand.java @@ -0,0 +1,58 @@ +package world.bentobox.warps.commands; + +import org.bukkit.World; +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.warps.Warp; +import world.bentobox.warps.objects.PlayerWarp; + +import java.util.List; +import java.util.UUID; + +public class ToggleWarpCommand extends CompositeCommand { + + private final Warp addon; + + public ToggleWarpCommand(Warp addon, CompositeCommand bsbIslandCmd) { + super(bsbIslandCmd, addon.getSettings().getToggleWarpCommand()); + this.addon = addon; + } + + public ToggleWarpCommand(Warp addon) { + super(addon.getSettings().getToggleWarpCommand()); + this.addon = addon; + } + + + @Override + public void setup() { + this.setPermission(this.getParent() == null ? Warp.WELCOME_WARP_SIGNS + ".togglewarp" : "island.warp.toggle"); + this.setOnlyPlayer(true); + this.setDescription("togglewarp.help.description"); + } + + @Override + public boolean execute(User user, String s, List list) { + UUID userUUID = user.getUniqueId(); + World userWorld = user.getWorld(); + + // Check if the user has a warp + boolean hasWarp = addon.getWarpSignsManager().hasWarp(userWorld, userUUID); + + if (hasWarp) { + // If the user has a warp, toggle its visibility + PlayerWarp warp = addon.getWarpSignsManager().getPlayerWarp(userWorld, userUUID); + // Check extreme case if PlayerWarp is null + if (warp == null) { + user.sendMessage("togglewarp.error.generic"); + return false; + } + warp.toggle(); + String message = warp.isEnabled() ? "togglewarp.enabled" : "togglewarp.disabled"; + user.sendMessage(message); + } else { + user.sendMessage("togglewarp.error.no-warp"); + } + return false; + } +} diff --git a/src/main/java/world/bentobox/warps/commands/WarpCommand.java b/src/main/java/world/bentobox/warps/commands/WarpCommand.java index 53af311..bb4e445 100644 --- a/src/main/java/world/bentobox/warps/commands/WarpCommand.java +++ b/src/main/java/world/bentobox/warps/commands/WarpCommand.java @@ -50,12 +50,12 @@ public class WarpCommand extends DelayedTeleportCommand { user.sendMessage("warps.warpTip", "[text]", addon.getSettings().getWelcomeLine()); return false; } else { - // Attemp to find warp with exact player's name + // Attempt to find warp with exact player's name UUID foundWarp = warpList.stream().filter(u -> getPlayers().getName(u).equalsIgnoreCase(args.get(0))).findFirst().orElse(null); if (foundWarp == null) { - // Atempt to find warp which starts with the given name + // Attempt to find warp which starts with the given name UUID foundAlernativeWarp = warpList.stream().filter(u -> getPlayers().getName(u).toLowerCase().startsWith(args.get(0).toLowerCase())).findFirst().orElse(null); if (foundAlernativeWarp == null) { diff --git a/src/main/java/world/bentobox/warps/config/Settings.java b/src/main/java/world/bentobox/warps/config/Settings.java index 883687b..85f90ae 100644 --- a/src/main/java/world/bentobox/warps/config/Settings.java +++ b/src/main/java/world/bentobox/warps/config/Settings.java @@ -61,6 +61,8 @@ public class Settings implements ConfigObject String warpCommand = "warp"; @ConfigEntry(path = "warps-command") String warpsCommand = "warps"; + @ConfigEntry(path = "togglewarp-command") + String toggleWarpCommand = "togglewarp"; // --------------------------------------------------------------------- // Section: Constructor @@ -205,6 +207,21 @@ public class Settings implements ConfigObject this.warpsCommand = warpsCommand; } + + /** + * @return the toggleWarpCommand + */ + public String getToggleWarpCommand() { + return "togglewarp"; + } + + /** + * @param toggleWarpCommand the toggleWarpCommand to set + */ + public void setToggleWarpCommand(String toggleWarpCommand) { + this.toggleWarpCommand = toggleWarpCommand; + } + /** * @return the removeExistingWarpsWhenFlagChanges */ diff --git a/src/main/java/world/bentobox/warps/managers/WarpSignsManager.java b/src/main/java/world/bentobox/warps/managers/WarpSignsManager.java index 3ae6565..18a7773 100644 --- a/src/main/java/world/bentobox/warps/managers/WarpSignsManager.java +++ b/src/main/java/world/bentobox/warps/managers/WarpSignsManager.java @@ -169,6 +169,10 @@ public class WarpSignsManager { // Bigger value of time means a more recent login TreeMap map = new TreeMap<>(); getWarpMap(world).forEach((uuid, value) -> { + // If the warp is not enabled, skip this iteration + if (!value.isEnabled()) { + return; + } // If never played, will be zero long lastPlayed = addon.getServer().getOfflinePlayer(uuid).getLastPlayed(); // This aims to avoid the chance that players logged off at exactly the same time @@ -197,7 +201,11 @@ public class WarpSignsManager { public Set listWarps(@NonNull World world) { // Remove any null locations getWarpMap(world).values().removeIf(Objects::isNull); - return getWarpMap(world).entrySet().stream().filter(e -> Util.sameWorld(world, Objects.requireNonNull(e.getValue().getLocation().getWorld()))).map(Map.Entry::getKey).collect(Collectors.toSet()); + // Remove any warps that have not been toggled on + Map enabledWarps = getWarpMap(world).entrySet().stream() + .filter(entry -> entry.getValue().isEnabled()) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + return enabledWarps.keySet(); } /** diff --git a/src/main/resources/addon.yml b/src/main/resources/addon.yml index a75733f..b8de846 100755 --- a/src/main/resources/addon.yml +++ b/src/main/resources/addon.yml @@ -18,3 +18,6 @@ permissions: '[gamemode].island.addwarp': description: Player can create a welcome warp sign default: true + '[gamemode].island.togglewarp': + description: Player can toggle a warp sign + default: true diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1975fd0..42c4d4a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -33,3 +33,4 @@ allow-in-other-worlds: false # Warp and warps commands. You can change them if they clash with other addons or plugins. warp-command: warp warps-command: warps +togglewarp-command: togglewarp diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 7198653..c48efee 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -60,6 +60,16 @@ warps: # Prefix for messages that are send from server. prefix: "&l&6 [BentoBox]: &r" +togglewarp: + help: + description: "toggle the warp sign" + enabled: "&a Your warp is now visible!" + disabled: "&c Your warp is now hidden!" + error: + no-permission: "&c You do not have permission to do that!" + generic: "&c An error occurred while toggling your warp." + no-warp: "&c You do not have a warp to toggle!" + protection: flags: PLACE_WARP: