Skip to content

Commit

Permalink
option to check the value on the suggestion list
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Oct 31, 2023
1 parent 0a2e569 commit 6e2135a
Showing 1 changed file with 22 additions and 22 deletions.
@@ -1,12 +1,8 @@
package me.hsgamer.bettergui.argument.type;

import me.hsgamer.bettergui.action.ActionApplier;
import me.hsgamer.bettergui.api.argument.ArgumentProcessor;
import me.hsgamer.bettergui.api.menu.Menu;
import me.hsgamer.bettergui.builder.ArgumentProcessorBuilder;
import me.hsgamer.bettergui.util.ProcessApplierConstants;
import me.hsgamer.hscore.bukkit.scheduler.Scheduler;
import me.hsgamer.hscore.collections.map.CaseInsensitiveStringMap;
import me.hsgamer.hscore.common.CollectionUtils;
import me.hsgamer.hscore.common.MapUtils;
import me.hsgamer.hscore.common.Pair;
Expand All @@ -17,18 +13,15 @@
import java.util.*;
import java.util.stream.Collectors;

public class StoreArgumentProcessor implements ArgumentProcessor {
private final ArgumentProcessorBuilder.Input input;
public class StoreArgumentProcessor extends BaseActionArgumentProcessor {
private final Map<UUID, String> map = new HashMap<>();
private final int length;
private final boolean takeRemaining;
private final ActionApplier actionApplier;
private final List<String> suggestions;
private final boolean checkSuggestion;

public StoreArgumentProcessor(ArgumentProcessorBuilder.Input input) {
this.input = input;

Map<String, Object> options = new CaseInsensitiveStringMap<>(input.options);
super(input);

this.length = Optional.ofNullable(options.get("length"))
.map(String::valueOf)
Expand All @@ -41,11 +34,14 @@ public StoreArgumentProcessor(ArgumentProcessorBuilder.Input input) {
.map(Boolean::parseBoolean)
.orElse(false);

this.actionApplier = new ActionApplier(input.menu, MapUtils.getIfFoundOrDefault(options, Collections.emptyList(), "required-command", "required-action", "action", "command"));

this.suggestions = Optional.ofNullable(MapUtils.getIfFound(options, "suggestion", "suggest"))
.map(CollectionUtils::createStringListFromObject)
.orElse(Collections.emptyList());

this.checkSuggestion = Optional.ofNullable(MapUtils.getIfFound(options, "check-suggestion", "check-suggest"))
.map(String::valueOf)
.map(Boolean::parseBoolean)
.orElse(false);
}

@Override
Expand All @@ -56,19 +52,28 @@ public Optional<String[]> process(UUID uuid, String[] args) {

if (length > 0 && args.length < length) {
BatchRunnable batchRunnable = new BatchRunnable();
batchRunnable.getTaskPool(ProcessApplierConstants.ACTION_STAGE).addLast(process -> actionApplier.accept(uuid, process));
batchRunnable.getTaskPool(ProcessApplierConstants.ACTION_STAGE).addLast(process -> onRequiredActionApplier.accept(uuid, process));
Scheduler.current().async().runTask(batchRunnable);
return Optional.empty();
}

String current;
String[] remaining;
if (takeRemaining) {
map.put(uuid, String.join(" ", args));
return Optional.of(new String[0]);
current = String.join(" ", args);
remaining = new String[0];
} else {
String[] store = Arrays.copyOfRange(args, 0, length);
map.put(uuid, String.join(" ", store));
return Optional.of(Arrays.copyOfRange(args, length, args.length));
current = String.join(" ", store);
remaining = Arrays.copyOfRange(args, length, args.length);
}

if (checkSuggestion && !suggestions.isEmpty() && !suggestions.contains(current)) {
return Optional.empty();
}

map.put(uuid, current);
return Optional.of(remaining);
}

@Override
Expand Down Expand Up @@ -98,9 +103,4 @@ public Pair<Optional<List<String>>, String[]> tabComplete(UUID uuid, String[] ar

return Pair.of(Optional.of(list), new String[0]);
}

@Override
public Menu getMenu() {
return input.menu;
}
}

0 comments on commit 6e2135a

Please sign in to comment.