Repository navigation
feat(navigation): add direct navigator submenu commands #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,162 @@ | ||
| package net.buildtheearth.buildteamtools.modules.navigation.components.navigator.commands; | ||
|
|
||
| import com.alpsbte.alpslib.utils.ChatHelper; | ||
| import net.buildtheearth.buildteamtools.modules.navigation.NavigationModule; | ||
| import io.papermc.paper.command.brigadier.CommandSourceStack; | ||
| import net.buildtheearth.buildteamtools.BuildTeamTools; | ||
| import net.buildtheearth.buildteamtools.modules.Module; | ||
| import net.buildtheearth.buildteamtools.modules.common.commands.BttCommandManager; | ||
| import net.buildtheearth.buildteamtools.modules.navigation.NavigationModule; | ||
| import net.buildtheearth.buildteamtools.modules.navigation.menu.CitySelectorMenu; | ||
| import net.buildtheearth.buildteamtools.modules.navigation.menu.CountrySelectorMenu; | ||
| import net.buildtheearth.buildteamtools.modules.navigation.menu.ExploreMenu; | ||
| import net.buildtheearth.buildteamtools.modules.navigation.menu.MainMenu; | ||
| import net.buildtheearth.buildteamtools.modules.navigation.menu.StateSelectorMenu; | ||
| import net.buildtheearth.buildteamtools.modules.network.model.Continent; | ||
| import net.buildtheearth.buildteamtools.modules.network.model.Permissions; | ||
| import net.buildtheearth.buildteamtools.modules.network.model.Region; | ||
| import net.buildtheearth.buildteamtools.utils.io.NavigationConfig; | ||
| import net.kyori.adventure.text.Component; | ||
| import org.bukkit.command.CommandSender; | ||
| import org.bukkit.entity.Player; | ||
| import org.incendo.cloud.Command; | ||
| import org.incendo.cloud.context.CommandContext; | ||
| import org.incendo.cloud.minecraft.extras.RichDescription; | ||
| import org.incendo.cloud.parser.standard.StringParser; | ||
| import org.incendo.cloud.suggestion.SuggestionProvider; | ||
| import org.jspecify.annotations.NonNull; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.UnaryOperator; | ||
|
|
||
| public class NavigatorCommand { | ||
| private static final String CONTINENT_ARGUMENT = "continent"; | ||
| private static final String STATE_ARGUMENT = "state"; | ||
| private static final String PLAYER_ONLY_MESSAGE = "You must be a %s to %s this command!"; | ||
|
|
||
| public void register(Module owner) { | ||
| var commandManager = BuildTeamTools.getInstance().getCommandManager(); | ||
| commandManager.registerNoArguments(owner, "navigator", Permissions.NAVIGATOR_USE, this::execute); | ||
| commandManager.registerSubcommand(owner, "navigator", "toggle", | ||
| "Toggle the navigator item on or off.", | ||
| builder -> builder.permission(Permissions.NAVIGATOR_USE), | ||
| context -> toggle(context.sender().getSender())); | ||
| commandManager.registerNoArguments(owner, "navigator", Permissions.NAVIGATOR_USE, this::openNavigator); | ||
| registerSubcommand(owner, "explore", "Open the continent selection menu.", | ||
| context -> openPlayer(context.sender().getSender(), player -> new ExploreMenu(player, true))); | ||
| registerSubcommand(owner, "countries", "Open the country selection menu for a continent.", | ||
| builder -> builder | ||
| .permission(Permissions.NAVIGATOR_USE) | ||
| .required(CONTINENT_ARGUMENT, StringParser.greedyStringParser(), | ||
| RichDescription.of(Component.text("Continent name")), | ||
| SuggestionProvider.blockingStrings((context, input) -> | ||
| BttCommandManager.matchingSuggestions(continentNames(), input.remainingInput()))), | ||
| context -> { | ||
| CommandSender sender = context.sender().getSender(); | ||
| Continent continent = parseContinent(context.get(CONTINENT_ARGUMENT)); | ||
| if (continent == null) { | ||
| sender.sendMessage(ChatHelper.getErrorString("Unknown continent. Available continents: %s", | ||
| String.join(", ", continentNames()))); | ||
| return; | ||
| } | ||
| openPlayer(sender, player -> new CountrySelectorMenu(player, continent, true)); | ||
| }); | ||
| registerSubcommand(owner, "states", "Open the USA state selection menu.", | ||
| context -> openPlayer(context.sender().getSender(), | ||
| player -> new StateSelectorMenu(player, Continent.NORTH_AMERICA, true))); | ||
| registerSubcommand(owner, "cities", "Open the city selection menu for a supported state.", | ||
| builder -> builder | ||
| .permission(Permissions.NAVIGATOR_USE) | ||
| .required(STATE_ARGUMENT, StringParser.greedyStringParser(), | ||
| RichDescription.of(Component.text("State name")), | ||
| SuggestionProvider.blockingStrings((context, input) -> | ||
| BttCommandManager.matchingSuggestions(cityStateNames(), input.remainingInput()))), | ||
| context -> { | ||
| CommandSender sender = context.sender().getSender(); | ||
| String stateName = context.<String>get(STATE_ARGUMENT).replace('_', ' '); | ||
| Region state = StateSelectorMenu.getCityStates().stream() | ||
| .filter(region -> region.getName().equalsIgnoreCase(stateName)) | ||
| .findFirst().orElse(null); | ||
| if (state == null) { | ||
| sender.sendMessage(ChatHelper.getErrorString( | ||
| "No city menu is available for that state. Available states: %s", | ||
| String.join(", ", cityStateNames()))); | ||
| return; | ||
| } | ||
| openPlayer(sender, player -> new CitySelectorMenu( | ||
| state, StateSelectorMenu.getCityTeamsForState(state), player, true)); | ||
| }); | ||
| // The module constructor registers commands before its own config field is set | ||
| NavigationConfig.MainMenuItems items = BuildTeamTools.getInstance().getNavigationConfig().mainMenuItems(); | ||
| if (items.buildItem().enabled()) { | ||
| registerSubcommand(owner, "build", "Run the configured Build menu action.", | ||
| context -> openPlayer(context.sender().getSender(), MainMenu::openBuild)); | ||
| } | ||
| if (items.plotsystemItem().enabled()) { | ||
| registerSubcommand(owner, "plotsystem", "Run the configured Plot System menu action.", | ||
| context -> openPlayer(context.sender().getSender(), MainMenu::openPlotSystem)); | ||
| } | ||
| if (items.tutorialItem().enabled()) { | ||
| registerSubcommand(owner, "tutorials", "Open the tutorial menu or run its configured action.", | ||
| context -> openPlayer(context.sender().getSender(), MainMenu::openTutorials)); | ||
| } | ||
| registerSubcommand(owner, "toggle", "Toggle the navigator hotbar item.", | ||
| context -> openPlayer(context.sender().getSender(), | ||
| NavigationModule.getInstance().getNavigatorComponent()::toggle)); | ||
| } | ||
|
|
||
| private void execute(@NonNull CommandSender sender) { | ||
| if (!(sender instanceof Player player)) { | ||
| sender.sendMessage(ChatHelper.getErrorString("You must be a %s to %s this command!", "player", "execute")); | ||
| return; | ||
| } | ||
| private void registerSubcommand( | ||
| Module owner, | ||
| String name, | ||
| String description, | ||
| Consumer<CommandContext<CommandSourceStack>> handler | ||
| ) { | ||
| registerSubcommand(owner, name, description, | ||
| builder -> builder.permission(Permissions.NAVIGATOR_USE), handler); | ||
| } | ||
|
|
||
| if (!player.hasPermission(Permissions.NAVIGATOR_USE)) { | ||
| player.sendMessage(ChatHelper.getErrorString("You don't have permission to use this command!")); | ||
| return; | ||
| } | ||
| private void registerSubcommand( | ||
| Module owner, | ||
| String name, | ||
| String description, | ||
| UnaryOperator<Command.Builder<CommandSourceStack>> argumentBuilder, | ||
| Consumer<CommandContext<CommandSourceStack>> handler | ||
| ) { | ||
| BuildTeamTools.getInstance().getCommandManager().registerSubcommand( | ||
| owner, "navigator", name, description, | ||
| argumentBuilder, | ||
| handler); | ||
| } | ||
|
|
||
| new MainMenu(player); | ||
| private void openNavigator(@NonNull CommandSender sender) { | ||
| openPlayer(sender, MainMenu::new); | ||
| } | ||
|
|
||
| private void toggle(@NonNull CommandSender sender) { | ||
| private void openPlayer(@NonNull CommandSender sender, Consumer<Player> action) { | ||
| if (!(sender instanceof Player player)) { | ||
| sender.sendMessage(ChatHelper.getErrorString("You must be a %s to %s this command!", "player", "execute")); | ||
| return; | ||
| } | ||
| if (!player.hasPermission(Permissions.NAVIGATOR_USE)) { | ||
| player.sendMessage(ChatHelper.getErrorString("You don't have permission to use this command!")); | ||
| sendPlayerOnlyMessage(sender); | ||
| return; | ||
| } | ||
| NavigationModule.getInstance().getNavigatorComponent().toggle(player); | ||
| action.accept(player); | ||
| } | ||
|
|
||
| private void sendPlayerOnlyMessage(@NonNull CommandSender sender) { | ||
| sender.sendMessage(ChatHelper.getErrorString(PLAYER_ONLY_MESSAGE, "player", "execute")); | ||
| } | ||
|
|
||
| private static List<String> continentNames() { | ||
| return Arrays.stream(Continent.values()) | ||
| .map(continent -> continent.name().toLowerCase(Locale.ROOT)) | ||
| .toList(); | ||
| } | ||
|
|
||
| private static Continent parseContinent(String name) { | ||
| String normalized = name.replace('_', ' ').replace('-', ' '); | ||
| return Arrays.stream(Continent.values()) | ||
| .filter(continent -> continent.getLabel().equalsIgnoreCase(normalized)) | ||
| .findFirst().orElse(null); | ||
| } | ||
|
|
||
| private static List<String> cityStateNames() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this nav submenu only meant for the US? Otherwise it may make more sense to say province. |
||
| return StateSelectorMenu.getCityStates().stream() | ||
| .map(region -> region.getName().replace(' ', '_')) | ||
| .distinct().toList(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,11 @@ public class MainMenu extends AbstractMenu { | |
|
|
||
| private static final String INVENTORY_NAME = "BuildTheEarth Navigator"; | ||
| public MainMenu(Player menuPlayer) { | ||
| super(3, INVENTORY_NAME, menuPlayer); | ||
| this(menuPlayer, true); | ||
| } | ||
|
|
||
| public MainMenu(Player menuPlayer, boolean autoLoad) { | ||
| super(3, INVENTORY_NAME, menuPlayer, autoLoad); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the inventory slot hard-coded here? Wouldn't it make more sense to add a command to give you the nav selector? (We can also make an issue if this is out of scope). |
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -87,7 +91,7 @@ protected void setItemClickEventsAsync() { | |
| getMenu().getSlot(Objects.requireNonNull(slots.pollFirst())) | ||
| .setClickHandler((clickPlayer, clickInformation) -> { | ||
| clickPlayer.closeInventory(); | ||
| performClickAction(clickPlayer, config().mainMenuItems().buildItem().action().replace("&", "§"), "build"); | ||
| openBuild(clickPlayer); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -96,7 +100,7 @@ protected void setItemClickEventsAsync() { | |
| getMenu().getSlot(Objects.requireNonNull(slots.pollFirst())) | ||
| .setClickHandler((clickPlayer, clickInformation) -> { | ||
| clickPlayer.closeInventory(); | ||
| performClickAction(clickPlayer, config().mainMenuItems().plotsystemItem().action().replace("&", "§"), "plotsystem"); | ||
| openPlotSystem(clickPlayer); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -118,15 +122,7 @@ protected void setItemClickEventsAsync() { | |
| if (config().mainMenuItems().tutorialItem().enabled()) { | ||
| getMenu().getSlot(Objects.requireNonNull(slots.pollFirst())).setClickHandler((clickPlayer, clickInformation) -> { | ||
| clickPlayer.closeInventory(); | ||
| String action = config().mainMenuItems().tutorialItem().action(); | ||
|
|
||
| // If no command or message is set, open the tutorial menu | ||
| if (action == null || action.equals("/command") || action.equals("message")) { | ||
| new TutorialsMenu(clickPlayer); | ||
| return; | ||
| } | ||
|
|
||
| performClickAction(clickPlayer, action.replace("&", "§"), "tutorial"); | ||
| openTutorials(clickPlayer); | ||
| }); | ||
| } | ||
| } | ||
|
|
@@ -189,15 +185,42 @@ protected Mask getMask() { | |
| return slots; | ||
| } | ||
|
|
||
| private @NotNull NavigationConfig config() { | ||
| private static @NotNull NavigationConfig config() { | ||
| NavigationModule navigationModule = NavigationModule.getInstance(); | ||
| if (navigationModule == null) { | ||
| throw new IllegalStateException("Navigation module is not initialized"); | ||
| } | ||
| return navigationModule.getConfig(); | ||
| } | ||
|
|
||
| private void performClickAction(Player p, String action, String type) { | ||
| public static void openBuild(Player player) { | ||
| openConfiguredItem(player, config().mainMenuItems().buildItem(), "build"); | ||
| } | ||
|
|
||
| public static void openPlotSystem(Player player) { | ||
| openConfiguredItem(player, config().mainMenuItems().plotsystemItem(), "plotsystem"); | ||
| } | ||
|
|
||
| public static void openTutorials(Player player) { | ||
| openConfiguredItem(player, config().mainMenuItems().tutorialItem(), "tutorial"); | ||
| } | ||
|
|
||
| static void openConfiguredItem(Player player, NavigationConfig.MenuItem item, String type) { | ||
| if (!item.enabled()) { | ||
| player.sendMessage(ChatHelper.getErrorString("The %s item is disabled in the navigation config.", type)); | ||
| return; | ||
| } | ||
| String action = item.action(); | ||
| if (type.equals("tutorial") && (action == null || action.equals("/command") || action.equals("message"))) { | ||
| new TutorialsMenu(player); | ||
| } else if (action == null) { | ||
| player.sendMessage(ChatHelper.getErrorString("No action is set for the %s item in the config yet! Please contact an %s.", type, "admin")); | ||
| } else { | ||
| performClickAction(player, action.replace("&", "§"), type); | ||
| } | ||
| } | ||
|
|
||
| private static void performClickAction(Player p, String action, String type) { | ||
| // Check if an action is set in the config | ||
| if (action.startsWith("transfer:")) { | ||
| NavUtils.transferPlayer(p, action.substring(9)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this fallback really necessary? Wouldn't it make more sense to just let the menu open but have no warps there yet? Then it's also easier to add states there in the future.