diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/commands/WarpCommand.java b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/commands/WarpCommand.java index 6cc63881..1ed56f93 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/commands/WarpCommand.java +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/commands/WarpCommand.java @@ -10,6 +10,7 @@ import net.buildtheearth.buildteamtools.modules.network.NetworkModule; import net.buildtheearth.buildteamtools.modules.network.model.BuildTeam; import net.buildtheearth.buildteamtools.modules.network.model.Permissions; +import net.buildtheearth.buildteamtools.utils.Utils; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -18,6 +19,7 @@ import org.jetbrains.annotations.Nullable; import org.jspecify.annotations.NonNull; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -51,6 +53,10 @@ public boolean onCommand(@NonNull CommandSender sender, @NonNull Command command return handleMigrateCommand(player, args); } + if (args[0].equalsIgnoreCase("random")) { + return handleRandomWarpCommand(player, args); + } + return handleWarpTeleport(player, args); } @@ -100,6 +106,39 @@ private boolean handleMigrateCommand(@NonNull Player player, String @NonNull [] return true; } + private boolean handleRandomWarpCommand(@NonNull Player player, String @NonNull [] args) { + + if (!player.hasPermission(Permissions.WARP_RANDOM)) { + player.sendMessage(ChatHelper.getErrorString("You don't have the required %s to %s warps.", "permission", + "random")); + return true; + } + + if (args.length > 1) { + player.sendMessage(ChatHelper.getErrorComponent("Usage: /warp random")); + return true; + } + + BuildTeam buildTeam = NetworkModule.getInstance().getBuildTeam(); + if (buildTeam == null) { + return true; + } + + List warps = buildTeam.getWarpGroups().stream() + .flatMap(group -> group.getWarps().stream()) + .toList(); + + Warp warp = Utils.pickRandom(warps); + if (warp == null) { + player.sendMessage("No warp found"); + return true; + } + + NavigationModule.getInstance().getWarpsComponent().warpPlayer(player, warp); + + return true; + } + private void handleMigrationResult(@NonNull Player player, @NonNull MigrationResult result, @Nullable Throwable throwable) { if (throwable != null) { player.sendMessage(ChatHelper.getErrorComponent("Something went wrong while migrating the warps: %s", @@ -152,14 +191,25 @@ private static boolean checkForWarpUsePermissionAndMessage(@NonNull Player playe @Override public @Nullable List onTabComplete(@NonNull CommandSender sender, @NonNull Command command, @NonNull String label, String @NonNull [] args) { if (args.length == 1) { + List list = new ArrayList<>(); + if (sender.hasPermission(Permissions.WARP_CREATE)) list.add("create"); + if (sender.hasPermission(Permissions.WARP_MIGRATE)) list.add("migrate"); + if (sender.hasPermission(Permissions.WARP_RANDOM)) list.add("random"); + String partial = args[0].toLowerCase(); BuildTeam buildTeam = NetworkModule.getInstance().getBuildTeam(); if (buildTeam != null && buildTeam.getWarpGroups() != null) { - return buildTeam.getWarpGroups().stream() + List warps = buildTeam.getWarpGroups().stream() .flatMap(gr -> gr.getWarps().stream().map(Warp::getName)) .filter(s -> s.toLowerCase().startsWith(partial)) .toList(); + + list.addAll(warps); } + + return list.stream() + .filter(s -> s.toLowerCase().startsWith(partial)) + .toList(); } return Collections.emptyList(); diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/menu/WarpMenu.java b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/menu/WarpMenu.java index 0713783b..1923383f 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/menu/WarpMenu.java +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/menu/WarpMenu.java @@ -1,5 +1,6 @@ package net.buildtheearth.buildteamtools.modules.navigation.components.warps.menu; +import jdk.jshell.execution.Util; import net.buildtheearth.buildteamtools.modules.navigation.NavigationModule; import net.buildtheearth.buildteamtools.modules.navigation.components.warps.WarpsComponent; import net.buildtheearth.buildteamtools.modules.navigation.components.warps.model.Warp; @@ -9,6 +10,7 @@ import net.buildtheearth.buildteamtools.modules.network.model.Permissions; import net.buildtheearth.buildteamtools.utils.ListUtil; import net.buildtheearth.buildteamtools.utils.MenuItems; +import net.buildtheearth.buildteamtools.utils.Utils; import net.buildtheearth.buildteamtools.utils.heads.HeadFactory; import net.buildtheearth.buildteamtools.utils.heads.HeadTexture; import net.buildtheearth.buildteamtools.utils.menus.AbstractMenu; @@ -27,6 +29,7 @@ public class WarpMenu extends AbstractPaginatedMenu { public static final int BACK_ITEM_SLOT = 27; public static final int SWITCH_PAGE_ITEM_SLOT = 34; + public static final int RANDOM_ITEM_SLOT = 35; private final WarpGroup warpGroup; private final AbstractMenu backMenu; @@ -47,6 +50,12 @@ public WarpMenu(Player menuPlayer, WarpGroup warpGroup, @Nullable AbstractMenu b @Override protected void setMenuItemsAsync() { + if (getMenuPlayer().hasPermission(Permissions.WARP_RANDOM)) { + getMenu().getSlot(RANDOM_ITEM_SLOT).setItem( + HeadFactory.head(HeadTexture.DICE, "§a§lTeleport to a random warp", ListUtil.createList("§8Click here to be teleported to a random warp.")) + ); + } + if (backMenu != null) setBackItem(BACK_ITEM_SLOT, backMenu); @@ -57,6 +66,22 @@ protected void setMenuItemsAsync() { @Override protected void setItemClickEventsAsync() { + getMenu().getSlot(RANDOM_ITEM_SLOT).setClickHandler((player, info) -> { + player.closeInventory(); + + List warps = warpGroup.getWarps(); + + if (warps.isEmpty()) { + player.sendMessage("§cNo warp available."); + return; + } + + Warp warp = Utils.pickRandom(warps); + + if (warp != null) { + NavigationModule.getInstance().getWarpsComponent().warpPlayer(player, warp); + } + }); if (getSource().size() > 27) setSwitchPageItemClickEvents(SWITCH_PAGE_ITEM_SLOT); } diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/Permissions.java b/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/Permissions.java index 78d2b3e7..4d07d73d 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/Permissions.java +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/Permissions.java @@ -28,6 +28,7 @@ public class Permissions { public static final String WARP_EDIT = "btt.warp.edit"; public static final String WARP_DELETE = "btt.warp.delete"; public static final String WARP_MIGRATE = "btt.warp.migrate"; + public static final String WARP_RANDOM = "btt.warp.random"; public static final String WARP_GROUP_CREATE = "btt.warp.group.create"; public static final String WARP_GROUP_EDIT = "btt.warp.group.edit"; diff --git a/src/main/java/net/buildtheearth/buildteamtools/utils/Utils.java b/src/main/java/net/buildtheearth/buildteamtools/utils/Utils.java index 160d4a91..8da47c61 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/utils/Utils.java +++ b/src/main/java/net/buildtheearth/buildteamtools/utils/Utils.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; public class Utils { @@ -43,6 +44,13 @@ public static Object pickRandom(Object[] array) { return array[(int) (Math.random() * array.length)]; } + public static T pickRandom(List list) { + if (list.isEmpty()) { + return null; + } + + return list.get(ThreadLocalRandom.current().nextInt(list.size())); + } /** * Converts the given Time to a time string diff --git a/src/main/java/net/buildtheearth/buildteamtools/utils/heads/HeadTexture.java b/src/main/java/net/buildtheearth/buildteamtools/utils/heads/HeadTexture.java index 409b4231..05fadede 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/utils/heads/HeadTexture.java +++ b/src/main/java/net/buildtheearth/buildteamtools/utils/heads/HeadTexture.java @@ -225,7 +225,10 @@ public enum HeadTexture { "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNGU5MTIwMGRmMWNhZTUxYWNjMDcxZjg1YzdmN2Y1Yjg0NDlkMzliYjMyZjM2M2IwYWE1MWRiYzg1ZDEzM2UifX19"), STONE_LETTER_QUESTION_MARK( - "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDIzZWFlZmJkNTgxMTU5Mzg0Mjc0Y2RiYmQ1NzZjZWQ4MmViNzI0MjNmMmVhODg3MTI0ZjllZDMzYTY4NzJjIn19fQ=="); + "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDIzZWFlZmJkNTgxMTU5Mzg0Mjc0Y2RiYmQ1NzZjZWQ4MmViNzI0MjNmMmVhODg3MTI0ZjllZDMzYTY4NzJjIn19fQ=="), + + DICE( + "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZjU4OTJmYjc1Nzc2NmUzMjhkODlhMmVmODU5MmE5MjQ4ODFiZDA3ZjIzYmRhNGNiYmRjZmY3OWE4ZGJmOTgxNSJ9fX0="); private final String base64;