Skip to content

Commit 98cf056

Browse files
committed
Replace username cache with squirrelid cache
1 parent 19a2b94 commit 98cf056

4 files changed

Lines changed: 76 additions & 61 deletions

File tree

realty-paper/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ dependencies {
1414
compileOnly("com.sk89q.worldguard:worldguard-bukkit:7.0.15") {
1515
exclude(group = "org.bukkit", module = "bukkit")
1616
}
17+
implementation("org.enginehub:squirrelid:0.3.2")
18+
implementation("org.xerial:sqlite-jdbc:3.46.1.0")
1719
compileOnly("net.essentialsx:EssentialsX:2.21.2") {
1820
exclude(group = "org.bukkit", module = "bukkit")
1921
exclude(group = "org.spigotmc", module = "spigot-api")
@@ -54,6 +56,9 @@ tasks {
5456
relocate("org.intellij.lang", "${base}.org.intellij.lang")
5557
relocate("net.kyori.option", "${base}.net.kyori.option")
5658
relocate("org.incendo.cloud", "${base}.org.incendo.cloud")
59+
relocate("org.enginehub.squirrelid", "${base}.org.enginehub.squirrelid")
60+
relocate("org.sqlite", "${base}.org.sqlite")
61+
mergeServiceFiles()
5762
}
5863

5964
processResources {

realty-paper/src/main/java/io/github/md5sha256/realty/Realty.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
import io.github.md5sha256.realty.util.DateFormatter;
7070
import io.github.md5sha256.realty.util.EssentialsNotificationService;
7171
import io.github.md5sha256.realty.util.EssentialsSafeBlockPredicate;
72-
import io.github.md5sha256.realty.util.PlayerNameCache;
7372
import io.github.md5sha256.realty.util.SimpleDateFormatSerializer;
73+
import io.github.md5sha256.realty.util.SquirrelIdUsernameResolver;
7474
import io.github.md5sha256.realty.util.TransientNotificationService;
7575
import io.papermc.paper.util.Tick;
7676
import net.kyori.adventure.text.Component;
@@ -124,7 +124,7 @@ public final class Realty extends JavaPlugin {
124124
private final AtomicReference<RealtyTags> realtyTags = new AtomicReference<>();
125125
private final RegionProfileService regionProfileService = new RegionProfileService(getLogger());
126126
private final SignCache signCache = new SignCache();
127-
private final PlayerNameCache nameCache = new PlayerNameCache(getServer());
127+
private SquirrelIdUsernameResolver nameResolver;
128128
private ExecutorState executorState;
129129
private RealtyBackend logic;
130130
private ProfileApplicator profileApplicator;
@@ -209,6 +209,16 @@ public void onEnable() {
209209
};
210210
this.executorState = new ExecutorState(getServer().getScheduler()
211211
.getMainThreadExecutor(this), Executors.newFixedThreadPool(4, threadFactory));
212+
try {
213+
this.nameResolver = new SquirrelIdUsernameResolver(
214+
new File(getDataFolder(), "profiles.sqlite"),
215+
this.executorState.dbExec());
216+
} catch (IOException ex) {
217+
getLogger().severe("Failed to initialize profile cache!");
218+
ex.printStackTrace();
219+
getServer().getPluginManager().disablePlugin(this);
220+
return;
221+
}
212222
MariaDatabase mariaDatabase = new MariaDatabase(this.databaseSettings, getLogger());
213223
this.database = mariaDatabase;
214224
try {
@@ -220,7 +230,7 @@ public void onEnable() {
220230
return;
221231
}
222232
this.logic = new RealtyBackendImpl(mariaDatabase,
223-
this.nameCache::getUsername,
233+
this.nameResolver::getUsername,
224234
dateTime -> DateFormatter.format(this.settings.get(), dateTime),
225235
() -> this.settings.get().offerPaymentDurationSeconds());
226236
var economyProvider = getServer().getServicesManager().getRegistration(Economy.class);

realty-paper/src/main/java/io/github/md5sha256/realty/util/PlayerNameCache.java

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.github.md5sha256.realty.util;
2+
3+
import org.enginehub.squirrelid.Profile;
4+
import org.enginehub.squirrelid.cache.ProfileCache;
5+
import org.enginehub.squirrelid.cache.SQLiteCache;
6+
import org.enginehub.squirrelid.resolver.CacheForwardingService;
7+
import org.enginehub.squirrelid.resolver.CombinedProfileService;
8+
import org.enginehub.squirrelid.resolver.HttpRepositoryService;
9+
import org.enginehub.squirrelid.resolver.PaperPlayerService;
10+
import org.enginehub.squirrelid.resolver.ProfileService;
11+
import org.jetbrains.annotations.NotNull;
12+
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.util.Optional;
16+
import java.util.UUID;
17+
import java.util.concurrent.CompletableFuture;
18+
import java.util.concurrent.Executor;
19+
20+
public class SquirrelIdUsernameResolver {
21+
22+
private final ProfileCache cache;
23+
private final ProfileService service;
24+
private final Executor asyncExecutor;
25+
26+
public SquirrelIdUsernameResolver(@NotNull File cacheFile,
27+
@NotNull Executor asyncExecutor) throws IOException {
28+
this.cache = new SQLiteCache(cacheFile);
29+
this.service = new CacheForwardingService(
30+
new CombinedProfileService(
31+
PaperPlayerService.getInstance(),
32+
HttpRepositoryService.forMinecraft()),
33+
this.cache);
34+
this.asyncExecutor = asyncExecutor;
35+
}
36+
37+
@NotNull
38+
public Optional<String> getUsernameIfCached(@NotNull UUID uuid) {
39+
Profile profile = this.cache.getIfPresent(uuid);
40+
return Optional.ofNullable(profile).map(Profile::getName);
41+
}
42+
43+
@NotNull
44+
public CompletableFuture<String> getUsername(@NotNull UUID uuid) {
45+
return CompletableFuture.supplyAsync(() -> {
46+
try {
47+
Profile profile = this.service.findByUuid(uuid);
48+
if (profile != null && profile.getName() != null) {
49+
return profile.getName();
50+
}
51+
return uuid.toString();
52+
} catch (Exception ex) {
53+
return uuid.toString();
54+
}
55+
}, this.asyncExecutor);
56+
}
57+
58+
}

0 commit comments

Comments
 (0)