Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -51,6 +53,10 @@ public boolean onCommand(@NonNull CommandSender sender, @NonNull Command command
return handleMigrateCommand(player, args);
}

if (args[0].equalsIgnoreCase("random")) {
Comment thread
Redstoner507 marked this conversation as resolved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The random subcommand doesn't show up in auto completion + all the other commands also not. Would probably make sense to fix that here

return handleRandomWarpCommand(player, args);
}

return handleWarpTeleport(player, args);
}

Expand Down Expand Up @@ -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<Warp> 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",
Expand Down Expand Up @@ -152,14 +191,25 @@ private static boolean checkForWarpUsePermissionAndMessage(@NonNull Player playe
@Override
public @Nullable List<String> onTabComplete(@NonNull CommandSender sender, @NonNull Command command, @NonNull String label, String @NonNull [] args) {
if (args.length == 1) {
List<String> 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<String> 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();

Expand Down

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of introducing a subpermission for random warps? and if the user doesn't have the permission the button + subcommand are not shown / executable

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.buildtheearth.buildteamtools.modules.navigation.components.warps.menu;

import jdk.jshell.execution.Util;
Comment thread
Redstoner507 marked this conversation as resolved.
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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -57,6 +66,22 @@ protected void setMenuItemsAsync() {

@Override
protected void setItemClickEventsAsync() {
getMenu().getSlot(RANDOM_ITEM_SLOT).setClickHandler((player, info) -> {
player.closeInventory();

List<Warp> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -43,6 +44,13 @@ public static Object pickRandom(Object[] array) {
return array[(int) (Math.random() * array.length)];
}

public static <T> T pickRandom(List<T> list) {
if (list.isEmpty()) {
return null;
}

return list.get(ThreadLocalRandom.current().nextInt(list.size()));
}

/**
* Converts the given Time to a time string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ public enum HeadTexture {
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNGU5MTIwMGRmMWNhZTUxYWNjMDcxZjg1YzdmN2Y1Yjg0NDlkMzliYjMyZjM2M2IwYWE1MWRiYzg1ZDEzM2UifX19"),

STONE_LETTER_QUESTION_MARK(
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDIzZWFlZmJkNTgxMTU5Mzg0Mjc0Y2RiYmQ1NzZjZWQ4MmViNzI0MjNmMmVhODg3MTI0ZjllZDMzYTY4NzJjIn19fQ==");
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDIzZWFlZmJkNTgxMTU5Mzg0Mjc0Y2RiYmQ1NzZjZWQ4MmViNzI0MjNmMmVhODg3MTI0ZjllZDMzYTY4NzJjIn19fQ=="),

DICE(
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZjU4OTJmYjc1Nzc2NmUzMjhkODlhMmVmODU5MmE5MjQ4ODFiZDA3ZjIzYmRhNGNiYmRjZmY3OWE4ZGJmOTgxNSJ9fX0=");

private final String base64;

Expand Down
Loading