From 3399b68e0c7bb8c5e8127d8ebf43a07e361a7ec1 Mon Sep 17 00:00:00 2001 From: Irmo van den Berge Date: Thu, 6 Dec 2012 21:10:51 +0100 Subject: [PATCH] Massive native code overhaul --- .../nolagg/chunks/ChunkCompressQueue.java | 31 +++---- .../nolagg/chunks/ChunkCompressionThread.java | 18 ++-- .../nolagg/chunks/ChunkCoordComparator.java | 8 +- .../nolagg/chunks/ChunkSendCommand.java | 17 ++-- .../bukkit/nolagg/chunks/ChunkSendQueue.java | 87 ++++++++----------- .../nolagg/chunks/DynamicViewDistance.java | 28 ++---- .../bukkit/nolagg/chunks/NLCListener.java | 2 +- .../bukkit/nolagg/chunks/NoLaggChunks.java | 18 ++-- .../antiloader/DummyInstancePlayerList.java | 5 +- .../chunks/antiloader/DummyPlayerManager.java | 21 ++--- .../nolagg/chunks/antiloader/DummyWorld.java | 13 +-- .../bukkit/nolagg/examine/NoLaggExamine.java | 6 +- .../bukkit/nolagg/itembuffer/ChunkItems.java | 84 +++++++++--------- .../bukkit/nolagg/itembuffer/ItemMap.java | 58 +++++-------- .../bukkit/nolagg/itembuffer/NLIListener.java | 6 +- .../nolagg/itemstacker/StackingTask.java | 13 +-- .../nolagg/itemstacker/WorldStackFormer.java | 3 +- .../nolagg/lighting/LightingFixThread.java | 73 +++++++--------- .../nolagg/monitor/PerformanceMonitor.java | 8 +- .../bukkit/nolagg/saving/AutoSaveChanger.java | 27 +++--- .../spawnlimiter/EntitySpawnHandler.java | 13 ++- .../nolagg/spawnlimiter/NLSLListener.java | 3 +- .../bukkit/nolagg/tnt/TNTHandler.java | 8 +- .../server/TimedChunkProviderServer.java | 24 ++--- 24 files changed, 245 insertions(+), 329 deletions(-) diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCompressQueue.java b/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCompressQueue.java index a150ec9..318d843 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCompressQueue.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCompressQueue.java @@ -3,11 +3,12 @@ import java.util.Iterator; import java.util.LinkedList; +import org.bukkit.Chunk; import org.bukkit.entity.Player; import com.bergerkiller.bukkit.common.utils.CommonUtil; +import com.bergerkiller.bukkit.common.utils.NativeUtil; -import net.minecraft.server.Chunk; import net.minecraft.server.EntityPlayer; import net.minecraft.server.Packet51MapChunk; @@ -47,11 +48,8 @@ public void clear() { } public boolean isAlive() { - if (this.owner.ep.netServerHandler == null) - return false; - if (this.owner.ep.netServerHandler.disconnected) - return false; - return true; + EntityPlayer ep = NativeUtil.getNative(this.owner.player); + return ep.netServerHandler != null && !ep.netServerHandler.disconnected; } public boolean canSend() { @@ -77,7 +75,7 @@ public void enqueue(final Chunk chunk) { } } else { // Let the server itself deal with it - Packet51MapChunk packet = new Packet51MapChunk(chunk, true, 0xffff); + Packet51MapChunk packet = new Packet51MapChunk(NativeUtil.getNative(chunk), true, 0xffff); packet.lowPriority = true; this.enqueue(new ChunkSendCommand(packet, chunk)); } @@ -98,7 +96,7 @@ public boolean remove(int x, int z) { Iterator iter = this.toCompress.iterator(); while (iter.hasNext()) { Chunk chunk = iter.next(); - if (chunk.x == x && chunk.z == z) { + if (chunk.getX() == x && chunk.getZ() == z) { iter.remove(); return true; } @@ -108,7 +106,7 @@ public boolean remove(int x, int z) { Iterator iter = this.toSend.iterator(); while (iter.hasNext()) { ChunkSendCommand cmd = iter.next(); - if (cmd.chunk.x == x && cmd.chunk.z == z) { + if (cmd.chunk.getX() == x && cmd.chunk.getZ() == z) { iter.remove(); return true; } @@ -118,7 +116,7 @@ public boolean remove(int x, int z) { } public boolean isNear(Chunk chunk) { - return this.isNear(chunk.x, chunk.z); + return this.isNear(chunk.getX(), chunk.getZ()); } public boolean isNear(int x, int z) { @@ -127,12 +125,8 @@ public boolean isNear(int x, int z) { } } - public EntityPlayer nativeOwner() { - return this.owner.ep; - } - public Player owner() { - return (Player) this.owner.ep.getBukkitEntity(); + return this.owner.player; } public boolean hasChunk() { @@ -160,7 +154,7 @@ public ChunkSendCommand pollSendCommand() { // In range of dynamic view? return cmd; } else { - this.owner.removeContained(cmd.chunk.x, cmd.chunk.z); + this.owner.removeContained(cmd.chunk.getX(), cmd.chunk.getZ()); return this.pollSendCommand(); } } @@ -168,10 +162,11 @@ public ChunkSendCommand pollSendCommand() { public boolean sendNext() { ChunkSendCommand next = this.pollSendCommand(); - if (next == null) + if (next == null) { return false; + } next.send(this.owner); - this.owner.removeContained(next.chunk.x, next.chunk.z); + this.owner.removeContained(next.chunk.getX(), next.chunk.getZ()); return true; } } diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCompressionThread.java b/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCompressionThread.java index 1be9cc3..21bf16b 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCompressionThread.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCompressionThread.java @@ -4,13 +4,16 @@ import java.util.logging.Level; import java.util.zip.Deflater; +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; + import com.bergerkiller.bukkit.common.AsyncTask; import com.bergerkiller.bukkit.common.reflection.classes.Packet51MapChunkRef; +import com.bergerkiller.bukkit.common.utils.NativeUtil; import com.lishid.orebfuscator.obfuscation.Calculations; import net.minecraft.server.Chunk; import net.minecraft.server.ChunkSection; -import net.minecraft.server.EntityPlayer; import net.minecraft.server.Packet51MapChunk; public class ChunkCompressionThread extends AsyncTask { @@ -112,7 +115,8 @@ private void rawAppend(byte[] data) { this.rawLength += data.length; } - public Packet51MapChunk createPacket(Chunk chunk) { + public Packet51MapChunk createPacket(org.bukkit.Chunk bchunk) { + Chunk chunk = NativeUtil.getNative(bchunk); // Version which uses the Chunkmap buffer to create the packet Packet51MapChunk mapchunk = new Packet51MapChunk(); Packet51MapChunkRef.x.set(mapchunk, chunk.x); @@ -232,7 +236,7 @@ public static double getBusyPercentage(long timescale) { * @param chunk to get the data for * @return compressed packet of which the raw data can no longer be used */ - public Packet51MapChunk getCompressedPacket(EntityPlayer player, Chunk chunk) { + public Packet51MapChunk getCompressedPacket(Player player, org.bukkit.Chunk chunk) { this.lasttime = System.currentTimeMillis(); Packet51MapChunk mapchunk = createPacket(chunk); @@ -240,7 +244,7 @@ public Packet51MapChunk getCompressedPacket(EntityPlayer player, Chunk chunk) { // ======================================== if (NoLaggChunks.isOreObfEnabled) { try { - Calculations.Obfuscate(mapchunk, player.netServerHandler.getPlayer(), false); + Calculations.Obfuscate(mapchunk, (CraftPlayer) player, false); } catch (Throwable t) { NoLaggChunks.plugin.log(Level.SEVERE, "An error occured in Orebfuscator: support for this plugin had to be removed!"); t.printStackTrace(); @@ -264,12 +268,12 @@ public void run() { sleep(200); return; } - Chunk chunk = queue.pollChunk(); + org.bukkit.Chunk chunk = queue.pollChunk(); if (chunk != null) { try { - queue.enqueue(new ChunkSendCommand(this.getCompressedPacket(queue.nativeOwner(), chunk), chunk)); + queue.enqueue(new ChunkSendCommand(this.getCompressedPacket(queue.owner(), chunk), chunk)); } catch (Throwable t) { - NoLaggChunks.plugin.log(Level.SEVERE, "Failed to compress map chunk [" + chunk.x + ", " + chunk.z + "] for player " + queue.owner().getName()); + NoLaggChunks.plugin.log(Level.SEVERE, "Failed to compress map chunk [" + chunk.getX() + ", " + chunk.getZ() + "] for player " + queue.owner().getName()); t.printStackTrace(); } } else if (idleCounter++ > compress.size()) { diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCoordComparator.java b/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCoordComparator.java index 883fa74..534b336 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCoordComparator.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCoordComparator.java @@ -2,12 +2,12 @@ import java.util.Comparator; +import org.bukkit.Chunk; import org.bukkit.block.BlockFace; import com.bergerkiller.bukkit.common.utils.CommonUtil; import com.bergerkiller.bukkit.common.utils.FaceUtil; -import net.minecraft.server.Chunk; import net.minecraft.server.ChunkCoordIntPair; /* @@ -199,10 +199,10 @@ public int getIndex(Object coord) { return getIndex(pair.x, pair.z); } else if (coord instanceof Chunk) { Chunk chunk = (Chunk) coord; - return getIndex(chunk.x, chunk.z); + return getIndex(chunk.getX(), chunk.getZ()); } else if (coord instanceof ChunkSendCommand) { - Chunk chunk = (Chunk) ((ChunkSendCommand) coord).chunk; - return getIndex(chunk.x, chunk.z); + ChunkSendCommand cmd = (ChunkSendCommand) coord; + return getIndex(cmd.chunk.getX(), cmd.chunk.getZ()); } else { return Integer.MAX_VALUE; } diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkSendCommand.java b/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkSendCommand.java index b5de78c..1cdd400 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkSendCommand.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkSendCommand.java @@ -2,12 +2,14 @@ import java.util.Collection; +import org.bukkit.Chunk; + import com.bergerkiller.bukkit.common.utils.BlockUtil; import com.bergerkiller.bukkit.common.utils.CommonUtil; +import com.bergerkiller.bukkit.common.utils.NativeUtil; import com.bergerkiller.bukkit.common.utils.PacketUtil; import com.bergerkiller.bukkit.common.utils.WorldUtil; -import net.minecraft.server.Chunk; import net.minecraft.server.ChunkCoordIntPair; import net.minecraft.server.Packet; import net.minecraft.server.Packet51MapChunk; @@ -35,20 +37,21 @@ public static void send(final ChunkSendQueue queue, final Packet51MapChunk mapPa if (mapPacket == null) { return; } - queue.sentChunks.add(new ChunkCoordIntPair(chunk.x, chunk.z)); - PacketUtil.sendPacket(queue.ep, mapPacket, !NoLaggChunks.useBufferedLoading); - chunk.seenByPlayer = true; + final net.minecraft.server.Chunk nativeChunk = NativeUtil.getNative(chunk); + queue.sentChunks.add(new ChunkCoordIntPair(chunk.getX(), chunk.getZ())); + PacketUtil.sendPacket(queue.player, mapPacket, !NoLaggChunks.useBufferedLoading); + nativeChunk.seenByPlayer = true; // Tile entities Packet p; - for (TileEntity tile : (Collection) chunk.tileEntities.values()) { + for (TileEntity tile : (Collection) nativeChunk.tileEntities.values()) { if ((p = BlockUtil.getUpdatePacket(tile)) != null) { - PacketUtil.sendPacket(queue.ep, p); + PacketUtil.sendPacket(queue.player, p); } } // Spawn messages CommonUtil.nextTick(new Runnable() { public void run() { - WorldUtil.getTracker(queue.ep.world).a(queue.ep, chunk); + WorldUtil.getTracker(queue.player.getWorld()).a(NativeUtil.getNative(queue.player), nativeChunk); } }); } diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkSendQueue.java b/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkSendQueue.java index d373a63..f9fa9f2 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkSendQueue.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/ChunkSendQueue.java @@ -7,13 +7,11 @@ import org.bukkit.ChatColor; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; -import org.bukkit.craftbukkit.entity.CraftPlayer; - import com.bergerkiller.bukkit.common.IntRemainder; -import com.bergerkiller.bukkit.common.Operation; import com.bergerkiller.bukkit.common.reflection.SafeField; import com.bergerkiller.bukkit.common.Task; import com.bergerkiller.bukkit.common.reflection.classes.EntityPlayerRef; @@ -22,6 +20,7 @@ import com.bergerkiller.bukkit.common.utils.EntityUtil; import com.bergerkiller.bukkit.common.utils.FaceUtil; import com.bergerkiller.bukkit.common.utils.MathUtil; +import com.bergerkiller.bukkit.common.utils.NativeUtil; import com.bergerkiller.bukkit.common.utils.WorldUtil; import com.bergerkiller.bukkit.nolagg.NoLagg; import com.bergerkiller.bukkit.nolagg.NoLaggComponents; @@ -32,8 +31,6 @@ import net.minecraft.server.INetworkManager; import net.minecraft.server.NetworkManager; import net.minecraft.server.Packet; -import net.minecraft.server.World; -import net.minecraft.server.WorldServer; @SuppressWarnings({ "rawtypes", "unchecked" }) public class ChunkSendQueue extends ChunkSendQueueBase { @@ -55,8 +52,8 @@ public void run() { double newper = ChunkCompressionThread.getBusyPercentage(System.currentTimeMillis() - prevtime); compressBusyPercentage = MathUtil.useOld(compressBusyPercentage, newper * 100.0, 0.1); prevtime = System.currentTimeMillis(); - for (EntityPlayer ep : CommonUtil.getOnlinePlayers()) { - ChunkSendQueue queue = bind(ep); + for (Player player : CommonUtil.getOnlinePlayers()) { + ChunkSendQueue queue = bind(player); queue.updating.next(true); queue.update(); queue.updating.reset(false); @@ -84,34 +81,25 @@ public static void deinit() { Task.stop(task); task = null; // clear bound queues - new Operation() { - public void run() { - this.doPlayers(); - } - - public void handle(EntityPlayer ep) { - ChunkSendQueue queue = bind(ep); - if (queue != null) { - EntityPlayerRef.chunkQueue.set(ep, queue.toLinkedList()); - } + for (Player player : CommonUtil.getOnlinePlayers()) { + ChunkSendQueue queue = bind(player); + if (queue != null) { + EntityPlayerRef.chunkQueue.set(NativeUtil.getNative(player), queue.toLinkedList()); } - }; + } } public static ChunkSendQueue bind(Player with) { - return bind(((CraftPlayer) with).getHandle()); - } - - public static ChunkSendQueue bind(EntityPlayer with) { - if (!(with.chunkCoordIntPairQueue instanceof ChunkSendQueue)) { + EntityPlayer ep = NativeUtil.getNative(with); + if (!(ep.chunkCoordIntPairQueue instanceof ChunkSendQueue)) { ChunkSendQueue queue = new ChunkSendQueue(with); - with.chunkCoordIntPairQueue.clear(); - EntityPlayerRef.chunkQueue.set(with, queue); + ep.chunkCoordIntPairQueue.clear(); + EntityPlayerRef.chunkQueue.set(ep, queue); } - return (ChunkSendQueue) with.chunkCoordIntPairQueue; + return (ChunkSendQueue) ep.chunkCoordIntPairQueue; } - public final EntityPlayer ep; + public final Player player; private int idleTicks = 0; public BlockFace sendDirection = BlockFace.NORTH; public World world; @@ -129,10 +117,11 @@ public static ChunkSendQueue bind(EntityPlayer with) { private int packetBufferQueueSize = 0; private int buffersizeavg = 0; - private ChunkSendQueue(final EntityPlayer ep) { - this.ep = ep; - this.world = ep.world; + private ChunkSendQueue(final Player player) { + this.player = player; + this.world = player.getWorld(); this.sendDirection = null; // Force a sorting operation the next tick + EntityPlayer ep = NativeUtil.getNative(player); this.x = (int) (ep.locX + ep.motX * 16) >> 4; this.z = (int) (ep.locZ + ep.motZ * 16) >> 4; this.chunkQueue = new ChunkCompressQueue(this); @@ -143,7 +132,7 @@ private ChunkSendQueue(final EntityPlayer ep) { } private void enforceBufferFullSize() { - INetworkManager nm = this.ep.netServerHandler.networkManager; + INetworkManager nm = NativeUtil.getNative(this.player).netServerHandler.networkManager; Object lockObject = new SafeField(NetworkManager.class, "h").get(nm); if (lockObject != null) { List low = new SafeField>(NetworkManager.class, "lowPriorityQueue").get(nm); @@ -172,21 +161,13 @@ public int getCenterZ() { } public static double getAverageRate() { - return new Operation() { - public void run() { - this.doPlayers(); - this.average = this.totalrate / (double) pcount; - } - - private double totalrate = 0; - private int pcount = 0; - public double average; - - public void handle(EntityPlayer ep) { - this.totalrate += bind(ep).rate.get(); - this.pcount++; - } - }.average; + double totalrate = 0; + int pcount = 0; + for (Player player : CommonUtil.getOnlinePlayers()) { + totalrate += bind(player).rate.get(); + pcount++; + } + return totalrate / (double) pcount; } public double getRate() { @@ -239,7 +220,7 @@ public void sort(List elements) { private void update() { // Update queue size if (NetworkManagerRef.queueSize.isValid()) { - this.packetBufferQueueSize = (Integer) NetworkManagerRef.queueSize.get(this.ep.netServerHandler.networkManager); + this.packetBufferQueueSize = (Integer) NetworkManagerRef.queueSize.get(NativeUtil.getNative(this.player).netServerHandler.networkManager); this.packetBufferQueueSize += 9437184; } // Update current buffer size @@ -311,7 +292,8 @@ private void update(int interval, int rate) { if (rate == 0) return; if (this.intervalcounter >= interval) { - updatePosition(ep.world, ep.locX + ep.motX * 16, ep.locZ + ep.motZ * 16, ep.yaw); + EntityPlayer ep = NativeUtil.getNative(this.player); + updatePosition(this.player.getWorld(), ep.locX + ep.motX * 16, ep.locZ + ep.motZ * 16, ep.yaw); this.sendBatch(rate); this.intervalcounter = 1; } else { @@ -325,7 +307,7 @@ private void update(int interval, int rate) { * @param position to set to */ public void updatePosition(Location position) { - updatePosition(WorldUtil.getNative(position.getWorld()), position.getX(), position.getZ(), position.getYaw()); + updatePosition(position.getWorld(), position.getX(), position.getZ(), position.getYaw()); } /** @@ -356,8 +338,7 @@ public void updatePosition(World world, double locX, double locZ, float yaw) { * Prepares the given amount of chunks for sending and flushed compressed * chunks * - * @param count - * of chunks to load + * @param count of chunks to load */ private void sendBatch(int count) { // load chunks @@ -367,7 +348,7 @@ private void sendBatch(int count) { if (pair == null) { break; } - this.chunkQueue.enqueue(((WorldServer) this.ep.world).chunkProviderServer.getChunkAt(pair.x, pair.z)); + this.chunkQueue.enqueue(WorldUtil.getChunk(this.player.getWorld(), pair.x, pair.z)); } // Filter the chunk load times to prevent duplication in the examiner if (NoLaggComponents.EXAMINE.isEnabled()) { @@ -417,7 +398,7 @@ public boolean remove(ChunkCoordIntPair pair) { @Override public boolean isNear(final int chunkx, final int chunkz, final int view) { - return EntityUtil.isNearChunk(this.ep, chunkx, chunkz, view + 1); + return EntityUtil.isNearChunk(this.player, chunkx, chunkz, view + 1); } @Override diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/DynamicViewDistance.java b/src/com/bergerkiller/bukkit/nolagg/chunks/DynamicViewDistance.java index 122dd6b..512421e 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/DynamicViewDistance.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/DynamicViewDistance.java @@ -5,12 +5,12 @@ import java.util.List; import java.util.Map; -import net.minecraft.server.WorldServer; +import org.bukkit.World; -import com.bergerkiller.bukkit.common.Operation; import com.bergerkiller.bukkit.common.Task; import com.bergerkiller.bukkit.common.utils.CommonUtil; import com.bergerkiller.bukkit.common.utils.MathUtil; +import com.bergerkiller.bukkit.common.utils.WorldUtil; import com.bergerkiller.bukkit.nolagg.NoLagg; import com.bergerkiller.bukkit.nolagg.chunks.antiloader.DummyPlayerManager; @@ -43,15 +43,9 @@ public static void init(List elements) { } // Alter player manager to prevent chunk loading outside range - new Operation() { - public void run() { - this.doWorlds(); - } - - public void handle(WorldServer world) { - DummyPlayerManager.convert(world); - } - }; + for (World world : WorldUtil.getWorlds()) { + DummyPlayerManager.convert(world); + } int lowest = Integer.MAX_VALUE; Iterator iter = elements.iterator(); @@ -76,15 +70,9 @@ public void handle(WorldServer world) { } NoLaggChunks.hasDynamicView = true; chunks = 0; - new Operation() { - public void run() { - this.doWorlds(); - } - - public void handle(WorldServer world) { - chunks += world.chunkProviderServer.getLoadedChunks(); - } - }; + for (World world : WorldUtil.getWorlds()) { + chunks += WorldUtil.getChunks(world).size(); + } chunksChanged = true; task = new Task(NoLagg.plugin) { diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/NLCListener.java b/src/com/bergerkiller/bukkit/nolagg/chunks/NLCListener.java index f08d190..138c3ed 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/NLCListener.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/NLCListener.java @@ -72,7 +72,7 @@ public void onWorldUnload(WorldUnloadEvent event) { if (event.isCancelled() || !NoLaggChunks.useDynamicView) { return; } - int chunkCount = WorldUtil.getNative(event.getWorld()).chunkProviderServer.getLoadedChunks(); + int chunkCount = WorldUtil.getChunks(event.getWorld()).size(); for (int i = 0; i < chunkCount; i++) { DynamicViewDistance.removeChunk(); } diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/NoLaggChunks.java b/src/com/bergerkiller/bukkit/nolagg/chunks/NoLaggChunks.java index 66f63d9..242e8e2 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/NoLaggChunks.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/NoLaggChunks.java @@ -4,10 +4,9 @@ import java.util.Arrays; import java.util.logging.Level; -import net.minecraft.server.ChunkCoordinates; -import net.minecraft.server.WorldServer; - import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.world.ChunkUnloadEvent; @@ -109,15 +108,14 @@ public ChunkUnloadTask(JavaPlugin plugin) { } public void run() { - for (WorldServer world : WorldUtil.getWorlds()) { + for (World world : WorldUtil.getWorlds()) { ArrayList unloadChunks = new ArrayList(); - for (net.minecraft.server.Chunk nmschunk : WorldUtil.getChunks(world)) { - Chunk chunk = nmschunk.bukkitChunk; + Location spawn = world.getKeepSpawnInMemory() ? world.getSpawnLocation() : null; + for (Chunk chunk : WorldUtil.getChunks(world)) { // Part of world spawn? - if (nmschunk.world.keepSpawnInMemory) { - ChunkCoordinates chunkcoordinates = nmschunk.world.getSpawn(); - int centerSpawnX = nmschunk.x * 16 + 8 - chunkcoordinates.x; - int centerSpawnZ = nmschunk.z * 16 + 8 - chunkcoordinates.z; + if (spawn != null) { + int centerSpawnX = chunk.getX() * 16 + 8 - spawn.getBlockX(); + int centerSpawnZ = chunk.getZ() * 16 + 8 - spawn.getBlockZ(); final int short1 = 128; if (centerSpawnX >= -short1 && centerSpawnX <= short1 && centerSpawnZ >= -short1 && centerSpawnZ <= short1) { continue; diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyInstancePlayerList.java b/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyInstancePlayerList.java index 9c5c7db..d0bd9e3 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyInstancePlayerList.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyInstancePlayerList.java @@ -2,7 +2,10 @@ import java.util.ArrayList; +import org.bukkit.entity.Player; + import com.bergerkiller.bukkit.common.reflection.classes.PlayerInstanceRef; +import com.bergerkiller.bukkit.common.utils.NativeUtil; import com.bergerkiller.bukkit.nolagg.chunks.ChunkSendQueue; import net.minecraft.server.ChunkCoordIntPair; @@ -27,7 +30,7 @@ public static void replace(DummyPlayerManager playerManager, Object playerInstan @Override public boolean contains(Object o) { if (super.contains(o)) { - if (!FILTER || ChunkSendQueue.bind((EntityPlayer) o).preUnloadChunk(this.location)) { + if (!FILTER || ChunkSendQueue.bind((Player) NativeUtil.getEntity((EntityPlayer) o)).preUnloadChunk(this.location)) { return true; } diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyPlayerManager.java b/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyPlayerManager.java index 7907062..f048cf2 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyPlayerManager.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyPlayerManager.java @@ -4,10 +4,9 @@ import org.bukkit.World; -import com.bergerkiller.bukkit.common.Operation; import com.bergerkiller.bukkit.common.reflection.classes.PlayerManagerRef; import com.bergerkiller.bukkit.common.reflection.classes.WorldServerRef; -import com.bergerkiller.bukkit.common.utils.WorldUtil; +import com.bergerkiller.bukkit.common.utils.NativeUtil; import net.minecraft.server.ChunkCoordIntPair; import net.minecraft.server.EntityPlayer; @@ -23,22 +22,16 @@ public static void convert(WorldServer world) { } public static void convert(World world) { - convert(WorldUtil.getNative(world)); + convert(NativeUtil.getNative(world)); } public static void revert() { - new Operation() { - public void run() { - this.doWorlds(); + for (WorldServer world : NativeUtil.getWorlds()) { + PlayerManager manager = world.getPlayerManager(); + if (manager instanceof DummyPlayerManager) { + WorldServerRef.playerManager.set(world, ((DummyPlayerManager) manager).base); } - - public void handle(WorldServer world) { - PlayerManager manager = world.getPlayerManager(); - if (manager instanceof DummyPlayerManager) { - WorldServerRef.playerManager.set(world, ((DummyPlayerManager) manager).base); - } - } - }; + } } public final PlayerManager base; diff --git a/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyWorld.java b/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyWorld.java index 6de6f4c..acea166 100644 --- a/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyWorld.java +++ b/src/com/bergerkiller/bukkit/nolagg/chunks/antiloader/DummyWorld.java @@ -1,13 +1,10 @@ package com.bergerkiller.bukkit.nolagg.chunks.antiloader; import java.io.File; -import java.util.Iterator; -import java.util.Map; import java.util.UUID; import java.util.logging.Level; import org.bukkit.Bukkit; -import org.bukkit.World; import org.bukkit.World.Environment; import com.bergerkiller.bukkit.common.reflection.classes.WorldServerRef; @@ -115,15 +112,7 @@ public DummyWorld(String worldname) throws Throwable { super(CommonUtil.getMCServer(), getDummyDataManager(), worldname, 0, getDummySettings(), CommonUtil.getMCServer().methodProfiler, Environment.NORMAL, null); enabled = true; // dereference this dummy world again... - Map worlds = WorldUtil.getWorldsMap(); - Iterator iter = worlds.values().iterator(); - while (iter.hasNext()) { - if (iter.next() == this.getWorld()) { - iter.remove(); - } - } - worlds.remove(worldname.toLowerCase()); - WorldUtil.getWorlds().remove(this); + WorldUtil.removeWorld(this.getWorld()); // set some variables to null this.chunkProvider = this.chunkProviderServer = new DummyChunkProvider(this); this.generator = null; diff --git a/src/com/bergerkiller/bukkit/nolagg/examine/NoLaggExamine.java b/src/com/bergerkiller/bukkit/nolagg/examine/NoLaggExamine.java index 8900727..d2b09ff 100644 --- a/src/com/bergerkiller/bukkit/nolagg/examine/NoLaggExamine.java +++ b/src/com/bergerkiller/bukkit/nolagg/examine/NoLaggExamine.java @@ -1,9 +1,9 @@ package com.bergerkiller.bukkit.nolagg.examine; -import net.minecraft.server.WorldServer; import net.timedminecraft.server.TimedChunkProviderServer; import org.bukkit.ChatColor; +import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -24,7 +24,7 @@ public class NoLaggExamine extends NoLaggComponent { public void onEnable(ConfigurationNode config) { plugin = this; this.onReload(config); - for (WorldServer world : WorldUtil.getWorlds()) { + for (World world : WorldUtil.getWorlds()) { TimedChunkProviderServer.convert(world); } this.register(NLEListener.class); @@ -35,7 +35,7 @@ public void onEnable(ConfigurationNode config) { public void onDisable(ConfigurationNode config) { SchedulerWatcher.deinit(); PluginLogger.stopTask(); - for (WorldServer world : WorldUtil.getWorlds()) { + for (World world : WorldUtil.getWorlds()) { TimedChunkProviderServer.restore(world); } } diff --git a/src/com/bergerkiller/bukkit/nolagg/itembuffer/ChunkItems.java b/src/com/bergerkiller/bukkit/nolagg/itembuffer/ChunkItems.java index 3024a77..aa0a791 100644 --- a/src/com/bergerkiller/bukkit/nolagg/itembuffer/ChunkItems.java +++ b/src/com/bergerkiller/bukkit/nolagg/itembuffer/ChunkItems.java @@ -4,57 +4,59 @@ import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; -import java.util.List; import java.util.Queue; import java.util.Set; -import net.minecraft.server.Chunk; +import org.bukkit.World; +import org.bukkit.entity.Item; + import net.minecraft.server.ChunkCoordIntPair; -import net.minecraft.server.Entity; import net.minecraft.server.EntityItem; +import net.minecraft.server.WorldServer; import com.bergerkiller.bukkit.common.utils.EntityUtil; +import com.bergerkiller.bukkit.common.utils.NativeUtil; import com.bergerkiller.bukkit.common.utils.WorldUtil; public class ChunkItems { + public final Set spawnedItems = new HashSet(); + public final Queue hiddenItems = new LinkedList(); + public final org.bukkit.Chunk chunk; - public final Set spawnedItems = new HashSet(); - public final Queue hiddenItems = new LinkedList(); - public final Chunk chunk; - - public ChunkItems(org.bukkit.Chunk chunk) { - this(WorldUtil.getNative(chunk)); - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public ChunkItems(final Chunk chunk) { + public ChunkItems(final org.bukkit.Chunk chunk) { this.chunk = chunk; - for (List list : chunk.entitySlices) { - for (Entity entity : (List) list) { - if (entity instanceof EntityItem) { - if (entity.dead) - continue; - if (EntityUtil.isIgnored(entity.getBukkitEntity())) - continue; - if (this.spawnedItems.size() < NoLaggItemBuffer.maxItemsPerChunk) { - this.spawnedItems.add((EntityItem) entity); - } else { - this.hiddenItems.add((EntityItem) entity); - entity.dead = true; - } + for (org.bukkit.entity.Entity entity : WorldUtil.getEntities(chunk)) { + if (entity instanceof Item) { + if (entity.isDead() || EntityUtil.isIgnored(entity)) { + continue; + } + if (this.spawnedItems.size() < NoLaggItemBuffer.maxItemsPerChunk) { + this.spawnedItems.add((Item) entity); + } else { + this.hiddenItems.add((Item) entity); + entity.remove(); } } } } + public World getWorld() { + return chunk.getWorld(); + } + + private void restoreItem(Item bitem) { + ChunkCoordIntPair coord = ItemMap.getChunkCoords(bitem); + WorldServer world = NativeUtil.getNative(getWorld()); + world.getChunkAt(coord.x, coord.z); + EntityItem item = NativeUtil.getNative(bitem); + item.dead = false; + world.addEntity(item); + } + public synchronized void deinit() { if (!this.hiddenItems.isEmpty()) { - ChunkCoordIntPair coord; - for (EntityItem item : this.hiddenItems) { - item.dead = false; - coord = ItemMap.getChunkCoords(item); - this.chunk.world.getChunkAt(coord.x, coord.z); - this.chunk.world.addEntity(item); + for (Item item : this.hiddenItems) { + restoreItem(item); } this.hiddenItems.clear(); } @@ -62,7 +64,7 @@ public synchronized void deinit() { } public synchronized void clear(Set types) { - Iterator iter = this.hiddenItems.iterator(); + Iterator iter = this.hiddenItems.iterator(); while (iter.hasNext()) { if (types.contains(EntityUtil.getName(iter.next()))) { iter.remove(); @@ -76,11 +78,7 @@ public synchronized void clear() { public synchronized void spawnInChunk() { while (!hiddenItems.isEmpty() && spawnedItems.size() < NoLaggItemBuffer.maxItemsPerChunk) { - EntityItem item = hiddenItems.poll(); - ChunkCoordIntPair coord = ItemMap.getChunkCoords(item); - this.chunk.world.getChunkAt(coord.x, coord.z); // Load chunk - item.dead = false; - this.chunk.world.addEntity(item); + restoreItem(hiddenItems.poll()); } } @@ -93,7 +91,7 @@ public synchronized void update() { this.spawnInChunk(); } - public synchronized boolean handleSpawn(EntityItem item) { + public synchronized boolean handleSpawn(Item item) { if (this.spawnedItems.contains(item)) { return true; } @@ -117,18 +115,18 @@ public synchronized boolean handleSpawn(EntityItem item) { * @return True if a change occurred, False if not */ private boolean refreshSpawnedItems() { - Iterator iter = this.spawnedItems.iterator(); - EntityItem e; + Iterator iter = this.spawnedItems.iterator(); + Item e; ChunkCoordIntPair pair; try { while (iter.hasNext()) { e = iter.next(); - if (e.dead) { + if (e.isDead()) { iter.remove(); return true; // Changed } else { pair = ItemMap.getChunkCoords(e); - if (pair.x != this.chunk.x || pair.z != this.chunk.z) { + if (pair.x != this.chunk.getX() || pair.z != this.chunk.getZ()) { // respawn in correct chunk iter.remove(); ItemMap.addItem(pair, e); diff --git a/src/com/bergerkiller/bukkit/nolagg/itembuffer/ItemMap.java b/src/com/bergerkiller/bukkit/nolagg/itembuffer/ItemMap.java index f188814..25f415c 100644 --- a/src/com/bergerkiller/bukkit/nolagg/itembuffer/ItemMap.java +++ b/src/com/bergerkiller/bukkit/nolagg/itembuffer/ItemMap.java @@ -8,26 +8,25 @@ import org.bukkit.Bukkit; import org.bukkit.World; +import org.bukkit.entity.Item; -import com.bergerkiller.bukkit.common.Operation; import com.bergerkiller.bukkit.common.Task; +import com.bergerkiller.bukkit.common.utils.EntityUtil; import com.bergerkiller.bukkit.common.utils.MathUtil; import com.bergerkiller.bukkit.common.utils.WorldUtil; import com.bergerkiller.bukkit.nolagg.NoLagg; -import net.minecraft.server.Chunk; import net.minecraft.server.ChunkCoordIntPair; -import net.minecraft.server.EntityItem; public class ItemMap { - private static Map items = new WeakHashMap(); + private static Map items = new WeakHashMap(); private static Task updateTask; - public static ChunkCoordIntPair getChunkCoords(EntityItem item) { - return new ChunkCoordIntPair(MathUtil.locToChunk(item.locX), MathUtil.locToChunk(item.locZ)); + public static ChunkCoordIntPair getChunkCoords(Item item) { + return new ChunkCoordIntPair(MathUtil.locToChunk(EntityUtil.getLocX(item)), MathUtil.locToChunk(EntityUtil.getLocZ(item))); } - private static ChunkItems getItems(net.minecraft.server.World world, ChunkCoordIntPair chunkCoordinates) { + private static ChunkItems getItems(World world, ChunkCoordIntPair chunkCoordinates) { if (currentUnload != null && chunkCoordinates.x == currentUnload.x && chunkCoordinates.z == currentUnload.z) { return null; } @@ -48,14 +47,14 @@ public static void clear(Collection worlds, Set types) { if (types.contains("all") || types.contains("items")) { // Remove all from worlds for (ChunkItems ci : items.values()) { - if (worldsClone.contains(ci.chunk.world.getWorld())) { + if (worldsClone.contains(ci.getWorld())) { ci.clear(); } } } else { // Remove item per type for (ChunkItems ci : items.values()) { - if (worldsClone.contains(ci.chunk.world.getWorld())) { + if (worldsClone.contains(ci.getWorld())) { ci.clear(types); } } @@ -66,7 +65,7 @@ public static void clear(Collection worlds, Set types) { public static void clear(World world) { synchronized (items) { for (ChunkItems ci : items.values()) { - if (ci.chunk.world.getWorld() == world) { + if (ci.chunk.getWorld() == world) { ci.clear(); } } @@ -82,15 +81,11 @@ public static void clear() { } public static void init() { - new Operation() { - public void run() { - this.doChunks(); - } - - public void handle(Chunk chunk) { + for (World world : WorldUtil.getWorlds()) { + for (org.bukkit.Chunk chunk : WorldUtil.getChunks(world)) { loadChunk(chunk); } - }; + } updateTask = new Task(NoLagg.plugin) { public void run() { synchronized (items) { @@ -114,11 +109,7 @@ public static void deinit() { public static ChunkCoordIntPair currentUnload = null; public static void unloadChunk(org.bukkit.Chunk chunk) { - unloadChunk(WorldUtil.getNative(chunk)); - } - - public static void unloadChunk(Chunk chunk) { - currentUnload = new ChunkCoordIntPair(chunk.x, chunk.z); + currentUnload = new ChunkCoordIntPair(chunk.getX(), chunk.getZ()); ChunkItems citems = items.remove(chunk); if (citems != null) { citems.deinit(); @@ -127,25 +118,23 @@ public static void unloadChunk(Chunk chunk) { } public static void loadChunk(org.bukkit.Chunk chunk) { - loadChunk(WorldUtil.getNative(chunk)); - } - - public static void loadChunk(Chunk chunk) { synchronized (items) { items.put(chunk, new ChunkItems(chunk)); } } - public static boolean addItem(EntityItem item) { - if (item == null) + public static boolean addItem(Item item) { + if (item == null) { return true; + } return addItem(getChunkCoords(item), item); } - public static boolean addItem(ChunkCoordIntPair coords, EntityItem item) { - if (item == null) + public static boolean addItem(ChunkCoordIntPair coords, Item item) { + if (item == null) { return true; - ChunkItems citems = getItems(item.world, coords); + } + ChunkItems citems = getItems(item.getWorld(), coords); if (citems == null) { return true; } else { @@ -153,10 +142,11 @@ public static boolean addItem(ChunkCoordIntPair coords, EntityItem item) { } } - public static void removeItem(EntityItem item) { - if (item == null) + public static void removeItem(Item item) { + if (item == null) { return; - ChunkItems citems = getItems(item.world, getChunkCoords(item)); + } + ChunkItems citems = getItems(item.getWorld(), getChunkCoords(item)); if (citems != null) { citems.spawnedItems.remove(item); citems.spawnInChunk(); diff --git a/src/com/bergerkiller/bukkit/nolagg/itembuffer/NLIListener.java b/src/com/bergerkiller/bukkit/nolagg/itembuffer/NLIListener.java index a0dadf8..82a15b1 100644 --- a/src/com/bergerkiller/bukkit/nolagg/itembuffer/NLIListener.java +++ b/src/com/bergerkiller/bukkit/nolagg/itembuffer/NLIListener.java @@ -11,14 +11,12 @@ import org.bukkit.event.world.ChunkLoadEvent; import org.bukkit.event.world.ChunkUnloadEvent; -import com.bergerkiller.bukkit.common.utils.EntityUtil; - public class NLIListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onItemSpawn(ItemSpawnEvent event) { if (event.getEntityType() == EntityType.DROPPED_ITEM) { - if (!ItemMap.addItem(EntityUtil.getNative(event.getEntity()))) { + if (!ItemMap.addItem(event.getEntity())) { event.setCancelled(true); } } @@ -26,7 +24,7 @@ public void onItemSpawn(ItemSpawnEvent event) { public void onItemDespawn(Item item) { if (item.getType() == EntityType.DROPPED_ITEM) { - ItemMap.removeItem(EntityUtil.getNative(item)); + ItemMap.removeItem(item); } } diff --git a/src/com/bergerkiller/bukkit/nolagg/itemstacker/StackingTask.java b/src/com/bergerkiller/bukkit/nolagg/itemstacker/StackingTask.java index 26b3385..bec3644 100644 --- a/src/com/bergerkiller/bukkit/nolagg/itemstacker/StackingTask.java +++ b/src/com/bergerkiller/bukkit/nolagg/itemstacker/StackingTask.java @@ -5,14 +5,15 @@ import java.util.Iterator; import java.util.List; -import org.bukkit.entity.Entity; +import net.minecraft.server.Entity; + import org.bukkit.entity.Item; import org.bukkit.inventory.ItemStack; -import com.bergerkiller.bukkit.common.utils.EntityUtil; import com.bergerkiller.bukkit.common.utils.ItemUtil; +import com.bergerkiller.bukkit.common.utils.NativeUtil; -public class StackingTask { +public class StackingTask { private T entity; private List nearby = new ArrayList(0); @@ -91,7 +92,7 @@ public void fillNearby(List> Entitytasks, final double radiusSqu } T entity; double d; - net.minecraft.server.Entity selfEntity = EntityUtil.getNative(this.entity); + Entity selfEntity = NativeUtil.getNative(this.entity); for (StackingTask task : Entitytasks) { if (!task.isValid()) { break; // Reached end of data @@ -99,7 +100,7 @@ public void fillNearby(List> Entitytasks, final double radiusSqu entity = task.entity; if (!entity.isDead() && entity != this.entity) { // Distance check - net.minecraft.server.Entity e = EntityUtil.getNative(this.entity); + Entity e = NativeUtil.getNative(entity); d = distance(selfEntity.locX, e.locX); if (d > radiusSquared) { continue; @@ -146,7 +147,7 @@ private static double distance(final double d1, final double d2) { * @param entities to transfer * @param tasks to transfer the entities to */ - public static void transfer(Collection entities, Collection> tasks) { + public static void transfer(Collection entities, Collection> tasks) { // Ensure required tasks capacity if (entities.size() > tasks.size()) { for (int i = tasks.size(); i < entities.size(); i++) { diff --git a/src/com/bergerkiller/bukkit/nolagg/itemstacker/WorldStackFormer.java b/src/com/bergerkiller/bukkit/nolagg/itemstacker/WorldStackFormer.java index 4dbdfee..959c42a 100644 --- a/src/com/bergerkiller/bukkit/nolagg/itemstacker/WorldStackFormer.java +++ b/src/com/bergerkiller/bukkit/nolagg/itemstacker/WorldStackFormer.java @@ -7,7 +7,6 @@ import org.bukkit.entity.ExperienceOrb; import org.bukkit.entity.Item; -import com.bergerkiller.bukkit.common.utils.EntityUtil; import com.bergerkiller.bukkit.common.utils.ItemUtil; /** @@ -134,7 +133,7 @@ public void runSync() { } } if (changed) { - ItemUtil.respawnItem(EntityUtil.getNative(itemTask.getEntity())); + ItemUtil.respawnItem(itemTask.getEntity()); } } } diff --git a/src/com/bergerkiller/bukkit/nolagg/lighting/LightingFixThread.java b/src/com/bergerkiller/bukkit/nolagg/lighting/LightingFixThread.java index e8e5abd..424b7d5 100644 --- a/src/com/bergerkiller/bukkit/nolagg/lighting/LightingFixThread.java +++ b/src/com/bergerkiller/bukkit/nolagg/lighting/LightingFixThread.java @@ -20,22 +20,21 @@ import net.minecraft.server.EntityPlayer; import net.minecraft.server.EnumSkyBlock; import net.minecraft.server.RegionFile; -import net.minecraft.server.WorldServer; import com.bergerkiller.bukkit.common.AsyncTask; -import com.bergerkiller.bukkit.common.Operation; import com.bergerkiller.bukkit.common.Task; import com.bergerkiller.bukkit.common.reflection.classes.RegionFileCacheRef; import com.bergerkiller.bukkit.common.utils.CommonUtil; import com.bergerkiller.bukkit.common.utils.EntityUtil; import com.bergerkiller.bukkit.common.utils.MaterialUtil; import com.bergerkiller.bukkit.common.utils.MathUtil; +import com.bergerkiller.bukkit.common.utils.NativeUtil; import com.bergerkiller.bukkit.common.utils.WorldUtil; import com.bergerkiller.bukkit.nolagg.NoLagg; public class LightingFixThread extends AsyncTask { private static AsyncTask task; - private static LinkedHashSet toFix = new LinkedHashSet(); + private static LinkedHashSet toFix = new LinkedHashSet(); private static List next = new ArrayList(); private static LinkedList pendingChunks = new LinkedList(); private static int pending = 0; @@ -46,10 +45,6 @@ public static int getPendingSize() { } private static void addForFixing(org.bukkit.Chunk chunk) { - addForFixing(WorldUtil.getNative(chunk)); - } - - private static void addForFixing(Chunk chunk) { synchronized (toFix) { toFix.add(chunk); if (task == null) { @@ -57,10 +52,10 @@ private static void addForFixing(Chunk chunk) { } } // get rid of current sending requests for this chunk - ChunkCoordIntPair p = new ChunkCoordIntPair(chunk.x, chunk.z); - for (EntityPlayer ep : CommonUtil.getOnlinePlayers()) { - if (ep != null && ep.world == chunk.world && ep.chunkCoordIntPairQueue != null) { - if (EntityUtil.isNearChunk(ep, chunk.x, chunk.z, CommonUtil.view)) { + ChunkCoordIntPair p = new ChunkCoordIntPair(chunk.getX(), chunk.getZ()); + for (EntityPlayer ep : NativeUtil.getOnlinePlayers()) { + if (ep != null && ep.world.getWorld() == chunk.getWorld() && ep.chunkCoordIntPairQueue != null) { + if (EntityUtil.isNearChunk(ep, chunk.getX(), chunk.getZ(), CommonUtil.view)) { ep.chunkCoordIntPairQueue.remove(p); } } @@ -86,8 +81,8 @@ public static File getRegionFolder(World world) { public static void fix(World world) { LongHashSet chunks = new LongHashSet(); // Add the initial chunks that are already loaded - for (Chunk chunk : WorldUtil.getChunks(WorldUtil.getNative(world))) { - chunks.add(LongHash.toLong(chunk.x, chunk.z)); + for (org.bukkit.Chunk chunk : WorldUtil.getChunks(world)) { + chunks.add(LongHash.toLong(chunk.getX(), chunk.getZ())); } // Get the region folder to look in File regionFolder = getRegionFolder(world); @@ -177,10 +172,6 @@ public void run() { } public static boolean isFixing(org.bukkit.Chunk chunk) { - return isFixing(WorldUtil.getNative(chunk)); - } - - public static boolean isFixing(Chunk chunk) { synchronized (toFix) { return toFix.contains(chunk); } @@ -213,7 +204,7 @@ private static boolean executeAll() { synchronized (toFix) { if (toFix.isEmpty()) return false; - for (Chunk c : toFix) { + for (org.bukkit.Chunk c : toFix) { next.add(new FixOperation(c)); if (maxChunksPerRun-- <= 0) { break; @@ -282,17 +273,17 @@ public PendingChunk(World world, int x, int z) { private static class FixOperation { private final Chunk chunk; - private final WorldServer world; + private final World world; public final ChunkSection[] sections; - public FixOperation(final Chunk chunk) { - this.chunk = chunk; - this.world = (WorldServer) chunk.world; + public FixOperation(org.bukkit.Chunk chunk) { + this.chunk = NativeUtil.getNative(chunk); + this.world = chunk.getWorld(); this.sections = this.chunk.i(); } private Chunk getChunk(final int x, final int z) { - return WorldUtil.getChunk(this.world, x, z); + return NativeUtil.getNative(WorldUtil.getChunk(this.world, x, z)); } private int getLightLevel(EnumSkyBlock mode, int x, final int y, int z) { @@ -340,11 +331,11 @@ public boolean smooth(EnumSkyBlock mode) { for (z = 0; z < 16; z++) { if (mode == EnumSkyBlock.SKY) { inity = this.chunk.b(x, z); - if (inity >= this.world.getHeight()) { - inity = this.world.getHeight() - 1; + if (inity >= this.world.getMaxHeight()) { + inity = this.world.getMaxHeight() - 1; } } else { - inity = this.world.getHeight() - 1; + inity = this.world.getMaxHeight() - 1; } for (y = inity; y > 0; --y) { if (this.chunk.i()[y >> 4] == null) { @@ -391,7 +382,7 @@ public boolean smooth(EnumSkyBlock mode) { public void prepare() { int x, y, z; int slicesLight = this.chunk.h(); - int maxheight = this.world.getHeight() - 1; + int maxheight = this.world.getMaxHeight() - 1; ChunkSection sec; // initial calculation of sky light for (x = 0; x < 16; x++) { @@ -414,28 +405,26 @@ public void prepare() { } } + @SuppressWarnings("unchecked") public void finish() { // send chunk (update) - CommonUtil.nextTick(new Operation(false) { - boolean found = false; - + CommonUtil.nextTick(new Runnable() { + @Override public void run() { - this.doPlayers(world); + boolean found = false; + for (EntityPlayer ep : NativeUtil.getOnlinePlayers()) { + if (Math.abs(chunk.x - MathUtil.locToChunk(ep.locX)) > CommonUtil.view) + return; + if (Math.abs(chunk.z - MathUtil.locToChunk(ep.locZ)) > CommonUtil.view) + return; + ep.chunkCoordIntPairQueue.add(0, new ChunkCoordIntPair(chunk.x, chunk.z)); + found = true; + } if (!found) { // unload chunk if there were no players nearby - world.chunkProviderServer.queueUnload(chunk.x, chunk.z); + WorldUtil.setChunkUnloading(world, chunk.x, chunk.z, true); } } - - @SuppressWarnings("unchecked") - public void handle(EntityPlayer ep) { - if (Math.abs(chunk.x - MathUtil.locToChunk(ep.locX)) > CommonUtil.view) - return; - if (Math.abs(chunk.z - MathUtil.locToChunk(ep.locZ)) > CommonUtil.view) - return; - ep.chunkCoordIntPairQueue.add(0, new ChunkCoordIntPair(chunk.x, chunk.z)); - found = true; - } }); } } diff --git a/src/com/bergerkiller/bukkit/nolagg/monitor/PerformanceMonitor.java b/src/com/bergerkiller/bukkit/nolagg/monitor/PerformanceMonitor.java index e064219..368c2fe 100644 --- a/src/com/bergerkiller/bukkit/nolagg/monitor/PerformanceMonitor.java +++ b/src/com/bergerkiller/bukkit/nolagg/monitor/PerformanceMonitor.java @@ -10,8 +10,6 @@ import java.util.HashSet; import java.util.logging.Level; -import net.minecraft.server.WorldServer; - import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.World; @@ -329,11 +327,11 @@ public void run() { int totalchunkcount = 0; int totaluchunkcount = 0; - for (WorldServer world : WorldUtil.getWorlds()) { - int count = world.chunkProviderServer.getLoadedChunks(); + for (World world : WorldUtil.getWorlds()) { + int count = WorldUtil.getChunks(world).size(); totalchunkcount += count; totaluchunkcount += count; - if (world.keepSpawnInMemory) { + if (world.getKeepSpawnInMemory()) { totaluchunkcount -= 144; } } diff --git a/src/com/bergerkiller/bukkit/nolagg/saving/AutoSaveChanger.java b/src/com/bergerkiller/bukkit/nolagg/saving/AutoSaveChanger.java index 1537f90..7e21f3a 100644 --- a/src/com/bergerkiller/bukkit/nolagg/saving/AutoSaveChanger.java +++ b/src/com/bergerkiller/bukkit/nolagg/saving/AutoSaveChanger.java @@ -3,13 +3,14 @@ import java.util.LinkedList; import java.util.Queue; -import net.minecraft.server.Chunk; +import org.bukkit.World; + import net.minecraft.server.ServerConfigurationManager; -import net.minecraft.server.WorldServer; -import com.bergerkiller.bukkit.common.Operation; import com.bergerkiller.bukkit.common.Task; import com.bergerkiller.bukkit.common.utils.CommonUtil; +import com.bergerkiller.bukkit.common.utils.NativeUtil; +import com.bergerkiller.bukkit.common.utils.WorldUtil; import com.bergerkiller.bukkit.nolagg.NoLagg; public class AutoSaveChanger { @@ -31,24 +32,20 @@ public void run() { } if (!isSaving()) { // Obtain a list of all the chunks to save - final Queue chunks = new LinkedList(); - new Operation() { - public void run() { - this.doChunks(); - } - @Override - public void handle(Chunk chunk) { - if (chunk.a(false)) { + final Queue chunks = new LinkedList(); + for (World world : WorldUtil.getWorlds()) { + for (org.bukkit.Chunk chunk : WorldUtil.getChunks(world)) { + if (NativeUtil.getNative(chunk).a(false)) { chunks.offer(chunk); } } - }; + } final double total = chunks.size(); activeTask = new Task(NoLagg.plugin) { public void run() { for (int i = 0; i < NoLaggSaving.autoSaveBatch; i++) { - Chunk chunk = chunks.poll(); + org.bukkit.Chunk chunk = chunks.poll(); if (chunk == null) { Task.stop(activeTask); activeTask = null; @@ -56,8 +53,8 @@ public void run() { } else { SAVE_PERCENTAGE = (int) ((100.0 * (double) chunks.size()) / total); } - if (chunk.bukkitChunk != null && chunk.bukkitChunk.isLoaded()) { - ((WorldServer) chunk.world).chunkProviderServer.saveChunk(chunk); + if (chunk.isLoaded()) { + WorldUtil.saveChunk(chunk); } } } diff --git a/src/com/bergerkiller/bukkit/nolagg/spawnlimiter/EntitySpawnHandler.java b/src/com/bergerkiller/bukkit/nolagg/spawnlimiter/EntitySpawnHandler.java index d7277dd..a4a0239 100644 --- a/src/com/bergerkiller/bukkit/nolagg/spawnlimiter/EntitySpawnHandler.java +++ b/src/com/bergerkiller/bukkit/nolagg/spawnlimiter/EntitySpawnHandler.java @@ -2,8 +2,7 @@ import java.util.HashSet; -import net.minecraft.server.WorldServer; - +import org.bukkit.World; import org.bukkit.entity.Entity; import com.bergerkiller.bukkit.common.utils.EntityUtil; @@ -90,14 +89,14 @@ public static EntityLimit getLimits(Entity entity, boolean mobSpawnerSpawned) { * Fills all handlers with the entities on the server */ public static void initEntities() { - for (WorldServer world : WorldUtil.getWorlds()) { - for (net.minecraft.server.Entity e : WorldUtil.getEntities(world)) { - if (ExistingRemovalMap.isRemovable(EntityUtil.getName(e))) { + for (World world : WorldUtil.getWorlds()) { + for (Entity entity : WorldUtil.getEntities(world)) { + if (ExistingRemovalMap.isRemovable(EntityUtil.getName(entity))) { // Can remove it - remove if needed - addEntity(e.getBukkitEntity()); + addEntity(entity); } else { // Can not remove it - just increment the count - forceSpawn(e.getBukkitEntity()); + forceSpawn(entity); } } } diff --git a/src/com/bergerkiller/bukkit/nolagg/spawnlimiter/NLSLListener.java b/src/com/bergerkiller/bukkit/nolagg/spawnlimiter/NLSLListener.java index 434dfa4..58d70bb 100644 --- a/src/com/bergerkiller/bukkit/nolagg/spawnlimiter/NLSLListener.java +++ b/src/com/bergerkiller/bukkit/nolagg/spawnlimiter/NLSLListener.java @@ -18,6 +18,7 @@ import com.bergerkiller.bukkit.common.events.EntityRemoveEvent; import com.bergerkiller.bukkit.common.utils.BlockUtil; import com.bergerkiller.bukkit.common.utils.EntityUtil; +import com.bergerkiller.bukkit.common.utils.NativeUtil; import com.bergerkiller.bukkit.nolagg.NoLagg; import com.bergerkiller.bukkit.nolagg.spawnlimiter.limit.EntityLimit; @@ -73,7 +74,7 @@ public void onChunkLoad(ChunkLoadEvent event) { // If any of them are dead, revoke the dead status and force-add on the limit for (Entity e : event.getChunk().getEntities()) { if (e.isDead() && !ExistingRemovalMap.isRemovable(EntityUtil.getName(e))) { - EntityUtil.getNative(e).dead = false; + NativeUtil.getNative(e).dead = false; EntitySpawnHandler.forceSpawn(e); } } diff --git a/src/com/bergerkiller/bukkit/nolagg/tnt/TNTHandler.java b/src/com/bergerkiller/bukkit/nolagg/tnt/TNTHandler.java index 1e53ab2..aca6e3f 100644 --- a/src/com/bergerkiller/bukkit/nolagg/tnt/TNTHandler.java +++ b/src/com/bergerkiller/bukkit/nolagg/tnt/TNTHandler.java @@ -7,7 +7,6 @@ import java.util.Queue; import java.util.logging.Level; -import net.minecraft.server.Chunk; import net.minecraft.server.Packet60Explosion; import net.minecraft.server.WorldServer; @@ -27,7 +26,6 @@ import com.bergerkiller.bukkit.nolagg.NoLagg; public class TNTHandler { - private static Queue todo = new LinkedList(); private static BlockSet added = new BlockSet(); private static Task task; @@ -81,9 +79,9 @@ public void run() { } } if (isLoaded) { - Chunk chunk = WorldUtil.getNative(next.getChunk()); - if (chunk.getTypeId(x & 15, y, z & 15) == Material.TNT.getId()) { - chunk.world.setTypeId(x, y, z, 0); + org.bukkit.Chunk chunk = next.getChunk(); + if (WorldUtil.getBlockTypeId(chunk, x, y, z) == Material.TNT.getId()) { + WorldUtil.setBlock(chunk, x, y, z, 0, 0); TNTPrimed tnt = next.getWorld().spawn(next.getLocation().add(0.5, 0.5, 0.5), TNTPrimed.class); int fuse = tnt.getFuseTicks(); diff --git a/src/net/timedminecraft/server/TimedChunkProviderServer.java b/src/net/timedminecraft/server/TimedChunkProviderServer.java index c6e54bd..000b6f5 100644 --- a/src/net/timedminecraft/server/TimedChunkProviderServer.java +++ b/src/net/timedminecraft/server/TimedChunkProviderServer.java @@ -7,6 +7,7 @@ import org.bukkit.generator.BlockPopulator; import com.bergerkiller.bukkit.common.reflection.classes.ChunkProviderServerRef; +import com.bergerkiller.bukkit.common.utils.NativeUtil; import com.bergerkiller.bukkit.common.utils.WorldUtil; import com.bergerkiller.bukkit.nolagg.examine.PluginLogger; import com.bergerkiller.bukkit.nolagg.examine.TaskMeasurement; @@ -23,7 +24,8 @@ * To keep things fair, all rights for this Class go to the Mojang team */ public class TimedChunkProviderServer extends ChunkProviderServer { - public static void transfer(ChunkProviderServer to, WorldServer world) { + private static void transfer(ChunkProviderServer to, org.bukkit.World bworld) { + WorldServer world = NativeUtil.getNative(bworld); try { ChunkProviderServerRef.TEMPLATE.transfer(world.chunkProviderServer, to); if (world.chunkProviderServer instanceof TimedChunkProviderServer) { @@ -36,27 +38,19 @@ public static void transfer(ChunkProviderServer to, WorldServer world) { } public static void convert(org.bukkit.World world) { - convert(WorldUtil.getNative(world)); - } - - public static void convert(WorldServer world) { transfer(new TimedChunkProviderServer(world), world); } public static void restore(org.bukkit.World world) { - restore(WorldUtil.getNative(world)); - } - - public static void restore(WorldServer world) { - transfer(new ChunkProviderServer(world, null, null), world); + transfer(new ChunkProviderServer(NativeUtil.getNative(world), null, null), world); } private boolean enabled = true; private TaskMeasurement loadmeas, genmeas, unloadmeas; private long prevtime; - private TimedChunkProviderServer(WorldServer world) { - super(world, null, null); + private TimedChunkProviderServer(org.bukkit.World world) { + super(NativeUtil.getNative(world), null, null); loadmeas = PluginLogger.getServerOperation("Chunk provider", "Chunk load", "Loads chunks from file"); genmeas = PluginLogger.getServerOperation("Chunk provider", "Chunk generate", "Generates the basic terrain"); unloadmeas = PluginLogger.getServerOperation("Chunk provider", "Chunk unload", "Unloads chunks and saves them to file"); @@ -82,8 +76,8 @@ public Chunk loadChunk(int x, int z) { public Chunk getChunkAt(int i, int j) { if (isEnabled()) { // CraftBukkit start - WorldUtil.setChunkUnloading(this, i, j, false); - Chunk chunk = WorldUtil.getChunk(this, i, j); + WorldUtil.setChunkUnloading(this.world.getWorld(), i, j, false); + Chunk chunk = NativeUtil.getNative(WorldUtil.getChunk(this.world.getWorld(), i, j)); boolean newChunk = false; // CraftBukkit end @@ -100,7 +94,7 @@ public Chunk getChunkAt(int i, int j) { newChunk = true; // CraftBukkit } - WorldUtil.setChunk(this, i, j, chunk); + WorldUtil.setChunk(this.world.getWorld(), i, j, NativeUtil.getChunk(chunk)); if (chunk != null) { chunk.addEntities(); }