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 @@ -90,6 +90,10 @@ public MainConfig getMainConfig() {
return mainConfigLoader.getConfig();
}

public NavigationConfig getNavigationConfig() {
return navigationConfigLoader.getConfig();
}

@Override
public void saveConfig() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class BttCommandManager {

private static final Set<String> COMMAND_NAMES = Set.of(
"btt", "generate", "kml", "geopoints", "geopath", "georing", "geosurface",
"navigator", "buildteam", "btwarps", "explore", "address", "warp", "blockpalette",
"navigator", "buildteam", "btwarps", "address", "warp", "blockpalette",
"help", "communicators", "cache", "debug", "checkforupdates", "reload-config", "update"
);
private static final ParserDescriptor<CommandSourceStack, String> ARGUMENT_PARSER =
Expand Down Expand Up @@ -423,7 +423,6 @@ private List<String> configuredAliases(String name) {
case "navigator" -> labels.navigator();
case "buildteam" -> labels.buildteam();
case "btwarps" -> labels.btwarps();
case "explore" -> labels.explore();
case "address" -> labels.address();
case "warp" -> labels.warp();
case "blockpalette" -> labels.blockpalette();
Expand Down Expand Up @@ -475,7 +474,6 @@ private static RichDescription descriptionFor(String name) {
case "navigator" -> "Open the build team navigation menu.";
case "buildteam" -> "Switch to a build team by name or tag.";
case "btwarps" -> "Open the warp menu for a build team.";
case "explore" -> "Explore nearby build team regions.";
case "address" -> "Look up the address at your current location.";
case "warp" -> "Warp to a saved location.";
case "blockpalette" -> "Open the block palette menu.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.buildtheearth.buildteamtools.modules.navigation.components.bluemap.BluemapComponent;
import net.buildtheearth.buildteamtools.modules.navigation.components.navigator.NavigatorComponent;
import net.buildtheearth.buildteamtools.modules.navigation.components.navigator.commands.BuildteamCommand;
import net.buildtheearth.buildteamtools.modules.navigation.components.navigator.commands.ExploreCommand;
import net.buildtheearth.buildteamtools.modules.navigation.components.navigator.commands.NavigatorCommand;
import net.buildtheearth.buildteamtools.modules.navigation.components.navigator.listeners.NavigatorJoinListener;
import net.buildtheearth.buildteamtools.modules.navigation.components.navigator.listeners.NavigatorOpenListener;
Expand Down Expand Up @@ -166,7 +165,6 @@ public void registerCommands() {
new NavigatorCommand().register(this);
new BuildteamCommand().register(this);
new BtWarpsCommand().register(this);
new ExploreCommand().register(this);
new AddressCommand().register(this);
}

Expand Down

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() {
return StateSelectorMenu.getCityStates().stream()
.map(region -> region.getName().replace(' ', '_'))
.distinct().toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

@Override
Expand Down Expand Up @@ -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);
});
}

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

Expand All @@ -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);
});
}
}
Expand Down Expand Up @@ -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));
Expand Down
Loading
Loading