Skip to content

Commit

Permalink
Dynamically grab flags
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 10, 2022
1 parent b63df41 commit 721f027
Show file tree
Hide file tree
Showing 3 changed files with 397 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,36 @@
public class AdminSettingsCommand extends CompositeCommand {

private static final String SPAWN_ISLAND = "spawn-island";
private final List<String> PROTECTION_FLAG_NAMES;
private List<String> protectionFlagNames;
private Island island;
private final List<String> SETTING_FLAG_NAMES;
private final List<String> WORLD_SETTING_FLAG_NAMES;
private List<String> settingFlagNames;
private List<String> worldSettingFlagNames;
private @NonNull Optional<Flag> flag = Optional.empty();
private boolean activeState;
private int rank;
private final GameModeAddon gameMode;

public AdminSettingsCommand(CompositeCommand islandCommand) {
super(islandCommand, "settings", "flags", "options");
// make constants
GameModeAddon gameMode = getPlugin().getIWM().getAddon(getWorld()).orElse(null);
PROTECTION_FLAG_NAMES = getPlugin().getFlagsManager().getFlags().stream()
gameMode = getPlugin().getIWM().getAddon(getWorld()).orElse(null);
}

private void makeLists() {
protectionFlagNames = getPlugin().getFlagsManager().getFlags().stream()
.filter(f -> f.getType().equals(Type.PROTECTION))
.filter(f -> f.getGameModes().isEmpty() || gameMode == null || f.getGameModes().contains(gameMode))
.map(Flag::getID)
.collect(Collectors.toList());
SETTING_FLAG_NAMES = getPlugin().getFlagsManager().getFlags().stream()
.toList();
settingFlagNames = getPlugin().getFlagsManager().getFlags().stream()
.filter(f -> f.getType().equals(Type.SETTING))
.filter(f -> f.getGameModes().isEmpty() || gameMode == null || f.getGameModes().contains(gameMode))
.map(Flag::getID)
.collect(Collectors.toList());
WORLD_SETTING_FLAG_NAMES = getPlugin().getFlagsManager().getFlags().stream()
.toList();
worldSettingFlagNames = getPlugin().getFlagsManager().getFlags().stream()
.filter(f -> f.getType().equals(Type.WORLD_SETTING))
.filter(f -> f.getGameModes().isEmpty() || gameMode == null || f.getGameModes().contains(gameMode))
.map(Flag::getID)
.collect(Collectors.toList());
.toList();
}

@Override
Expand All @@ -79,7 +82,7 @@ public boolean canExecute(User user, String label, List<String> args) {
}
if (args.size() > 1) {
// Command
return checkSyntax(user, args);
return checkSyntax(user, args);
}
return getIsland(user, args);
}
Expand Down Expand Up @@ -112,10 +115,12 @@ private boolean getIsland(User user, List<String> args) {
* @return true if the syntax is correct
*/
private boolean checkSyntax(User user, List<String> args) {
// Update the flag lists
this.makeLists();
if (args.size() == 2) {
// Should be a world setting
// If world settings, then active/disabled, otherwise player flags
if (WORLD_SETTING_FLAG_NAMES.contains(args.get(0).toUpperCase(Locale.ENGLISH))) {
if (worldSettingFlagNames.contains(args.get(0).toUpperCase(Locale.ENGLISH))) {
if (checkActiveDisabled(user, args.get(1))) {
flag = getPlugin().getFlagsManager().getFlag(args.get(0).toUpperCase(Locale.ENGLISH));
return true;
Expand All @@ -130,17 +135,17 @@ private boolean checkSyntax(User user, List<String> args) {
return false;
}

if (!SETTING_FLAG_NAMES.contains(args.get(1).toUpperCase(Locale.ENGLISH))
&& !PROTECTION_FLAG_NAMES.contains(args.get(1).toUpperCase(Locale.ENGLISH))) {
if (!settingFlagNames.contains(args.get(1).toUpperCase(Locale.ENGLISH))
&& !protectionFlagNames.contains(args.get(1).toUpperCase(Locale.ENGLISH))) {
user.sendMessage("commands.admin.settings.unknown-flag", TextVariables.NAME, args.get(2));
return false;
return false;
}
// Set flag
flag = getPlugin().getFlagsManager().getFlag(args.get(1).toUpperCase(Locale.ENGLISH));
// Check settings
if (flag.isPresent()) {
if (flag.get().getType().equals(Type.SETTING)) {
return checkActiveDisabled(user, args.get(2));
return checkActiveDisabled(user, args.get(2));
} else {
// Protection flag
return checkRank(user, String.join(" ", args.subList(2, args.size())));
Expand Down Expand Up @@ -175,7 +180,7 @@ private boolean checkActiveDisabled(User user, String string) {
String disabled = ChatColor.stripColor(user.getTranslation("protection.panel.flag-item.setting-disabled"));
if (!string.equalsIgnoreCase(active) && !string.equalsIgnoreCase(disabled)) {
user.sendMessage("commands.admin.settings.unknown-setting", TextVariables.NAME, string);
return false;
return false;
}
activeState = string.equalsIgnoreCase(active);
return true;
Expand All @@ -199,7 +204,7 @@ public boolean execute(User user, String label, List<String> args) {
f.setSetting(getWorld(), activeState);
break;
default:
break;
break;
}
});
user.sendMessage("general.success");
Expand Down Expand Up @@ -236,36 +241,39 @@ public boolean execute(User user, String label, List<String> args) {

@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
// Update with the latest lists
this.makeLists();
String active = ChatColor.stripColor(user.getTranslation("protection.panel.flag-item.setting-active"));
String disabled = ChatColor.stripColor(user.getTranslation("protection.panel.flag-item.setting-disabled"));
List<String> options = new ArrayList<>();
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
System.out.println("Arg size = " + args.size());
if (args.size() == 2) {
// Player names or world settings
// Player names or world settings
options = Util.tabLimit(Util.getOnlinePlayerList(user), lastArg);
options.addAll(WORLD_SETTING_FLAG_NAMES);
options.addAll(worldSettingFlagNames);
if (getIslands().getSpawn(getWorld()).isPresent()) {
options.add(SPAWN_ISLAND);
}
} else if (args.size() == 3) {
// If world settings, then active/disabled, otherwise player flags
if (WORLD_SETTING_FLAG_NAMES.contains(args.get(1).toUpperCase(Locale.ENGLISH))) {
if (worldSettingFlagNames.contains(args.get(1).toUpperCase(Locale.ENGLISH))) {
options = Arrays.asList(active, disabled);
} else {
// Flag IDs
options.addAll(PROTECTION_FLAG_NAMES);
options.addAll(SETTING_FLAG_NAMES);
options.addAll(protectionFlagNames);
options.addAll(settingFlagNames);
}
} else if (args.size() == 4) {
// Get flag in previous argument
options = getPlugin().getFlagsManager().getFlag(args.get(2).toUpperCase(Locale.ENGLISH)).map(f -> switch (f.getType()) {
case PROTECTION -> getPlugin().getRanksManager()
.getRanks().entrySet().stream()
.filter(en -> en.getValue() > RanksManager.BANNED_RANK && en.getValue() <= RanksManager.OWNER_RANK)
.map(Entry::getKey)
.map(user::getTranslation).collect(Collectors.toList());
case SETTING -> Arrays.asList(active, disabled);
default -> Collections.<String>emptyList();
case PROTECTION -> getPlugin().getRanksManager()
.getRanks().entrySet().stream()
.filter(en -> en.getValue() > RanksManager.BANNED_RANK && en.getValue() <= RanksManager.OWNER_RANK)
.map(Entry::getKey)
.map(user::getTranslation).collect(Collectors.toList());
case SETTING -> Arrays.asList(active, disabled);
default -> Collections.<String>emptyList();
}).orElse(Collections.emptyList());
}
return Optional.of(Util.tabLimit(options, lastArg));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected List<Flag> getFlags() {
List<Flag> flags = plugin.getFlagsManager().getFlags().stream().filter(f -> f.getType().equals(type))
// We're stripping colors to avoid weird sorting issues
.sorted(Comparator.comparing(flag -> ChatColor.stripColor(user.getTranslation(flag.getNameReference()))))
.collect(Collectors.toList());
.toList();
// Remove any that are not for this game mode
plugin.getIWM().getAddon(world).ifPresent(gm -> flags.removeIf(f -> !f.getGameModes().isEmpty() && !f.getGameModes().contains(gm)));
// Remove any that are the wrong rank or that will be on the top row
Expand Down Expand Up @@ -136,27 +136,27 @@ public Map<Integer, PanelItem> getTabIcons() {
}
// Add the mode icon
switch (plugin.getPlayers().getFlagsDisplayMode(user.getUniqueId())) {
case ADVANCED -> icons.put(7, new PanelItemBuilder().icon(Material.GOLD_INGOT)
.name(user.getTranslation(PROTECTION_PANEL + "mode.advanced.name"))
.description(user.getTranslation(PROTECTION_PANEL + "mode.advanced.description"), "",
user.getTranslation(CLICK_TO_SWITCH,
TextVariables.NEXT, user.getTranslation(PROTECTION_PANEL + "mode.expert.name")))
.clickHandler(this)
.build());
case EXPERT -> icons.put(7, new PanelItemBuilder().icon(Material.NETHER_BRICK)
.name(user.getTranslation(PROTECTION_PANEL + "mode.expert.name"))
.description(user.getTranslation(PROTECTION_PANEL + "mode.expert.description"), "",
user.getTranslation(CLICK_TO_SWITCH,
TextVariables.NEXT, user.getTranslation(PROTECTION_PANEL + "mode.basic.name")))
.clickHandler(this)
.build());
default -> icons.put(7, new PanelItemBuilder().icon(Material.IRON_INGOT)
.name(user.getTranslation(PROTECTION_PANEL + "mode.basic.name"))
.description(user.getTranslation(PROTECTION_PANEL + "mode.basic.description"), "",
user.getTranslation(CLICK_TO_SWITCH,
TextVariables.NEXT, user.getTranslation(PROTECTION_PANEL + "mode.advanced.name")))
.clickHandler(this)
.build());
case ADVANCED -> icons.put(7, new PanelItemBuilder().icon(Material.GOLD_INGOT)
.name(user.getTranslation(PROTECTION_PANEL + "mode.advanced.name"))
.description(user.getTranslation(PROTECTION_PANEL + "mode.advanced.description"), "",
user.getTranslation(CLICK_TO_SWITCH,
TextVariables.NEXT, user.getTranslation(PROTECTION_PANEL + "mode.expert.name")))
.clickHandler(this)
.build());
case EXPERT -> icons.put(7, new PanelItemBuilder().icon(Material.NETHER_BRICK)
.name(user.getTranslation(PROTECTION_PANEL + "mode.expert.name"))
.description(user.getTranslation(PROTECTION_PANEL + "mode.expert.description"), "",
user.getTranslation(CLICK_TO_SWITCH,
TextVariables.NEXT, user.getTranslation(PROTECTION_PANEL + "mode.basic.name")))
.clickHandler(this)
.build());
default -> icons.put(7, new PanelItemBuilder().icon(Material.IRON_INGOT)
.name(user.getTranslation(PROTECTION_PANEL + "mode.basic.name"))
.description(user.getTranslation(PROTECTION_PANEL + "mode.basic.description"), "",
user.getTranslation(CLICK_TO_SWITCH,
TextVariables.NEXT, user.getTranslation(PROTECTION_PANEL + "mode.advanced.name")))
.clickHandler(this)
.build());
}
// Add the reset everything to default - it's only in the player's settings panel
if (island != null && user.getUniqueId().equals(island.getOwner())) {
Expand Down

0 comments on commit 721f027

Please sign in to comment.