aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0356-Optimize-Server-World-Map.patch
blob: 9b62be86f269705a38ef5afd5fe074ac5efec36e (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
From 6c00757284ea40b18a629c0be9fa09042496f06d Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Mon, 17 Sep 2018 23:37:31 -0400
Subject: [PATCH] Optimize Server World Map

Minecraft moved worlds to a hashmap in 1.13.1.
This creates inconsistent order for iteration of the map.

This patch restores World management to be back as an Array.

.values() will allow us to iterate as it was pre 1.13.1 by
ArrayList, giving consistent ordering and effecient iteration performance.

KeySet and EntrySet iteration is proxied to the List iterator,
and should retain manipulation behavior but nothing should be doing that.

Getting a World by dimension ID is now back a constant time operation.

Hopefully no other plugins try to mess with this map, as we are only handling
known NMS used methods, but we can add more if naughty plugins are found later.

diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldMap.java b/src/main/java/com/destroystokyo/paper/PaperWorldMap.java
new file mode 100644
index 0000000000..af9e4455c6
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldMap.java
@@ -0,0 +1,191 @@
+package com.destroystokyo.paper;
+
+import net.minecraft.server.DimensionManager;
+import net.minecraft.server.WorldServer;
+
+import javax.annotation.Nonnull;
+import java.util.AbstractSet;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class PaperWorldMap extends HashMap<DimensionManager, WorldServer> {
+    private final List<WorldServer> worlds = new ArrayList<>();
+    private final List<WorldServer> worldsIterable = new ArrayList<WorldServer>() {
+        @Override
+        public Iterator<WorldServer> iterator() {
+            Iterator<WorldServer> iterator = super.iterator();
+            return new Iterator<WorldServer>() {
+                private WorldServer last;
+
+                @Override
+                public boolean hasNext() {
+                    return iterator.hasNext();
+                }
+
+                @Override
+                public WorldServer next() {
+                    this.last = iterator.next();
+                    return last;
+                }
+
+                @Override
+                public void remove() {
+                    worlds.set(last.dimension.getDimensionID()+1, null);
+                }
+            };
+        }
+    };
+    @Override
+    public int size() {
+        return worldsIterable.size();
+    }
+
+    @Override
+    public boolean isEmpty() {
+        return worldsIterable.isEmpty();
+    }
+
+    @Override
+    public WorldServer get(Object key) {
+        // Will hit the below method
+        return key instanceof DimensionManager ? get((DimensionManager) key) : null;
+    }
+
+    public WorldServer get(DimensionManager key) {
+        int id = key.getDimensionID()+1;
+        return worlds.size() > id ? worlds.get(id) : null;
+    }
+
+    @Override
+    public boolean containsKey(Object key) {
+        // will hit below method
+        return key instanceof DimensionManager && containsKey((DimensionManager) key);
+    }
+    public boolean containsKey(DimensionManager key) {
+        return get(key) != null;
+    }
+
+    @Override
+    public WorldServer put(DimensionManager key, WorldServer value) {
+        while (worlds.size() <= key.getDimensionID()+1) {
+            worlds.add(null);
+        }
+        WorldServer old = worlds.set(key.getDimensionID()+1, value);
+        if (old != null) {
+            worldsIterable.remove(old);
+        }
+        worldsIterable.add(value);
+        return old;
+    }
+
+    @Override
+    public void putAll(Map<? extends DimensionManager, ? extends WorldServer> m) {
+        for (Entry<? extends DimensionManager, ? extends WorldServer> e : m.entrySet()) {
+            put(e.getKey(), e.getValue());
+        }
+    }
+
+    @Override
+    public WorldServer remove(Object key) {
+        return key instanceof DimensionManager ? remove((DimensionManager) key) : null;
+    }
+
+    public WorldServer remove(DimensionManager key) {
+        WorldServer old;
+        if (key.getDimensionID()+1 == worlds.size() - 1) {
+            old = worlds.remove(key.getDimensionID()+1);
+        } else {
+            old = worlds.set(key.getDimensionID() + 1, null);
+        }
+        if (old != null) {
+            worldsIterable.remove(old);
+        }
+        return old;
+    }
+
+    @Override
+    public void clear() {
+        throw new RuntimeException("What the hell are you doing?");
+    }
+
+    @Override
+    public boolean containsValue(Object value) {
+        return value instanceof WorldServer && get(((WorldServer) value).dimension) != null;
+    }
+
+    @Nonnull
+    @Override
+    public Set<DimensionManager> keySet() {
+        return new AbstractSet<DimensionManager>() {
+            @Override
+            public Iterator<DimensionManager> iterator() {
+                Iterator<WorldServer> iterator = worldsIterable.iterator();
+                return new Iterator<DimensionManager>() {
+
+                    @Override
+                    public boolean hasNext() {
+                        return iterator.hasNext();
+                    }
+
+                    @Override
+                    public DimensionManager next() {
+                        return iterator.next().dimension;
+                    }
+
+                    @Override
+                    public void remove() {
+                        iterator.remove();
+                    }
+                };
+            }
+
+            @Override
+            public int size() {
+                return worlds.size();
+            }
+        };
+    }
+
+    @Override
+    public Collection<WorldServer> values() {
+        return worldsIterable;
+    }
+
+    @Override
+    public Set<Entry<DimensionManager, WorldServer>> entrySet() {
+        return new AbstractSet<Entry<DimensionManager, WorldServer>>() {
+            @Override
+            public Iterator<Entry<DimensionManager, WorldServer>> iterator() {
+                Iterator<WorldServer> iterator = worldsIterable.iterator();
+                return new Iterator<Entry<DimensionManager, WorldServer>>() {
+
+                    @Override
+                    public boolean hasNext() {
+                        return iterator.hasNext();
+                    }
+
+                    @Override
+                    public Entry<DimensionManager, WorldServer> next() {
+                        WorldServer entry = iterator.next();
+                        return new SimpleEntry<>(entry.dimension, entry);
+                    }
+
+                    @Override
+                    public void remove() {
+                        iterator.remove();
+                    }
+                };
+            }
+
+            @Override
+            public int size() {
+                return worldsIterable.size();
+            }
+        };
+    }
+}
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 4543ac8f15..d2ee4e5781 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -82,7 +82,7 @@ public abstract class MinecraftServer implements IAsyncTaskHandler, IMojangStati
     public final DataFixer dataConverterManager;
     private String serverIp;
     private int q = -1;
-    public final Map<DimensionManager, WorldServer> worldServer = Maps.newLinkedHashMap(); // CraftBukkit - keep order, k+v already use identity methods
+    public final Map<DimensionManager, WorldServer> worldServer = new com.destroystokyo.paper.PaperWorldMap(); // Paper;
     private PlayerList playerList;
     private boolean isRunning = true;
     private boolean isRestarting = false; // Paper - flag to signify we're attempting to restart
@@ -546,7 +546,7 @@ public abstract class MinecraftServer implements IAsyncTaskHandler, IMojangStati
             }
         }
 
-        for (WorldServer world : this.getWorlds()) {
+        for (WorldServer world : com.google.common.collect.Lists.newArrayList(this.getWorlds())) { // Paper - avoid como if 1 world triggers another world
             this.server.getPluginManager().callEvent(new org.bukkit.event.world.WorldLoadEvent(world.getWorld()));
         }
         // CraftBukkit end
-- 
2.21.0