-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
PlayerArgumentProcessor.java
58 lines (49 loc) · 1.73 KB
/
PlayerArgumentProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package me.hsgamer.bettergui.argument.type;
import me.hsgamer.bettergui.builder.ArgumentProcessorBuilder;
import me.hsgamer.bettergui.util.StringReplacerApplier;
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
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("");
}
@Override
protected String getValue(String query, UUID uuid, OfflinePlayer object) {
if (query.startsWith("papi_")) {
String papiQuery = query.substring("papi_".length());
return StringReplacerApplier.replace("%" + papiQuery + "%", object.getUniqueId(), this);
} else {
return StringReplacerApplier.replace("{" + query + "}", object.getUniqueId(), this);
}
}
}