Skip to content

Commit

Permalink
implement SingleArgumentProcessor & PlayerArgumentProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Oct 29, 2023
1 parent 6a1aed6 commit b7cf207
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package me.hsgamer.bettergui.argument.type;

import me.hsgamer.bettergui.builder.ArgumentProcessorBuilder;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;

import java.util.Arrays;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;

public class PlayerArgumentProcessor extends SingleArgumentProcessor<OfflinePlayer> {
private final boolean onlineOnly;

public PlayerArgumentProcessor(ArgumentProcessorBuilder.Input input) {
super(input);

this.onlineOnly = Optional.ofNullable(options.get("online-only"))
.map(String::valueOf)
.map(Boolean::parseBoolean)
.orElse(false);
}

@Override
public String getValue(String query, UUID uuid) {
return getObject(uuid).map(this::getArgumentValue).orElse("");
}

@Override
protected Optional<OfflinePlayer> getObject(String name) {
if (onlineOnly) {
return Optional.ofNullable(Bukkit.getPlayer(name));
} else {
//noinspection deprecation
return Optional.ofNullable(Bukkit.getOfflinePlayer(name));
}
}

@Override
protected Stream<OfflinePlayer> getObjectStream() {
if (onlineOnly) {
return Arrays.stream(Bukkit.getOnlinePlayers().toArray(new OfflinePlayer[0]));
} else {
return Arrays.stream(Bukkit.getOfflinePlayers());
}
}

@Override
protected String getArgumentValue(OfflinePlayer object) {
return Optional.ofNullable(object.getName()).orElse("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package me.hsgamer.bettergui.argument.type;

import me.hsgamer.bettergui.BetterGUI;
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.collections.map.CaseInsensitiveStringMap;
import me.hsgamer.hscore.common.MapUtils;
import me.hsgamer.hscore.common.Pair;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class SingleArgumentProcessor<T> implements ArgumentProcessor {
protected final Map<String, Object> options;
private final ArgumentProcessorBuilder.Input input;
private final Map<UUID, T> map = new HashMap<>();
private final ActionApplier onRequiredActionApplier;
private final ActionApplier onInvalidActionApplier;

public SingleArgumentProcessor(ArgumentProcessorBuilder.Input input) {
this.input = input;
options = new CaseInsensitiveStringMap<>(input.options);
this.onRequiredActionApplier = new ActionApplier(input.menu, MapUtils.getIfFoundOrDefault(options, Collections.emptyList(), "required-command", "required-action", "action", "command"));
this.onInvalidActionApplier = new ActionApplier(input.menu, MapUtils.getIfFoundOrDefault(options, Collections.emptyList(), "invalid-command", "invalid-action"));
}

protected abstract Optional<T> getObject(String name);

protected abstract Stream<T> getObjectStream();

protected abstract String getArgumentValue(T object);

protected Optional<T> getObject(UUID uuid) {
return Optional.ofNullable(map.get(uuid));
}

@Override
public Optional<String[]> process(UUID uuid, String[] args) {
if (args.length == 0) {
BetterGUI.runBatchRunnable(batchRunnable ->
batchRunnable.getTaskPool(ProcessApplierConstants.ACTION_STAGE)
.addLast(process ->
onRequiredActionApplier.accept(uuid, process)
)
);
return Optional.empty();
}

Optional<T> object = getObject(args[0]);
if (!object.isPresent()) {
BetterGUI.runBatchRunnable(batchRunnable ->
batchRunnable.getTaskPool(ProcessApplierConstants.ACTION_STAGE)
.addLast(process ->
onInvalidActionApplier.accept(uuid, process)
)
);
return Optional.empty();
}

map.put(uuid, object.get());
return Optional.of(Arrays.copyOfRange(args, 1, args.length));
}

@Override
public Pair<Optional<List<String>>, String[]> tabComplete(UUID uuid, String[] args) {
if (args.length == 1) {
String query = args[0];
List<String> suggestions = getObjectStream()
.map(this::getArgumentValue)
.filter(s -> s.toLowerCase(Locale.ROOT).startsWith(query.toLowerCase(Locale.ROOT)))
.collect(Collectors.toList());
return Pair.of(Optional.of(suggestions), Arrays.copyOfRange(args, 1, args.length));
}

return Pair.of(Optional.empty(), args);
}

@Override
public Menu getMenu() {
return input.menu;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.hsgamer.bettergui.api.argument.ArgumentProcessor;
import me.hsgamer.bettergui.api.menu.Menu;
import me.hsgamer.bettergui.argument.type.PlayerArgumentProcessor;
import me.hsgamer.bettergui.argument.type.StoreArgumentProcessor;
import me.hsgamer.hscore.builder.MassBuilder;
import me.hsgamer.hscore.collections.map.CaseInsensitiveStringMap;
Expand All @@ -22,6 +23,7 @@ public final class ArgumentProcessorBuilder extends MassBuilder<ArgumentProcesso

private ArgumentProcessorBuilder() {
register(StoreArgumentProcessor::new, "store");
register(PlayerArgumentProcessor::new, "player");
}

/**
Expand Down

0 comments on commit b7cf207

Please sign in to comment.