9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-21 16:09:19 +00:00
Files
Leaf/patches/server/0082-Cache-player-profileResult.patch

110 lines
5.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Thu, 28 Mar 2024 13:36:09 -0400
Subject: [PATCH] Cache player profileResult
diff --git a/build.gradle.kts b/build.gradle.kts
index 6eaa616acb8c6f21c6554b0bead67ac7cf613933..370c43af11cd862a31a980549ad822cab2277c05 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -31,6 +31,10 @@ dependencies {
}
// Leaf end
+ // Leaf start - Libraries
+ implementation("com.github.ben-manes.caffeine:caffeine:3.1.8")
+ // Leaf end
+
// Paper start
implementation("com.github.luben:zstd-jni:1.5.5-11") // LinearPurpur
implementation("org.lz4:lz4-java:1.8.0") // LinearPurpur
diff --git a/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
index 0f3ba1abb609562e92dd9eb0ab7a621b8ed2d09f..910c8d2f32370c82c829535d4773fc7283c2e138 100644
--- a/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerLoginPacketListenerImpl.java
@@ -1,5 +1,7 @@
package net.minecraft.server.network;
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.primitives.Ints;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.exceptions.AuthenticationUnavailableException;
@@ -11,6 +13,7 @@ import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.security.PrivateKey;
import java.util.Objects;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
import javax.crypto.SecretKey;
@@ -67,6 +70,11 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener,
private ServerPlayer player; // CraftBukkit
public boolean iKnowThisMayNotBeTheBestIdeaButPleaseDisableUsernameValidation = false; // Paper - username validation overriding
private int velocityLoginMessageId = -1; // Paper - Add Velocity IP Forwarding Support
+ // Leaf start - Cache player profileResult
+ private final Cache<String, ProfileResult> playerProfileResultCahce = Caffeine.newBuilder()
+ .expireAfterWrite(org.dreeam.leaf.config.modules.misc.Cache.cachePlayerProfileResultTimeout, TimeUnit.MINUTES)
+ .build();
+ // Leaf end
public ServerLoginPacketListenerImpl(MinecraftServer server, Connection connection) {
this.state = ServerLoginPacketListenerImpl.State.HELLO;
@@ -272,7 +280,19 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener,
String s1 = (String) Objects.requireNonNull(ServerLoginPacketListenerImpl.this.requestedUsername, "Player name not initialized");
try {
- ProfileResult profileresult = ServerLoginPacketListenerImpl.this.server.getSessionService().hasJoinedServer(s1, s, this.getAddress());
+ // Leaf start - Cache player profileResult
+ ProfileResult profileresult;
+ if (org.dreeam.leaf.config.modules.misc.Cache.cachePlayerProfileResult) {
+ profileresult = playerProfileResultCahce.getIfPresent(s1);
+
+ if (profileresult == null) {
+ profileresult = ServerLoginPacketListenerImpl.this.server.getSessionService().hasJoinedServer(s1, s, this.getAddress());
+ playerProfileResultCahce.put(s1, profileresult);
+ }
+ } else {
+ profileresult = ServerLoginPacketListenerImpl.this.server.getSessionService().hasJoinedServer(s1, s, this.getAddress());
+ }
+ // Leaf end
if (profileresult != null) {
GameProfile gameprofile = profileresult.profile();
diff --git a/src/main/java/org/dreeam/leaf/config/modules/misc/Cache.java b/src/main/java/org/dreeam/leaf/config/modules/misc/Cache.java
new file mode 100644
index 0000000000000000000000000000000000000000..e6e0a193adb5465a24b40bda30c2b180bd93bc6e
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/config/modules/misc/Cache.java
@@ -0,0 +1,29 @@
+package org.dreeam.leaf.config.modules.misc;
+
+import org.dreeam.leaf.config.ConfigInfo;
+import org.dreeam.leaf.config.EnumConfigCategory;
+import org.dreeam.leaf.config.IConfigModule;
+
+public class Cache implements IConfigModule {
+
+ @Override
+ public EnumConfigCategory getCategory() {
+ return EnumConfigCategory.MISC;
+ }
+
+ @Override
+ public String getBaseName() {
+ return "cache";
+ }
+
+ @ConfigInfo(baseName = "cache-player-profile-result", comments = """
+ Cache the player profile result on they first join.
+ It's useful if Mojang's verification server is down.
+ """)
+ public static boolean cachePlayerProfileResult = true;
+
+ @ConfigInfo(baseName = "cache-player-profile-result-timeout", comments = """
+ The timeout of the cache. Unit: Minutes.
+ """)
+ public static int cachePlayerProfileResultTimeout = 1440;
+}