diff options
Diffstat (limited to 'src/main/java/org')
6 files changed, 164 insertions, 112 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index f0d4b58a..45d74964 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -535,7 +535,7 @@ public final class CraftServer implements Server { internal.worldMaps = console.worlds.get(0).worldMaps; - internal.tracker = new EntityTracker(console, dimension); + internal.tracker = new EntityTracker(console, internal); // CraftBukkit internal.addIWorldAccess((IWorldAccess) new WorldManager(console, internal)); internal.difficulty = 1; internal.setSpawnFlags(true, true); diff --git a/src/main/java/org/bukkit/craftbukkit/util/EntryBase.java b/src/main/java/org/bukkit/craftbukkit/util/EntryBase.java new file mode 100644 index 00000000..1167ce93 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/util/EntryBase.java @@ -0,0 +1,8 @@ +package org.bukkit.craftbukkit.util; + +public class EntryBase { + protected long key; + public EntryBase(long key) { + this.key = key; + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongAbstractHashtable.java b/src/main/java/org/bukkit/craftbukkit/util/LongAbstractHashtable.java new file mode 100644 index 00000000..2da732cc --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/util/LongAbstractHashtable.java @@ -0,0 +1,115 @@ +package org.bukkit.craftbukkit.util; + +import net.minecraft.server.Chunk; +import net.minecraft.server.MinecraftServer; + +import java.util.ArrayList; + +import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf; + +public abstract class LongAbstractHashtable extends LongHash { + + EntryBase[][][] values = new EntryBase[256][][]; + EntryBase cache = null; + + public void put(int msw, int lsw, EntryBase entry) { + put(entry); + } + + public EntryBase getEntry(int msw, int lsw) { + return getEntry(toLong(msw, lsw)); + } + + public synchronized void put(EntryBase entry) { + int mainIdx = (int) (entry.key & 255); + EntryBase[][] outer = this.values[mainIdx]; + if (outer == null) this.values[mainIdx] = outer = new EntryBase[256][]; + + int outerIdx = (int) ((entry.key >> 32) & 255); + EntryBase[] inner = outer[outerIdx]; + + if (inner == null) { + outer[outerIdx] = inner = new EntryBase[5]; + inner[0] = this.cache = entry; + } else { + int i; + for (i = 0; i < inner.length; i++) { + if (inner[i] == null || inner[i].key == entry.key) { + inner[i] = this.cache = entry; + return; + } + } + + outer[outerIdx] = inner = Arrays_copyOf(inner, i + i); + inner[i] = entry; + } + } + + public synchronized EntryBase getEntry(long key) { + return containsKey(key) ? cache : null; + } + + public synchronized boolean containsKey(long key) { + if (this.cache != null && cache.key == key) return true; + + int outerIdx = (int) ((key >> 32) & 255); + EntryBase[][] outer = this.values[(int) (key & 255)]; + if (outer == null) return false; + + EntryBase[] inner = outer[outerIdx]; + if (inner == null) return false; + + for (int i = 0; i < inner.length; i++) { + EntryBase e = inner[i]; + if (e == null) { + return false; + } else if (e.key == key) { + this.cache = e; + return true; + } + } + return false; + } + + public synchronized void remove(long key) { + EntryBase[][] outer = this.values[(int) (key & 255)]; + if (outer == null) return; + + EntryBase[] inner = outer[(int) ((key >> 32) & 255)]; + if (inner == null) return; + + for (int i = 0; i < inner.length; i++) { + if (inner[i] == null) continue; + + if (inner[i].key == key) { + for (i++; i < inner.length; i++) { + if (inner[i] == null) break; + inner[i-1] = inner[i]; + } + + inner[i-1] = null; + this.cache = null; + return; + } + } + } + + public synchronized ArrayList<EntryBase> entries() { + ArrayList<EntryBase> ret = new ArrayList<EntryBase>(); + + for (EntryBase[][] outer: this.values) { + if (outer == null) continue; + + for (EntryBase[] inner: outer) { + if (inner == null) continue; + + for (EntryBase entry: inner) { + if (entry == null) break; + + ret.add(entry); + } + } + } + return ret; + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHash.java b/src/main/java/org/bukkit/craftbukkit/util/LongHash.java index f903f2ea..5cb09ed7 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHash.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHash.java @@ -1,15 +1,15 @@ package org.bukkit.craftbukkit.util; public abstract class LongHash { - static long toLong(int msw, int lsw) { + public static long toLong(int msw, int lsw) { return ((long) msw << 32) + lsw - Integer.MIN_VALUE; } - static int msw(long l) { + public static int msw(long l) { return (int) (l >> 32); } - static int lsw(long l) { + public static int lsw(long l) { return (int) (l & 0xFFFFFFFF) + Integer.MIN_VALUE; } diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java index 592f3254..0368aa6b 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java @@ -125,6 +125,31 @@ public class LongHashset extends LongHash { return 0; } + public long[] popAll() { + int index = 0; + rl.lock(); + try { + long[] ret = new long[this.count]; + for (long[][] outer: this.values) { + if (outer == null) continue; + + for (int oIdx = outer.length - 1; oIdx >= 0; oIdx--) { + long[] inner = outer[oIdx]; + if (inner == null) continue; + + for (long entry: inner) { + ret[index++] = entry; + } + outer[oIdx] = null; + } + } + count = 0; + return ret; + } finally { + rl.unlock(); + } + } + public long[] keys() { int index = 0; rl.lock(); diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java index b438e8fa..d3912e49 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java @@ -5,136 +5,40 @@ import net.minecraft.server.Chunk; import net.minecraft.server.MinecraftServer; import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf; -public class LongHashtable<V> extends LongHash { - Object[][][] values = new Object[256][][]; - Entry cache = null; +public class LongHashtable<V> extends LongAbstractHashtable { public void put(int msw, int lsw, V value) { put(toLong(msw, lsw), value); - if (value instanceof Chunk) { - Chunk c = (Chunk) value; - if (msw != c.x || lsw != c.z) { - MinecraftServer.log.info("Chunk (" + c.x + ", " + c.z + ") stored at (" + msw + ", " + lsw + ")"); - Throwable x = new Throwable(); - x.fillInStackTrace(); - x.printStackTrace(); - } - } } public V get(int msw, int lsw) { - V value = get(toLong(msw, lsw)); - if (value instanceof Chunk) { - Chunk c = (Chunk) value; - if (msw != c.x || lsw != c.z) { - MinecraftServer.log.info("Chunk (" + c.x + ", " + c.z + ") stored at (" + msw + ", " + lsw + ")"); - Throwable x = new Throwable(); - x.fillInStackTrace(); - x.printStackTrace(); - } - } - return value; + return get(toLong(msw, lsw)); } public synchronized void put(long key, V value) { - int mainIdx = (int) (key & 255); - Object[][] outer = this.values[mainIdx]; - if (outer == null) this.values[mainIdx] = outer = new Object[256][]; - - int outerIdx = (int) ((key >> 32) & 255); - Object[] inner = outer[outerIdx]; - - if (inner == null) { - outer[outerIdx] = inner = new Object[5]; - inner[0] = this.cache = new Entry(key, value); - } else { - int i; - for (i = 0; i < inner.length; i++) { - if (inner[i] == null || ((Entry) inner[i]).key == key) { - inner[i] = this.cache = new Entry(key, value); - return; - } - } - - outer[outerIdx] = inner = Arrays_copyOf(inner, i + i); - inner[i] = new Entry(key, value); - } + put(new Entry(key, value)); } public synchronized V get(long key) { - return containsKey(key) ? (V) cache.value : null; - } - - public synchronized boolean containsKey(long key) { - if (this.cache != null && cache.key == key) return true; - - int outerIdx = (int) ((key >> 32) & 255); - Object[][] outer = this.values[(int) (key & 255)]; - if (outer == null) return false; - - Object[] inner = outer[outerIdx]; - if (inner == null) return false; - - for (int i = 0; i < inner.length; i++) { - Entry e = (Entry) inner[i]; - if (e == null) { - return false; - } else if (e.key == key) { - this.cache = e; - return true; - } - } - return false; - } - - public synchronized void remove(long key) { - Object[][] outer = this.values[(int) (key & 255)]; - if (outer == null) return; - - Object[] inner = outer[(int) ((key >> 32) & 255)]; - if (inner == null) return; - - for (int i = 0; i < inner.length; i++) { - if (inner[i] == null) continue; - - if (((Entry) inner[i]).key == key) { - for (i++; i < inner.length; i++) { - if (inner[i] == null) break; - inner[i-1] = inner[i]; - } - - inner[i-1] = null; - this.cache = null; - return; - } - } + Entry entry = ((Entry)getEntry(key)); + return entry != null ? entry.value : null; } public synchronized ArrayList<V> values() { ArrayList<V> ret = new ArrayList<V>(); - for (Object[][] outer: this.values) { - if (outer == null) continue; - - for (Object[] inner: outer) { - if (inner == null) continue; + ArrayList<EntryBase> entries = entries(); - for (Object entry: inner) { - if (entry == null) break; - - ret.add((V) ((Entry) entry).value); - } - } + for(EntryBase entry : entries) { + ret.add(((Entry)entry).value); } return ret; } - private class Entry { - long key; - Object value; - - Entry(long k, Object v) { - this.key = k; + private class Entry extends EntryBase { + V value; + Entry(long k, V v) { + super(k); this.value = v; } } |