1
0
mirror of https://github.com/BentoBoxWorld/Warps.git synced 2024-11-24 19:46:28 +01:00

feat: toggle warp command

This commit is contained in:
TreemanK 2024-06-21 20:19:24 +10:00
parent ef81a1c2f0
commit eeead7fb49
8 changed files with 103 additions and 3 deletions

View File

@ -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;
}
});

View File

@ -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<String> 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;
}
}

View File

@ -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) {

View File

@ -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
*/

View File

@ -169,6 +169,10 @@ public class WarpSignsManager {
// Bigger value of time means a more recent login
TreeMap<Long, UUID> 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<UUID> 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<UUID, PlayerWarp> enabledWarps = getWarpMap(world).entrySet().stream()
.filter(entry -> entry.getValue().isEnabled())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return enabledWarps.keySet();
}
/**

View File

@ -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

View File

@ -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

View File

@ -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: