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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
...earth/buildteamtools/modules/navigation/components/navigator/commands/ExploreCommand.java
This file was deleted.
Oops, something went wrong.
160 changes: 137 additions & 23 deletions
160
...rth/buildteamtools/modules/navigation/components/navigator/commands/NavigatorCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { | ||
|
Zoriot marked this conversation as resolved.
|
||
| return StateSelectorMenu.getCityStates().stream() | ||
| .map(region -> region.getName().replace(' ', '_')) | ||
| .distinct().toList(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.