Skip to content

Commit 12b2cc2

Browse files
committed
move getName method to cache class
1 parent 82fe0ce commit 12b2cc2

11 files changed

Lines changed: 86 additions & 87 deletions

File tree

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/pro/cloudnode/smp/smpcore/CachedProfile.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pro.cloudnode.smp.smpcore;
22

3+
import com.destroystokyo.paper.profile.PlayerProfile;
34
import com.google.gson.JsonObject;
45
import com.google.gson.JsonParser;
56
import io.papermc.paper.plugin.configuration.PluginMeta;
@@ -204,6 +205,28 @@ public static void cleanUp() {
204205
}
205206
}
206207

208+
public static @NotNull String getName(final @NotNull OfflinePlayer player) {
209+
final @Nullable String name = player.getName();
210+
if (name != null)
211+
return name;
212+
213+
final PlayerProfile profile = player.getPlayerProfile();
214+
215+
if (profile.completeFromCache(true)) {
216+
final @Nullable String profileName = profile.getName();
217+
if (profileName != null && !profileName.isEmpty())
218+
return profileName;
219+
}
220+
221+
try {
222+
return get(player).name();
223+
}
224+
catch (IllegalStateException e) {
225+
SMPCore.getInstance().getLogger().warning("Failed to fetch");
226+
return player.getUniqueId().toString();
227+
}
228+
}
229+
207230
private static @NotNull Date expirationThreshold() {
208231
return new Date(System.currentTimeMillis() - MAX_AGE.toMillis());
209232
}

src/main/java/pro/cloudnode/smp/smpcore/Configuration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public boolean deathBanEnabled() {
113113
}
114114

