Skip to content

Commit

Permalink
Massive native code overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
bergerkiller committed Dec 6, 2012
1 parent b352956 commit 3399b68
Show file tree
Hide file tree
Showing 24 changed files with 245 additions and 329 deletions.
31 changes: 13 additions & 18 deletions src/com/bergerkiller/bukkit/nolagg/chunks/ChunkCompressQueue.java
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand All @@ -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));
}
Expand All @@ -98,7 +96,7 @@ public boolean remove(int x, int z) {
Iterator<Chunk> 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;
}
Expand All @@ -108,7 +106,7 @@ public boolean remove(int x, int z) {
Iterator<ChunkSendCommand> 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;
}
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -160,18 +154,19 @@ 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();
}
}
}

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;
}
}
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -232,15 +236,15 @@ 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);

// send chunk through possible plugins
// ========================================
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();
Expand All @@ -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()) {
Expand Down
Expand Up @@ -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;

/*
Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 10 additions & 7 deletions src/com/bergerkiller/bukkit/nolagg/chunks/ChunkSendCommand.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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<TileEntity>) chunk.tileEntities.values()) {
for (TileEntity tile : (Collection<TileEntity>) 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);
}
});
}
Expand Down

0 comments on commit 3399b68

Please sign in to comment.