fix(navigation): preserve disabled preference
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
package games.dmg.spigotbase;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
final class BaseNavigationCommand implements CommandExecutor {
|
||||
final class BaseNavigationCommand implements CommandExecutor, TabCompleter {
|
||||
private static final List<String> MODES = List.of("on", "off");
|
||||
|
||||
private final BaseStateManager stateManager;
|
||||
|
||||
BaseNavigationCommand(BaseStateManager stateManager) {
|
||||
@@ -28,10 +33,17 @@ final class BaseNavigationCommand implements CommandExecutor {
|
||||
player.sendMessage(ChatColor.RED + "Set your base before enabling navigation.");
|
||||
return true;
|
||||
}
|
||||
final boolean enabled;
|
||||
try {
|
||||
enabled = NavigationPreference.resolve(state.navigationEnabled(), arguments);
|
||||
} catch (IllegalArgumentException exception) {
|
||||
player.sendMessage(ChatColor.RED + "Usage: /basenavigation [on|off]");
|
||||
return true;
|
||||
}
|
||||
state = stateManager.update(
|
||||
player.getUniqueId(),
|
||||
player.getName(),
|
||||
current -> current.withNavigationEnabled(!current.navigationEnabled())
|
||||
current -> current.withNavigationEnabled(enabled)
|
||||
);
|
||||
stateManager.saveIfDirty();
|
||||
player.sendMessage(ChatColor.YELLOW + "Base navigation is now "
|
||||
@@ -44,4 +56,18 @@ final class BaseNavigationCommand implements CommandExecutor {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(
|
||||
CommandSender sender,
|
||||
Command command,
|
||||
String alias,
|
||||
String[] arguments
|
||||
) {
|
||||
if (arguments.length != 1) {
|
||||
return List.of();
|
||||
}
|
||||
String prefix = arguments[0].toLowerCase(Locale.ROOT);
|
||||
return MODES.stream().filter(mode -> mode.startsWith(prefix)).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public final class BaseProgressionService {
|
||||
}
|
||||
boolean unlocked = baseLevel != previousLevel;
|
||||
PlayerState updated = player.withGrassAndDirtProgress(count, baseLevel);
|
||||
if (baseLevel >= 2 && !updated.navigationEnabled()) {
|
||||
if (unlocked && baseLevel == 2) {
|
||||
updated = updated.withNavigationEnabled(true);
|
||||
}
|
||||
return new ProgressionUpdate(updated, unlocked, false, false, false, false);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package games.dmg.spigotbase;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
final class NavigationPreference {
|
||||
private NavigationPreference() {
|
||||
}
|
||||
|
||||
static boolean resolve(boolean current, String[] arguments) {
|
||||
if (arguments.length == 0) {
|
||||
return !current;
|
||||
}
|
||||
if (arguments.length != 1) {
|
||||
throw new IllegalArgumentException("expected zero or one argument");
|
||||
}
|
||||
return switch (arguments[0].toLowerCase(Locale.ROOT)) {
|
||||
case "on" -> true;
|
||||
case "off" -> false;
|
||||
default -> throw new IllegalArgumentException("expected on or off");
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,9 @@ public final class SpigotBasePlugin extends JavaPlugin {
|
||||
command("baseprogress").setExecutor(
|
||||
new BaseProgressCommand(stateManager, settingsProvider)
|
||||
);
|
||||
command("basenavigation").setExecutor(new BaseNavigationCommand(stateManager));
|
||||
BaseNavigationCommand navigationCommand = new BaseNavigationCommand(stateManager);
|
||||
command("basenavigation").setExecutor(navigationCommand);
|
||||
command("basenavigation").setTabCompleter(navigationCommand);
|
||||
command("baseflight").setExecutor(new BaseFlightCommand(stateManager, flightController));
|
||||
command("basevisitors").setExecutor(new BaseVisitorsCommand(stateManager));
|
||||
GoToBaseCommand goToBaseCommand = new GoToBaseCommand(stateManager, teleportManager);
|
||||
|
||||
@@ -13,7 +13,7 @@ commands:
|
||||
usage: /base [upgrade]
|
||||
basenavigation:
|
||||
description: Toggle particle navigation toward your base.
|
||||
usage: /basenavigation
|
||||
usage: /basenavigation [on|off]
|
||||
baseflight:
|
||||
description: Toggle flight within your base.
|
||||
usage: /baseflight
|
||||
|
||||
Reference in New Issue
Block a user