115115
public void staffCommands(final boolean addMode, final @NotNull OfflinePlayer player) {
116-
final String name = SMPCore.getName(player);
116+
final String name = CachedProfile.getName(player);
117117

118118
final List<String> commands = config.getStringList("staff.commands." + (addMode ? "add" : "remove"));
119119

src/main/java/pro/cloudnode/smp/smpcore/Member.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public boolean isAlt() {
8181
while (rs.next()) alts.add(new Member(rs));
8282
}
8383
catch (final @NotNull SQLException e) {
84-
SMPCore.getInstance().getLogger().log(Level.SEVERE, "could not get alts for " + SMPCore.getName(player()), e);
84+
SMPCore.getInstance().getLogger().log(Level.SEVERE, "could not get alts for " + CachedProfile.getName(player()), e);
8585
}
8686

8787
return alts;
@@ -249,14 +249,14 @@ public static int count() {
249249

250250
public static @NotNull Set<@NotNull String> getNames() {
251251
return get().stream()
252-
.map(m -> SMPCore.getName(m.player()))
252+
.map(m -> CachedProfile.getName(m.player()))
253253
.collect(Collectors.toSet());
254254
}
255255

256256
public static @NotNull Set<@NotNull String> getAltNames() {
257257
return get().stream()
258258
.filter(Member::isAlt)
259-
.map(m -> SMPCore.getName(m.player()))
259+
.map(m -> CachedProfile.getName(m.player()))
260260
.collect(Collectors.toSet());
261261
}
262262

src/main/java/pro/cloudnode/smp/smpcore/Messages.java

Lines changed: 47 additions & 47 deletions
Large diffs are not rendered by default.

src/main/java/pro/cloudnode/smp/smpcore/REST.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private void e404 (final @NotNull io.javalin.http.Context ctx) {
2929
final @NotNull JsonObject obj = new JsonObject();
3030
final @NotNull OfflinePlayer player = member.player();
3131
obj.addProperty("uuid", member.uuid.toString());
32-
obj.addProperty("name", SMPCore.getName(player));
32+
obj.addProperty("name", CachedProfile.getName(player));
3333
obj.addProperty("nation", member.nationID);
3434
obj.addProperty("staff", member.staff);
3535
obj.addProperty("online", !member.staff && player.isOnline());
@@ -159,7 +159,7 @@ public REST(final int port) {
159159
final @NotNull JsonObject altObj = new JsonObject();
160160
final @NotNull OfflinePlayer player = alt.player();
161161
altObj.addProperty("uuid", alt.uuid.toString());
162-
altObj.addProperty("name", SMPCore.getName(player));
162+
altObj.addProperty("name", CachedProfile.getName(player));
163163
altObj.addProperty("nation", alt.nationID);
164164
altObj.addProperty("added", alt.added.getTime());
165165
altObj.addProperty("lastSeen", alt.staff ? 0 : player.getLastSeen());

src/main/java/pro/cloudnode/smp/smpcore/SMPCore.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package pro.cloudnode.smp.smpcore;
22

3-
import com.destroystokyo.paper.profile.PlayerProfile;
43
import com.zaxxer.hikari.HikariConfig;
54
import com.zaxxer.hikari.HikariDataSource;
65
import io.papermc.paper.util.Tick;
76
import net.kyori.adventure.text.Component;
87
import org.bukkit.Bukkit;
9-
import org.bukkit.OfflinePlayer;
108
import org.bukkit.World;
119
import org.bukkit.plugin.java.JavaPlugin;
1210
import org.jetbrains.annotations.NotNull;
@@ -247,26 +245,4 @@ public static boolean ifDisallowedCharacters(final @NotNull String source, final
247245
return new Date(ticks * 3600 + 21600000);
248246
}
249247

250-
@SuppressWarnings("ProfileCache")
251-
public static @NotNull String getName(final @NotNull OfflinePlayer player) {
252-
final @Nullable String name = player.getName();
253-
if (name != null)
254-
return name;
255-
256-
final PlayerProfile profile = player.getPlayerProfile();
257-
258-
if (profile.completeFromCache(true)) {
259-
final @Nullable String profileName = profile.getName();
260-
if (profileName != null && !profileName.isEmpty())
261-
return profileName;
262-
}
263-
264-
try {
265-
return CachedProfile.get(player).name();
266-
}
267-
catch (IllegalStateException e) {
268-
getInstance().getLogger().warning("Failed to fetch");
269-
return player.getUniqueId().toString();
270-
}
271-
}
272248
}

src/main/java/pro/cloudnode/smp/smpcore/command/BanCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public boolean run(@NotNull CommandSender sender, @NotNull String label, @NotNul
126126
if (args.length <= 1)
127127
return Arrays.stream(SMPCore.getInstance().getServer().getOfflinePlayers())
128128
.filter(p -> !p.isBanned())
129-
.map(SMPCore::getName)
129+
.map(CachedProfile::getName)
130130
.toList();
131131
return List.of();
132132
}

src/main/java/pro/cloudnode/smp/smpcore/command/MainCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ else switch (args[1]) {
7272
if (sender instanceof final @NotNull Player player) {
7373
final @NotNull Optional<@NotNull Member> member = Member.get(player);
7474
member.ifPresent(value -> suggestions.addAll(value.getAlts().stream()
75-
.map(m -> SMPCore.getName(m.player()))
75+
.map(m -> CachedProfile.getName(m.player()))
7676
.toList()));
7777
}
7878
}

src/main/java/pro/cloudnode/smp/smpcore/command/NationCommand.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ else switch (args[0]) {
160160
break;
161161
list.addAll(nation.get().citizens().stream()
162162
.filter(c -> !c.uuid.equals(nation.get().leaderUUID))
163-
.map(c -> SMPCore.getName(c.player()))
163+
.map(c -> CachedProfile.getName(c.player()))
164164
.toList()
165165
);
166166
}
@@ -172,7 +172,7 @@ else switch (args[0]) {
172172
list.addAll(nation.get().citizens().stream()
173173
.filter(c -> !(c.uuid.equals(nation.get().leaderUUID) ||
174174
c.uuid.equals(nation.get().viceLeaderUUID)))
175-
.map(c -> SMPCore.getName(c.player()))
175+
.map(c -> CachedProfile.getName(c.player()))
176176
.toList());
177177
}
178178
case "demote" -> {
@@ -184,7 +184,7 @@ else switch (args[0]) {
184184
if (vice.uuid.equals(nation.get().leaderUUID))
185185
break;
186186

187-
list.add(SMPCore.getName(vice.player()));
187+
list.add(CachedProfile.getName(vice.player()));
188188
}
189189
case "invite", "request", "req" -> {
190190
if (other && !sender.hasPermission(Permission.NATION_INVITE_OTHER))
@@ -193,7 +193,7 @@ else switch (args[0]) {
193193
break;
194194
list.addAll(Member.get().stream()
195195
.filter(m -> !nation.get().id.equals(m.nationID))
196-
.map(c -> SMPCore.getName(c.player()))
196+
.map(c -> CachedProfile.getName(c.player()))
197197
.toList());
198198
}
199199
case "cancel", "reject", "decline", "withdraw", "refuse", "deny" -> {
@@ -204,7 +204,7 @@ else switch (args[0]) {
204204
list.addAll(Stream.concat(
205205
CitizenRequest.get(nation.get(), true).stream(),
206206
CitizenRequest.get(nation.get(), false).stream()
207-
).map(req -> SMPCore.getName(req.member().player()))
207+
).map(req -> CachedProfile.getName(req.member().player()))
208208
.sorted()
209209
.toList());
210210
}
@@ -215,7 +215,7 @@ else switch (args[0]) {
215215
break;
216216
list.addAll(Member.get().stream()
217217
.filter(m -> !nation.get().id.equals(m.nationID))
218-
.map(c -> SMPCore.getName(c.player()))
218+
.map(c -> CachedProfile.getName(c.player()))
219219
.toList());
220220
}
221221
}

0 commit comments

Comments
 (0)