2020-05-06 11:48:49 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2018-08-11 14:46:46 +02:00
From: egg82 <phantom_zero@ymail.com>
Date: Tue, 7 Aug 2018 01:24:23 -0600
Subject: [PATCH] Use ConcurrentHashMap in JsonList
This is specifically aimed at fixing #471
Using a ConcurrentHashMap because thread safety
The performance benefit of Map over ConcurrentMap is negligabe at best in this scenaio, as most operations will be get and not add or remove
Even without considering the use-case the benefits are still negligable
Original ideas for the system included an expiration policy and/or handler
The simpler solution was to use a computeIfPresent in the get method
This will simultaneously have an O(1) lookup time and automatically expire any values
Since the get method (nor other similar methods) don't seem to have a critical need to flush the map to disk at any of these points further processing is simply wasteful
Meaning the original function expired values unrelated to the current value without actually having any explicit need to
The h method was heavily modified to be much more efficient in its processing
Also instead of being called on every get, it's now called just before a save
This will eliminate stale values being flushed to disk
Modified isEmpty to use the isEmpty() method instead of the slightly confusing size() < 1
The point of this is readability, but does have a side-benefit of a small microptimization
Finally, added a couple obfhelpers for the modified code
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/server/players/JsonList.java b/src/main/java/net/minecraft/server/players/JsonList.java
index 52256f72b00d3b868ef1a60e15a3836197c769d9..cd35b833d3047a38be980ee550641e87bd3b9b01 100644
--- a/src/main/java/net/minecraft/server/players/JsonList.java
+++ b/src/main/java/net/minecraft/server/players/JsonList.java
2020-06-25 16:09:55 +02:00
@@ -12,6 +12,8 @@ import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
+import java.lang.reflect.ParameterizedType; // Paper
+import java.lang.reflect.Type; // Paper
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Iterator;
2021-03-16 08:19:45 +01:00
@@ -28,7 +30,22 @@ public abstract class JsonList<K, V extends JsonListEntry<K>> {
2019-05-05 04:23:25 +02:00
protected static final Logger LOGGER = LogManager.getLogger();
2020-06-25 16:09:55 +02:00
private static final Gson b = (new GsonBuilder()).setPrettyPrinting().create();
2018-08-11 14:46:46 +02:00
private final File c;
- private final Map<String, V> d = Maps.newHashMap();
+ // Paper - replace HashMap is ConcurrentHashMap
+ private final Map<String, V> d = Maps.newConcurrentMap(); private final Map<String, V> getBackingMap() { return this.d; } // Paper - OBFHELPER
2020-06-25 16:09:55 +02:00
+ private boolean e = true;
+ private static final ParameterizedType f = new ParameterizedType() {
+ public Type[] getActualTypeArguments() {
+ return new Type[]{JsonListEntry.class};
+ }
+
+ public Type getRawType() {
+ return List.class;
+ }
+
+ public Type getOwnerType() {
+ return null;
+ }
+ };
public JsonList(File file) {
this.c = file;
2021-03-16 08:19:45 +01:00
@@ -51,8 +68,13 @@ public abstract class JsonList<K, V extends JsonListEntry<K>> {
2018-08-11 14:46:46 +02:00
2018-08-11 15:31:18 +02:00
@Nullable
2018-08-11 14:46:46 +02:00
public V get(K k0) {
2020-06-25 16:09:55 +02:00
- this.g();
2018-08-11 14:46:46 +02:00
- return (V) this.d.get(this.a(k0)); // CraftBukkit - fix decompile error
+ // Paper start
2020-06-25 16:09:55 +02:00
+ // this.g();
2018-08-11 14:46:46 +02:00
+ // return (V) this.d.get(this.a(k0)); // CraftBukkit - fix decompile error
+ return (V) this.getBackingMap().computeIfPresent(this.getMappingKey(k0), (k, v) -> {
+ return v.hasExpired() ? null : v;
+ });
+ // Paper end
}
public void remove(K k0) {
2021-03-16 08:19:45 +01:00
@@ -81,9 +103,11 @@ public abstract class JsonList<K, V extends JsonListEntry<K>> {
2018-08-11 14:46:46 +02:00
// CraftBukkit end
public boolean isEmpty() {
- return this.d.size() < 1;
+ // return this.d.size() < 1; // Paper
+ return this.getBackingMap().isEmpty(); // Paper - readability is the goal. As an aside, isEmpty() uses only sumCount() and a comparison. size() uses sumCount(), casts, and boolean logic
}
+ protected final String getMappingKey(K k0) { return a(k0); } // Paper - OBFHELPER
protected String a(K k0) {
return k0.toString();
}
2021-03-16 08:19:45 +01:00
@@ -92,8 +116,9 @@ public abstract class JsonList<K, V extends JsonListEntry<K>> {
2018-08-11 14:46:46 +02:00
return this.d.containsKey(this.a(k0));
}
2020-06-25 16:09:55 +02:00
+ private void removeStaleEntries() { g(); } // Paper - OBFHELPER
private void g() {
2019-01-01 04:15:55 +01:00
- List<K> list = Lists.newArrayList();
+ /*List<K> list = Lists.newArrayList();
2018-08-11 14:46:46 +02:00
Iterator iterator = this.d.values().iterator();
while (iterator.hasNext()) {
2021-03-16 08:19:45 +01:00
@@ -110,8 +135,10 @@ public abstract class JsonList<K, V extends JsonListEntry<K>> {
2019-01-01 04:15:55 +01:00
K k0 = (K) iterator.next(); // CraftBukkit - decompile error
2018-08-11 14:46:46 +02:00
2019-01-01 04:15:55 +01:00
this.d.remove(this.a(k0));
2018-08-11 14:46:46 +02:00
- }
+ }*/
2018-08-11 15:31:18 +02:00
+ this.getBackingMap().values().removeIf(JsonListEntry::hasExpired);
2018-08-11 14:46:46 +02:00
+ // Paper end
}
2020-06-25 16:09:55 +02:00
protected abstract JsonListEntry<K> a(JsonObject jsonobject);
2021-03-16 08:19:45 +01:00
@@ -121,6 +148,7 @@ public abstract class JsonList<K, V extends JsonListEntry<K>> {
2018-08-11 14:46:46 +02:00
}
public void save() throws IOException {
+ this.removeStaleEntries(); // Paper - remove expired values before saving
2020-06-25 16:09:55 +02:00
JsonArray jsonarray = new JsonArray();
this.d.values().stream().map((jsonlistentry) -> {
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
2021-03-16 14:04:28 +01:00
index 349bd74e7e4d1dea9587e55cb6afc1968628ca01..3aaa6313a33865b9823aa38e5d0cce7edce04592 100644
2021-03-16 08:19:45 +01:00
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
2021-03-16 14:04:28 +01:00
@@ -618,7 +618,7 @@ public abstract class PlayerList {
2020-06-28 10:43:47 +02:00
} else if (!this.isWhitelisted(gameprofile, event)) { // Paper
2021-02-21 20:45:33 +01:00
//chatmessage = new ChatMessage("multiplayer.disconnect.not_whitelisted"); // Paper
2020-05-17 05:26:49 +02:00
//event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, org.spigotmc.SpigotConfig.whitelistMessage); // Spigot // Paper - moved to isWhitelisted
- } else if (getIPBans().isBanned(socketaddress) && !getIPBans().get(socketaddress).hasExpired()) {
+ } else if (getIPBans().isBanned(socketaddress) && getIPBans().get(socketaddress) != null && !getIPBans().get(socketaddress).hasExpired()) { // Paper - fix NPE with temp ip bans
IpBanEntry ipbanentry = this.l.get(socketaddress);
chatmessage = new ChatMessage("multiplayer.disconnect.banned_ip.reason", new Object[]{ipbanentry.getReason()});