-
Notifications
You must be signed in to change notification settings - Fork 631
Expand file tree
/
Copy pathPlaceholderAPIIntegration.java
More file actions
147 lines (118 loc) · 4.71 KB
/
PlaceholderAPIIntegration.java
File metadata and controls
147 lines (118 loc) · 4.71 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package io.github.thebusybiscuit.slimefun4.integrations;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
/**
* This is our integration for {@link PlaceholderAPI}.
*
* @author TheBusyBiscuit
*
*/
class PlaceholderAPIIntegration extends PlaceholderExpansion {
private final String version;
private final String author;
public PlaceholderAPIIntegration(@Nonnull Slimefun plugin) {
this.version = plugin.getDescription().getVersion();
this.author = plugin.getDescription().getAuthors().toString();
}
@Nonnull
@Override
public String getIdentifier() {
return "slimefun";
}
@Nonnull
@Override
public String getVersion() {
return version;
}
@Nonnull
@Override
public String getAuthor() {
return author;
}
@Override
public boolean persist() {
return true;
}
@Override
public boolean canRegister() {
return true;
}
private boolean isPlaceholder(@Nullable OfflinePlayer p, boolean requiresProfile, @Nonnull String params, @Nonnull String placeholder) {
if (requiresProfile) {
if (p != null && placeholder.equals(params)) {
PlayerProfile.request(p);
return true;
} else {
return false;
}
} else {
return placeholder.equals(params);
}
}
@Override
public String onRequest(@Nullable OfflinePlayer p, @Nonnull String params) {
if (isPlaceholder(p, true, params, "researches_total_xp_levels_spent")) {
Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) {
Stream<Research> stream = profile.get().getResearches().stream();
return String.valueOf(stream.mapToInt(Research::getCost).sum());
} else if (p instanceof Player player) {
return getProfilePlaceholder(player);
}
}
if (isPlaceholder(p, true, params, "researches_total_researches_unlocked")) {
Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) {
Set<Research> set = profile.get().getResearches();
return String.valueOf(set.size());
} else if (p instanceof Player player) {
return getProfilePlaceholder(player);
}
}
if (isPlaceholder(p, false, params, "researches_total_researches")) {
return String.valueOf(Slimefun.getRegistry().getResearches().size());
}
if (isPlaceholder(p, true, params, "researches_percentage_researches_unlocked")) {
Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) {
int unlockedResearches = profile.get().countNonEmptyResearches(profile.get().getResearches());
int allResearches = profile.get().countNonEmptyResearches(Slimefun.getRegistry().getResearches());
return String.valueOf(Math.round(((unlockedResearches * 100.0F) / allResearches) * 100.0F) / 100.0F);
} else if (p instanceof Player player) {
return getProfilePlaceholder(player);
}
}
if (isPlaceholder(p, true, params, "researches_title")) {
Optional<PlayerProfile> profile = PlayerProfile.find(p);
if (profile.isPresent()) {
return profile.get().getTitle();
} else if (p instanceof Player player) {
return getProfilePlaceholder(player);
}
}
if (isPlaceholder(p, false, params, "gps_complexity") && p != null) {
return String.valueOf(Slimefun.getGPSNetwork().getNetworkComplexity(p.getUniqueId()));
}
if (isPlaceholder(p, false, params, "timings_lag")) {
return Slimefun.getProfiler().getTime();
}
if (isPlaceholder(p, false, params, "language") && p instanceof Player player) {
return Slimefun.getLocalization().getLanguage(player).getName(player);
}
return null;
}
@Nonnull
private String getProfilePlaceholder(@Nonnull Player p) {
return Slimefun.getLocalization().getMessage(p, "placeholderapi.profile-loading");
}
}