mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-17 11:05:06 +00:00
refactor: port multipart packet handlers
This commit is contained in:
1 parent
bed000cacf
commit
fdfd7763dd
10 files changed
+538
-257
No files matched your search
@@ -0,0 +1,88 @@
|
||||
package codechicken.multipart.handler;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.play.INetHandlerPlayClient;
|
||||
import net.minecraft.network.play.server.S40PacketDisconnect;
|
||||
import net.minecraft.util.ChatComponentText;
|
||||
import net.minecraft.util.ChatComponentTranslation;
|
||||
import net.minecraft.world.ChunkCoordIntPair;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import codechicken.lib.packet.PacketCustom;
|
||||
import codechicken.lib.packet.PacketCustom.IClientPacketHandler;
|
||||
import codechicken.lib.vec.BlockCoord;
|
||||
import codechicken.multipart.MultiPartRegistry;
|
||||
import codechicken.multipart.TileMultipart;
|
||||
import scala.MatchError;
|
||||
|
||||
public final class MultipartCPH$ extends MultipartPH implements IClientPacketHandler {
|
||||
|
||||
public static final MultipartCPH$ MODULE$ = new MultipartCPH$();
|
||||
|
||||
private MultipartCPH$() {}
|
||||
|
||||
@Override
|
||||
public void handlePacket(PacketCustom packet, Minecraft minecraft, INetHandlerPlayClient netHandler) {
|
||||
try {
|
||||
int type = packet.getType();
|
||||
switch (type) {
|
||||
case 1:
|
||||
handlePartRegistration(packet, netHandler);
|
||||
return;
|
||||
case 2:
|
||||
handleCompressedTileDesc(packet, minecraft.theWorld);
|
||||
return;
|
||||
case 3:
|
||||
handleCompressedTileData(packet, minecraft.theWorld);
|
||||
return;
|
||||
default:
|
||||
throw new MatchError(type);
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
if (e.getMessage() != null && e.getMessage().startsWith("DC: ")) {
|
||||
netHandler
|
||||
.handleDisconnect(new S40PacketDisconnect(new ChatComponentText(e.getMessage().substring(4))));
|
||||
return;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void handlePartRegistration(PacketCustom packet, INetHandlerPlayClient netHandler) {
|
||||
List<String> missing = MultiPartRegistry.readIDMap(packet);
|
||||
if (!missing.isEmpty()) {
|
||||
netHandler.handleDisconnect(
|
||||
new S40PacketDisconnect(
|
||||
new ChatComponentTranslation("multipart.missing", String.join(", ", missing))));
|
||||
}
|
||||
}
|
||||
|
||||
public void handleCompressedTileDesc(PacketCustom packet, World world) {
|
||||
ChunkCoordIntPair chunk = new ChunkCoordIntPair(packet.readInt(), packet.readInt());
|
||||
int count = packet.readUShort();
|
||||
for (int i = 0; i < count; i++) {
|
||||
TileMultipart.handleDescPacket(
|
||||
world,
|
||||
new BlockCoord(
|
||||
packet.readByte() + (chunk.chunkXPos << 4),
|
||||
packet.readInt(),
|
||||
packet.readByte() + (chunk.chunkZPos << 4)),
|
||||
packet);
|
||||
}
|
||||
}
|
||||
|
||||
public void handleCompressedTileData(PacketCustom packet, World world) {
|
||||
int x = packet.readInt();
|
||||
while (x != Integer.MAX_VALUE) {
|
||||
BlockCoord position = new BlockCoord(x, packet.readInt(), packet.readInt());
|
||||
int part = packet.readUByte();
|
||||
while (part < 255) {
|
||||
TileMultipart.handlePacket(position, world, part, packet);
|
||||
part = packet.readUByte();
|
||||
}
|
||||
x = packet.readInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package codechicken.multipart.handler;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.play.INetHandlerPlayClient;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import codechicken.lib.packet.PacketCustom;
|
||||
|
||||
public final class MultipartCPH {
|
||||
|
||||
private MultipartCPH() {}
|
||||
|
||||
public static MultipartMod$ channel() {
|
||||
return MultipartCPH$.MODULE$.channel();
|
||||
}
|
||||
|
||||
public static String registryChannel() {
|
||||
return MultipartCPH$.MODULE$.registryChannel();
|
||||
}
|
||||
|
||||
public static void handlePacket(PacketCustom packet, Minecraft minecraft, INetHandlerPlayClient netHandler) {
|
||||
MultipartCPH$.MODULE$.handlePacket(packet, minecraft, netHandler);
|
||||
}
|
||||
|
||||
public static void handlePartRegistration(PacketCustom packet, INetHandlerPlayClient netHandler) {
|
||||
MultipartCPH$.MODULE$.handlePartRegistration(packet, netHandler);
|
||||
}
|
||||
|
||||
public static void handleCompressedTileDesc(PacketCustom packet, World world) {
|
||||
MultipartCPH$.MODULE$.handleCompressedTileDesc(packet, world);
|
||||
}
|
||||
|
||||
public static void handleCompressedTileData(PacketCustom packet, World world) {
|
||||
MultipartCPH$.MODULE$.handleCompressedTileData(packet, world);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package codechicken.multipart.handler;
|
||||
|
||||
public class MultipartPH {
|
||||
|
||||
private final MultipartMod$ channel = MultipartMod$.MODULE$;
|
||||
private final String registryChannel = "ForgeMultipart";
|
||||
|
||||
public MultipartMod$ channel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public String registryChannel() {
|
||||
return registryChannel;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package codechicken.multipart.handler;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.network.NetHandlerPlayServer;
|
||||
import net.minecraft.network.play.INetHandlerPlayServer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.ChunkCoordIntPair;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
|
||||
import codechicken.lib.packet.PacketCustom;
|
||||
import codechicken.lib.packet.PacketCustom.IHandshakeHandler;
|
||||
import codechicken.lib.packet.PacketCustom.IServerPacketHandler;
|
||||
import codechicken.lib.vec.BlockCoord;
|
||||
import codechicken.multipart.ControlKeyModifer;
|
||||
import codechicken.multipart.MultiPartRegistry;
|
||||
import codechicken.multipart.PacketScheduler;
|
||||
import codechicken.multipart.TileMultipart;
|
||||
import scala.MatchError;
|
||||
import scala.Option;
|
||||
import scala.Tuple2;
|
||||
import scala.collection.mutable.HashMap;
|
||||
import scala.collection.mutable.HashSet;
|
||||
import scala.collection.mutable.Map;
|
||||
import scala.collection.mutable.Set;
|
||||
|
||||
public final class MultipartSPH$ extends MultipartPH implements IServerPacketHandler, IHandshakeHandler {
|
||||
|
||||
public static final MultipartSPH$ MODULE$ = new MultipartSPH$();
|
||||
|
||||
private final Map<World, Map<BlockCoord, MultipartSPH.MCByteStream>> codechicken$multipart$handler$MultipartSPH$$updateMap = new HashMap<>();
|
||||
private final HashMap<Object, Set<ChunkCoordIntPair>> codechicken$multipart$handler$MultipartSPH$$chunkWatchers = new HashMap<>();
|
||||
private final Map<Object, LinkedList<ChunkCoordIntPair>> codechicken$multipart$handler$MultipartSPH$$newWatchers = new HashMap<>();
|
||||
|
||||
private MultipartSPH$() {}
|
||||
|
||||
public Map<World, Map<BlockCoord, MultipartSPH.MCByteStream>> codechicken$multipart$handler$MultipartSPH$$updateMap() {
|
||||
return codechicken$multipart$handler$MultipartSPH$$updateMap;
|
||||
}
|
||||
|
||||
public HashMap<Object, Set<ChunkCoordIntPair>> codechicken$multipart$handler$MultipartSPH$$chunkWatchers() {
|
||||
return codechicken$multipart$handler$MultipartSPH$$chunkWatchers;
|
||||
}
|
||||
|
||||
public Map<Object, LinkedList<ChunkCoordIntPair>> codechicken$multipart$handler$MultipartSPH$$newWatchers() {
|
||||
return codechicken$multipart$handler$MultipartSPH$$newWatchers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePacket(PacketCustom packet, EntityPlayerMP sender, INetHandlerPlayServer netHandler) {
|
||||
int type = packet.getType();
|
||||
if (type == 1) {
|
||||
ControlKeyModifer.map().put(sender, packet.readBoolean());
|
||||
return;
|
||||
}
|
||||
throw new MatchError(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handshakeRecieved(NetHandlerPlayServer netHandler) {
|
||||
PacketCustom packet = new PacketCustom(registryChannel(), 1);
|
||||
MultiPartRegistry.writeIDMap(packet);
|
||||
netHandler.sendPacket(packet.toPacket());
|
||||
}
|
||||
|
||||
public void onWorldUnload(World world) {
|
||||
if (!world.isRemote) {
|
||||
codechicken$multipart$handler$MultipartSPH$$updateMap.remove(world);
|
||||
}
|
||||
}
|
||||
|
||||
public MultipartSPH.MCByteStream getTileStream(World world, BlockCoord position) {
|
||||
Option<Map<BlockCoord, MultipartSPH.MCByteStream>> worldOption = codechicken$multipart$handler$MultipartSPH$$updateMap
|
||||
.get(world);
|
||||
Map<BlockCoord, MultipartSPH.MCByteStream> worldUpdates;
|
||||
if (worldOption.isEmpty()) {
|
||||
if (world.isRemote) {
|
||||
throw new IllegalArgumentException("Cannot use MultipartSPH on a client world");
|
||||
}
|
||||
worldUpdates = new HashMap<>();
|
||||
codechicken$multipart$handler$MultipartSPH$$updateMap.put(world, worldUpdates);
|
||||
} else {
|
||||
worldUpdates = worldOption.get();
|
||||
}
|
||||
|
||||
Option<MultipartSPH.MCByteStream> streamOption = worldUpdates.get(position);
|
||||
if (!streamOption.isEmpty()) {
|
||||
return streamOption.get();
|
||||
}
|
||||
|
||||
MultipartSPH.MCByteStream stream = new MultipartSPH.MCByteStream(new ByteArrayOutputStream());
|
||||
stream.writeCoord(position);
|
||||
worldUpdates.put(position, stream);
|
||||
return stream;
|
||||
}
|
||||
|
||||
public void onTickEnd(scala.collection.Seq<EntityPlayerMP> players) {
|
||||
PacketScheduler.sendScheduled();
|
||||
|
||||
scala.collection.Iterator<EntityPlayerMP> playerIterator = players.iterator();
|
||||
while (playerIterator.hasNext()) {
|
||||
EntityPlayerMP player = playerIterator.next();
|
||||
Object playerId = player.getEntityId();
|
||||
Option<Set<ChunkCoordIntPair>> chunkOption = codechicken$multipart$handler$MultipartSPH$$chunkWatchers
|
||||
.get(playerId);
|
||||
if (chunkOption.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Option<Map<BlockCoord, MultipartSPH.MCByteStream>> updatesOption = codechicken$multipart$handler$MultipartSPH$$updateMap
|
||||
.get(player.worldObj);
|
||||
if (updatesOption.isEmpty() || updatesOption.get().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Set<ChunkCoordIntPair> chunks = chunkOption.get();
|
||||
PacketCustom packet = new PacketCustom(channel(), 3).compress();
|
||||
boolean send = false;
|
||||
scala.collection.Iterator<Tuple2<BlockCoord, MultipartSPH.MCByteStream>> updateIterator = updatesOption
|
||||
.get().iterator();
|
||||
while (updateIterator.hasNext()) {
|
||||
Tuple2<BlockCoord, MultipartSPH.MCByteStream> update = updateIterator.next();
|
||||
BlockCoord position = update._1();
|
||||
if (chunks.contains(new ChunkCoordIntPair(position.x >> 4, position.z >> 4))) {
|
||||
send = true;
|
||||
packet.writeByteArray(update._2().getBytes());
|
||||
packet.writeByte(255);
|
||||
}
|
||||
}
|
||||
if (send) {
|
||||
packet.writeInt(Integer.MAX_VALUE);
|
||||
packet.sendToPlayer(player);
|
||||
}
|
||||
}
|
||||
|
||||
scala.collection.Iterator<Tuple2<World, Map<BlockCoord, MultipartSPH.MCByteStream>>> worldIterator = codechicken$multipart$handler$MultipartSPH$$updateMap
|
||||
.iterator();
|
||||
while (worldIterator.hasNext()) {
|
||||
worldIterator.next()._2().clear();
|
||||
}
|
||||
|
||||
playerIterator = players.iterator();
|
||||
while (playerIterator.hasNext()) {
|
||||
EntityPlayerMP player = playerIterator.next();
|
||||
Object playerId = player.getEntityId();
|
||||
Option<LinkedList<ChunkCoordIntPair>> chunksOption = codechicken$multipart$handler$MultipartSPH$$newWatchers
|
||||
.get(playerId);
|
||||
if (chunksOption.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (ChunkCoordIntPair chunkPosition : chunksOption.get()) {
|
||||
Chunk chunk = player.worldObj.getChunkFromChunkCoords(chunkPosition.chunkXPos, chunkPosition.chunkZPos);
|
||||
@SuppressWarnings("unchecked")
|
||||
Iterator<TileEntity> tiles = ((java.util.Map<Object, TileEntity>) chunk.chunkTileEntityMap).values()
|
||||
.iterator();
|
||||
PacketCustom packet = getDescPacket(chunk, tiles);
|
||||
if (packet != null) {
|
||||
packet.sendToPlayer(player);
|
||||
}
|
||||
addChunkWatcher(playerId, chunkPosition);
|
||||
}
|
||||
}
|
||||
codechicken$multipart$handler$MultipartSPH$$newWatchers.clear();
|
||||
}
|
||||
|
||||
public void onChunkWatch(EntityPlayer player, ChunkCoordIntPair chunk) {
|
||||
Object playerId = player.getEntityId();
|
||||
Option<LinkedList<ChunkCoordIntPair>> option = codechicken$multipart$handler$MultipartSPH$$newWatchers
|
||||
.get(playerId);
|
||||
LinkedList<ChunkCoordIntPair> chunks;
|
||||
if (option.isEmpty()) {
|
||||
chunks = new LinkedList<>();
|
||||
codechicken$multipart$handler$MultipartSPH$$newWatchers.put(playerId, chunks);
|
||||
} else {
|
||||
chunks = option.get();
|
||||
}
|
||||
chunks.add(chunk);
|
||||
}
|
||||
|
||||
public void onChunkUnWatch(EntityPlayer player, ChunkCoordIntPair chunk) {
|
||||
Object playerId = player.getEntityId();
|
||||
Option<LinkedList<ChunkCoordIntPair>> pending = codechicken$multipart$handler$MultipartSPH$$newWatchers
|
||||
.get(playerId);
|
||||
if (!pending.isEmpty()) {
|
||||
pending.get().remove(chunk);
|
||||
}
|
||||
|
||||
Option<Set<ChunkCoordIntPair>> active = codechicken$multipart$handler$MultipartSPH$$chunkWatchers.get(playerId);
|
||||
if (!active.isEmpty()) {
|
||||
Set<ChunkCoordIntPair> chunks = active.get();
|
||||
chunks.remove(chunk);
|
||||
if (chunks.isEmpty()) {
|
||||
codechicken$multipart$handler$MultipartSPH$$chunkWatchers.remove(playerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public PacketCustom getDescPacket(Chunk chunk, Iterator<TileEntity> tiles) {
|
||||
MultipartSPH.MCByteStream stream = new MultipartSPH.MCByteStream(new ByteArrayOutputStream());
|
||||
int count = 0;
|
||||
while (tiles.hasNext()) {
|
||||
TileEntity tile = tiles.next();
|
||||
if (tile instanceof TileMultipart) {
|
||||
stream.writeByte(tile.xCoord & 0xF);
|
||||
stream.writeInt(tile.yCoord);
|
||||
stream.writeByte(tile.zCoord & 0xF);
|
||||
((TileMultipart) tile).writeDesc(stream);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count == 0) {
|
||||
return null;
|
||||
}
|
||||
return new PacketCustom(channel(), 2).compress().writeInt(chunk.xPosition).writeInt(chunk.zPosition)
|
||||
.writeShort(count).writeByteArray(stream.getBytes());
|
||||
}
|
||||
|
||||
private void addChunkWatcher(Object playerId, ChunkCoordIntPair chunk) {
|
||||
Option<Set<ChunkCoordIntPair>> option = codechicken$multipart$handler$MultipartSPH$$chunkWatchers.get(playerId);
|
||||
Set<ChunkCoordIntPair> chunks;
|
||||
if (option.isEmpty()) {
|
||||
chunks = new HashSet<>();
|
||||
codechicken$multipart$handler$MultipartSPH$$chunkWatchers.put(playerId, chunks);
|
||||
} else {
|
||||
chunks = option.get();
|
||||
}
|
||||
chunks.add(chunk);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package codechicken.multipart.handler;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.util.Iterator;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.network.NetHandlerPlayServer;
|
||||
import net.minecraft.network.play.INetHandlerPlayServer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.ChunkCoordIntPair;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
|
||||
import codechicken.lib.data.MCDataOutputWrapper;
|
||||
import codechicken.lib.packet.PacketCustom;
|
||||
import codechicken.lib.vec.BlockCoord;
|
||||
|
||||
public final class MultipartSPH {
|
||||
|
||||
private MultipartSPH() {}
|
||||
|
||||
public static class MCByteStream extends MCDataOutputWrapper {
|
||||
|
||||
private final ByteArrayOutputStream bout;
|
||||
|
||||
public MCByteStream(ByteArrayOutputStream bout) {
|
||||
super(new DataOutputStream(bout));
|
||||
this.bout = bout;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return bout.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
public static MultipartMod$ channel() {
|
||||
return MultipartSPH$.MODULE$.channel();
|
||||
}
|
||||
|
||||
public static String registryChannel() {
|
||||
return MultipartSPH$.MODULE$.registryChannel();
|
||||
}
|
||||
|
||||
public static void handlePacket(PacketCustom packet, EntityPlayerMP sender, INetHandlerPlayServer netHandler) {
|
||||
MultipartSPH$.MODULE$.handlePacket(packet, sender, netHandler);
|
||||
}
|
||||
|
||||
public static void handshakeRecieved(NetHandlerPlayServer netHandler) {
|
||||
MultipartSPH$.MODULE$.handshakeRecieved(netHandler);
|
||||
}
|
||||
|
||||
public static void onWorldUnload(World world) {
|
||||
MultipartSPH$.MODULE$.onWorldUnload(world);
|
||||
}
|
||||
|
||||
public static MCByteStream getTileStream(World world, BlockCoord position) {
|
||||
return MultipartSPH$.MODULE$.getTileStream(world, position);
|
||||
}
|
||||
|
||||
public static void onTickEnd(scala.collection.Seq<EntityPlayerMP> players) {
|
||||
MultipartSPH$.MODULE$.onTickEnd(players);
|
||||
}
|
||||
|
||||
public static void onChunkWatch(EntityPlayer player, ChunkCoordIntPair chunk) {
|
||||
MultipartSPH$.MODULE$.onChunkWatch(player, chunk);
|
||||
}
|
||||
|
||||
public static void onChunkUnWatch(EntityPlayer player, ChunkCoordIntPair chunk) {
|
||||
MultipartSPH$.MODULE$.onChunkUnWatch(player, chunk);
|
||||
}
|
||||
|
||||
public static PacketCustom getDescPacket(Chunk chunk, Iterator<TileEntity> tiles) {
|
||||
return MultipartSPH$.MODULE$.getDescPacket(chunk, tiles);
|
||||
}
|
||||
}
|
||||
@@ -1,243 +0,0 @@
|
||||
package codechicken.multipart.handler
|
||||
|
||||
import codechicken.lib.packet.PacketCustom.{
|
||||
IHandshakeHandler,
|
||||
IClientPacketHandler,
|
||||
IServerPacketHandler
|
||||
}
|
||||
import codechicken.lib.packet.PacketCustom
|
||||
import net.minecraft.client.Minecraft
|
||||
import codechicken.multipart.MultiPartRegistry
|
||||
import net.minecraft.entity.player.EntityPlayerMP
|
||||
import codechicken.multipart.ControlKeyModifer
|
||||
import net.minecraft.world.World
|
||||
import net.minecraft.world.chunk.Chunk
|
||||
import java.util.{Map => JMap}
|
||||
import java.util.Iterator
|
||||
import net.minecraft.tileentity.TileEntity
|
||||
import codechicken.multipart.TileMultipart
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import scala.collection.mutable.{Map, Set, HashMap, MultiMap}
|
||||
import codechicken.lib.vec.BlockCoord
|
||||
import codechicken.lib.data.MCDataOutputWrapper
|
||||
import java.io.DataOutputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import net.minecraft.world.ChunkCoordIntPair
|
||||
import codechicken.multipart.PacketScheduler
|
||||
import java.util.LinkedList
|
||||
import scala.collection.JavaConversions._
|
||||
import net.minecraft.network.play.{INetHandlerPlayServer, INetHandlerPlayClient}
|
||||
import net.minecraft.network.play.server.S40PacketDisconnect
|
||||
import net.minecraft.util.{ChatComponentText, ChatComponentTranslation}
|
||||
import net.minecraft.network.NetHandlerPlayServer
|
||||
|
||||
class MultipartPH {
|
||||
val channel = MultipartMod$.MODULE$
|
||||
val registryChannel = "ForgeMultipart"
|
||||
}
|
||||
|
||||
object MultipartCPH extends MultipartPH with IClientPacketHandler {
|
||||
def handlePacket(
|
||||
packet: PacketCustom,
|
||||
mc: Minecraft,
|
||||
netHandler: INetHandlerPlayClient
|
||||
) {
|
||||
try {
|
||||
packet.getType match {
|
||||
case 1 => handlePartRegistration(packet, netHandler)
|
||||
case 2 => handleCompressedTileDesc(packet, mc.theWorld)
|
||||
case 3 => handleCompressedTileData(packet, mc.theWorld)
|
||||
}
|
||||
} catch {
|
||||
case e: RuntimeException
|
||||
if e.getMessage != null && e.getMessage.startsWith("DC: ") =>
|
||||
netHandler.handleDisconnect(
|
||||
new S40PacketDisconnect(
|
||||
new ChatComponentText(e.getMessage.substring(4))
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
def handlePartRegistration(
|
||||
packet: PacketCustom,
|
||||
netHandler: INetHandlerPlayClient
|
||||
) {
|
||||
val missing = MultiPartRegistry.readIDMap(packet)
|
||||
if (!missing.isEmpty)
|
||||
netHandler.handleDisconnect(
|
||||
new S40PacketDisconnect(
|
||||
new ChatComponentTranslation(
|
||||
"multipart.missing",
|
||||
missing.mkString(", ")
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
def handleCompressedTileDesc(packet: PacketCustom, world: World) {
|
||||
val cc = new ChunkCoordIntPair(packet.readInt, packet.readInt)
|
||||
val num = packet.readUShort
|
||||
for (i <- 0 until num)
|
||||
TileMultipart.handleDescPacket(
|
||||
world,
|
||||
new BlockCoord(
|
||||
packet.readByte + (cc.chunkXPos << 4),
|
||||
packet.readInt,
|
||||
packet.readByte + (cc.chunkZPos << 4)
|
||||
),
|
||||
packet
|
||||
)
|
||||
}
|
||||
|
||||
def handleCompressedTileData(packet: PacketCustom, world: World) {
|
||||
var x = packet.readInt
|
||||
while (x != Int.MaxValue) {
|
||||
val pos = new BlockCoord(x, packet.readInt, packet.readInt)
|
||||
var i = packet.readUByte
|
||||
while (i < 255) {
|
||||
TileMultipart.handlePacket(pos, world, i, packet)
|
||||
i = packet.readUByte
|
||||
}
|
||||
x = packet.readInt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object MultipartSPH
|
||||
extends MultipartPH
|
||||
with IServerPacketHandler
|
||||
with IHandshakeHandler {
|
||||
class MCByteStream(bout: ByteArrayOutputStream)
|
||||
extends MCDataOutputWrapper(new DataOutputStream(bout)) {
|
||||
def getBytes = bout.toByteArray
|
||||
}
|
||||
|
||||
private val updateMap = Map[World, Map[BlockCoord, MCByteStream]]()
|
||||
|
||||
/** These maps are keyed by entityID so that new player instances with the
|
||||
* same entity id don't conflict world references
|
||||
*/
|
||||
private val chunkWatchers = new HashMap[Int, Set[ChunkCoordIntPair]]
|
||||
with MultiMap[Int, ChunkCoordIntPair]
|
||||
private val newWatchers = Map[Int, LinkedList[ChunkCoordIntPair]]()
|
||||
|
||||
def handlePacket(
|
||||
packet: PacketCustom,
|
||||
sender: EntityPlayerMP,
|
||||
netHandler: INetHandlerPlayServer
|
||||
) {
|
||||
packet.getType match {
|
||||
case 1 => ControlKeyModifer.map.put(sender, packet.readBoolean)
|
||||
}
|
||||
}
|
||||
|
||||
def handshakeRecieved(netHandler: NetHandlerPlayServer) {
|
||||
val packet = new PacketCustom(registryChannel, 1)
|
||||
MultiPartRegistry.writeIDMap(packet)
|
||||
netHandler.sendPacket(packet.toPacket)
|
||||
}
|
||||
|
||||
def onWorldUnload(world: World) {
|
||||
if (!world.isRemote)
|
||||
updateMap.remove(world)
|
||||
}
|
||||
|
||||
def getTileStream(world: World, pos: BlockCoord) =
|
||||
updateMap
|
||||
.getOrElseUpdate(
|
||||
world, {
|
||||
if (world.isRemote)
|
||||
throw new IllegalArgumentException(
|
||||
"Cannot use MultipartSPH on a client world"
|
||||
)
|
||||
Map()
|
||||
}
|
||||
)
|
||||
.getOrElseUpdate(
|
||||
pos, {
|
||||
val s = new MCByteStream(new ByteArrayOutputStream)
|
||||
s.writeCoord(pos)
|
||||
s
|
||||
}
|
||||
)
|
||||
|
||||
def onTickEnd(players: Seq[EntityPlayerMP]) {
|
||||
PacketScheduler.sendScheduled()
|
||||
|
||||
for (p <- players if chunkWatchers.containsKey(p.getEntityId)) {
|
||||
updateMap.get(p.worldObj) match {
|
||||
case Some(m) if !m.isEmpty =>
|
||||
val chunks = chunkWatchers(p.getEntityId)
|
||||
val packet = new PacketCustom(channel, 3).compress()
|
||||
var send = false
|
||||
for (
|
||||
(pos, stream) <- m
|
||||
if chunks(new ChunkCoordIntPair(pos.x >> 4, pos.z >> 4))
|
||||
) {
|
||||
send = true
|
||||
packet.writeByteArray(stream.getBytes)
|
||||
packet.writeByte(255) // terminator
|
||||
}
|
||||
if (send) {
|
||||
packet.writeInt(Int.MaxValue) // terminator
|
||||
packet.sendToPlayer(p)
|
||||
}
|
||||
case _ =>
|
||||
}
|
||||
}
|
||||
updateMap.foreach(_._2.clear())
|
||||
for (p <- players if newWatchers.containsKey(p.getEntityId)) {
|
||||
for (c <- newWatchers(p.getEntityId)) {
|
||||
val chunk = p.worldObj.getChunkFromChunkCoords(c.chunkXPos, c.chunkZPos)
|
||||
val pkt = getDescPacket(
|
||||
chunk,
|
||||
chunk.chunkTileEntityMap
|
||||
.asInstanceOf[JMap[_, TileEntity]]
|
||||
.values
|
||||
.iterator
|
||||
)
|
||||
if (pkt != null) pkt.sendToPlayer(p)
|
||||
chunkWatchers.addBinding(p.getEntityId, c)
|
||||
}
|
||||
}
|
||||
newWatchers.clear()
|
||||
}
|
||||
|
||||
def onChunkWatch(p: EntityPlayer, c: ChunkCoordIntPair) {
|
||||
newWatchers.getOrElseUpdate(p.getEntityId, new LinkedList).add(c)
|
||||
}
|
||||
|
||||
def onChunkUnWatch(p: EntityPlayer, c: ChunkCoordIntPair) {
|
||||
newWatchers.get(p.getEntityId) match {
|
||||
case Some(chunks) => chunks.remove(c)
|
||||
case _ =>
|
||||
}
|
||||
chunkWatchers.removeBinding(p.getEntityId, c)
|
||||
}
|
||||
|
||||
def getDescPacket(chunk: Chunk, it: Iterator[TileEntity]): PacketCustom = {
|
||||
val s = new MCByteStream(new ByteArrayOutputStream)
|
||||
|
||||
var num = 0
|
||||
while (it.hasNext) {
|
||||
val tile = it.next
|
||||
if (tile.isInstanceOf[TileMultipart]) {
|
||||
s.writeByte(tile.xCoord & 0xf)
|
||||
s.writeInt(tile.yCoord)
|
||||
s.writeByte(tile.zCoord & 0xf)
|
||||
tile.asInstanceOf[TileMultipart].writeDesc(s)
|
||||
num += 1
|
||||
}
|
||||
}
|
||||
if (num != 0) {
|
||||
return new PacketCustom(channel, 2)
|
||||
.compress()
|
||||
.writeInt(chunk.xPosition)
|
||||
.writeInt(chunk.zPosition)
|
||||
.writeShort(num)
|
||||
.writeByteArray(s.getBytes)
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user