-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement SingleArgumentProcessor & PlayerArgumentProcessor
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/java/me/hsgamer/bettergui/argument/type/PlayerArgumentProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/me/hsgamer/bettergui/argument/type/SingleArgumentProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters