Last active
December 18, 2015 20:39
-
-
Save aadnk/5841715 to your computer and use it in GitHub Desktop.
Measure the size of map packets sent to every player.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.comphenix.example; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.util.ArrayDeque; | |
import java.util.Deque; | |
import net.minecraft.server.v1_5_R3.Packet; | |
import org.bukkit.Location; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.entity.Player; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import com.comphenix.protocol.Packets; | |
import com.comphenix.protocol.ProtocolLibrary; | |
import com.comphenix.protocol.events.ConnectionSide; | |
import com.comphenix.protocol.events.PacketAdapter; | |
import com.comphenix.protocol.events.PacketEvent; | |
import com.google.common.io.CountingOutputStream; | |
import com.google.common.io.NullOutputStream; | |
public class ExampleMod extends JavaPlugin { | |
private Deque<Location> undo = new ArrayDeque<Location>(); | |
@Override | |
public void onEnable() { | |
ProtocolLibrary.getProtocolManager().addPacketListener( | |
new PacketAdapter(this, ConnectionSide.SERVER_SIDE, Packets.Server.MAP_CHUNK, Packets.Server.MAP_CHUNK_BULK) { | |
@Override | |
public void onPacketSending(PacketEvent event) { | |
Packet packet = (Packet) event.getPacket().getHandle(); | |
CountingOutputStream counting = new CountingOutputStream(new NullOutputStream()); | |
// Figure out the size of the packet | |
try { | |
packet.a(new DataOutputStream(counting)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Size of " + packet + ": " + humanReadableByteCount(counting.getCount(), false)); | |
} | |
} | |
); | |
} | |
public static String humanReadableByteCount(long bytes, boolean si) { | |
int unit = si ? 1000 : 1024; | |
if (bytes < unit) return bytes + " B"; | |
int exp = (int) (Math.log(bytes) / Math.log(unit)); | |
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); | |
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment