mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-25 22:44:27 +00:00
Optimized networking.
Sends a compressed packet containing the description of all tiles on chunk load. Uses position in chunk as an integer rather than world coords. Combines and compresses all updates packets (change, add, remove) for each player at the end of tick.
This commit is contained in:
1 parent
b067a08b34
commit
626a9ed50e
8 files changed
+191
-88
No files matched your search
@@ -1,7 +1,7 @@
|
||||
package codechicken.microblock
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import codechicken.core.lighting.LightMatrix;
|
||||
import codechicken.core.lighting.LightMatrix
|
||||
import codechicken.core.packet.PacketCustom
|
||||
import codechicken.microblock.MicroMaterialRegistry._
|
||||
import codechicken.core.vec.BlockCoord
|
||||
@@ -26,6 +26,8 @@ import scala.collection.mutable.ListBuffer
|
||||
import codechicken.multipart.TMultiPart
|
||||
import codechicken.scala.JSeq
|
||||
import codechicken.scala.ScalaBridge._
|
||||
import codechicken.core.data.MCDataOutput
|
||||
import codechicken.core.data.MCDataInput
|
||||
|
||||
object CommonMicroblock
|
||||
{
|
||||
@@ -213,13 +215,13 @@ abstract class Microblock(var shape:Byte = 0, var material:Int = 0) extends TCub
|
||||
return null//unreachable
|
||||
}
|
||||
|
||||
override def write(packet:PacketCustom)
|
||||
override def writeDesc(packet:MCDataOutput)
|
||||
{
|
||||
packet.writeByte(shape)
|
||||
writePartID(packet, material)
|
||||
}
|
||||
|
||||
override def read(packet:PacketCustom)
|
||||
override def readDesc(packet:MCDataInput)
|
||||
{
|
||||
shape = packet.readByte
|
||||
material = readPartID(packet)
|
||||
|
||||
@@ -39,7 +39,7 @@ class ExpandingPlacement($pos:BlockCoord, $part:Microblock, opart:Microblock) ex
|
||||
{
|
||||
opart.shape = part.shape
|
||||
opart.tile.notifyPartChange()
|
||||
opart.sendDescPacket()
|
||||
opart.sendDescUpdate()
|
||||
}
|
||||
|
||||
def consume(world:World, player:EntityPlayer, item:ItemStack)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package codechicken.multipart
|
||||
|
||||
import codechicken.core.packet.PacketCustom
|
||||
import codechicken.core.vec.Cuboid6
|
||||
import codechicken.core.vec.Vector3
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
@@ -23,6 +22,8 @@ import codechicken.core.render.CCRenderState
|
||||
import codechicken.scala.JSeq
|
||||
import codechicken.scala.ScalaBridge._
|
||||
import codechicken.core.lighting.LazyLightMatrix
|
||||
import codechicken.core.data.MCDataOutput
|
||||
import codechicken.core.data.MCDataInput
|
||||
|
||||
abstract class TMultiPart
|
||||
{
|
||||
@@ -57,8 +58,9 @@ abstract class TMultiPart
|
||||
@SideOnly(Side.CLIENT)
|
||||
def drawBreaking(renderBlocks:RenderBlocks){}
|
||||
|
||||
def read(packet:PacketCustom){}
|
||||
def write(packet:PacketCustom){}
|
||||
def read(packet:MCDataInput) = readDesc(packet)
|
||||
def readDesc(packet:MCDataInput){}
|
||||
def writeDesc(packet:MCDataOutput){}
|
||||
def save(tag:NBTTagCompound){}
|
||||
def load(tag:NBTTagCompound){}
|
||||
|
||||
@@ -72,7 +74,7 @@ abstract class TMultiPart
|
||||
def update(){}
|
||||
def pickItem(hit:MovingObjectPosition):ItemStack = null
|
||||
|
||||
def sendDescPacket() = tile.sendPartDesc(this)
|
||||
def sendDescUpdate() = writeDesc(tile.getWriteStream(this))
|
||||
}
|
||||
|
||||
trait TFacePart extends TMultiPart
|
||||
|
||||
@@ -35,6 +35,7 @@ import cpw.mods.fml.relauncher.Side
|
||||
import scala.collection.mutable.Queue
|
||||
import codechicken.multipart.handler.MultipartSPH
|
||||
import codechicken.core.lighting.LazyLightMatrix
|
||||
import net.minecraft.world.ChunkCoordIntPair
|
||||
|
||||
trait TileMultipart extends TileEntity
|
||||
{
|
||||
@@ -145,6 +146,10 @@ trait TileMultipart extends TileEntity
|
||||
return parts.forall(part => part.occlusionTest(npart) && npart.occlusionTest(part))
|
||||
}
|
||||
|
||||
def getWriteStream(part:TMultiPart):MCDataOutput = getWriteStream.writeByte(partList.indexOf(part))
|
||||
|
||||
private def getWriteStream = MultipartSPH.getTileStream(worldObj, new BlockCoord(this))
|
||||
|
||||
private[multipart] def addPart(part:TMultiPart)
|
||||
{
|
||||
addPart_do(part)
|
||||
@@ -154,19 +159,16 @@ trait TileMultipart extends TileEntity
|
||||
|
||||
if(!worldObj.isRemote)
|
||||
{
|
||||
val packet = new PacketCustom(MultipartSPH.channel, 3)
|
||||
packet.setChunkDataPacket()
|
||||
packet.writeCoord(xCoord, yCoord, zCoord)
|
||||
MultiPartRegistry.writePartID(packet, part)
|
||||
part.write(packet)
|
||||
packet.sendToChunk(worldObj, xCoord>>4, zCoord>>4)
|
||||
val stream = getWriteStream.writeByte(253)
|
||||
MultiPartRegistry.writePartID(stream, part)
|
||||
part.writeDesc(stream)
|
||||
}
|
||||
}
|
||||
|
||||
private[multipart] def addPart_do(part:TMultiPart)
|
||||
{
|
||||
if(partList.size >= 256)
|
||||
throw new IllegalArgumentException("Tried to add more than 256 parts to the one tile")
|
||||
if(partList.size >= 250)
|
||||
throw new IllegalArgumentException("Tried to add more than 250 parts to the one tile. You're doing it wrong")
|
||||
|
||||
partList+=part
|
||||
val mask = part.getSlotMask
|
||||
@@ -198,13 +200,7 @@ trait TileMultipart extends TileEntity
|
||||
}
|
||||
|
||||
if(!worldObj.isRemote)
|
||||
{
|
||||
val packet = new PacketCustom(MultipartSPH.channel, 4)
|
||||
packet.setChunkDataPacket()
|
||||
packet.writeCoord(xCoord, yCoord, zCoord)
|
||||
packet.writeByte(i)
|
||||
packet.sendToChunk(worldObj, xCoord>>4, zCoord>>4)
|
||||
}
|
||||
getWriteStream.writeByte(254).writeByte(i)
|
||||
|
||||
if(!isInvalid())
|
||||
return MultipartGenerator.partRemoved(this, part)
|
||||
@@ -259,29 +255,13 @@ trait TileMultipart extends TileEntity
|
||||
partList.clear()
|
||||
}
|
||||
|
||||
override def getDescriptionPacket():Packet =
|
||||
def writeDesc(packet:PacketCustom)
|
||||
{
|
||||
val packet = new PacketCustom(MultipartSPH.channel, 2);
|
||||
packet.setChunkDataPacket()
|
||||
|
||||
val superPacket = super.getDescriptionPacket
|
||||
var partCount = partList.size
|
||||
if(superPacket != null)
|
||||
partCount |= 0x80
|
||||
|
||||
packet.writeByte(partCount)
|
||||
if(superPacket != null)
|
||||
{
|
||||
packet.writeByte(superPacket.getPacketId)
|
||||
superPacket.writePacketData(new DataOutputStream(new MCDataOutputStream(packet)))
|
||||
}
|
||||
|
||||
packet.writeCoord(xCoord, yCoord, zCoord)
|
||||
packet.writeByte(partList.size)
|
||||
partList.foreach{part => {
|
||||
MultiPartRegistry.writePartID(packet, part)
|
||||
part.write(packet)
|
||||
part.writeDesc(packet)
|
||||
}}
|
||||
return packet.toPacket;
|
||||
}
|
||||
|
||||
def harvestPart(index:Int, drop:Boolean):Boolean =
|
||||
@@ -301,20 +281,6 @@ trait TileMultipart extends TileEntity
|
||||
items.foreach(item => InventoryUtils.dropItem(item, worldObj, pos))
|
||||
}
|
||||
|
||||
def sendPartDesc(part:TMultiPart)
|
||||
{
|
||||
val i = partList.indexOf(part)
|
||||
if(i < 0)
|
||||
throw new IllegalArgumentException("Tried to send desc for non-existant part")
|
||||
|
||||
val packet = new PacketCustom(MultipartSPH.channel, 5)
|
||||
packet.setChunkDataPacket()
|
||||
packet.writeCoord(xCoord, yCoord, zCoord)
|
||||
packet.writeByte(i)
|
||||
part.write(packet)
|
||||
packet.sendToChunk(worldObj, xCoord>>4, zCoord>>4)
|
||||
}
|
||||
|
||||
def markRender()
|
||||
{
|
||||
worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord)
|
||||
@@ -457,24 +423,14 @@ object TileMultipartObj
|
||||
return MultipartGenerator.addPart(world, pos, part)
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
def handleDescPacket(world:World, packet:PacketCustom, netHandler:NetClientHandler)
|
||||
def handleDescPacket(world:World, pos:BlockCoord, packet:PacketCustom)
|
||||
{
|
||||
var partCount = packet.readUnsignedByte()
|
||||
if((partCount & 0x80) > 0)//composite
|
||||
{
|
||||
val superPacket = Packet.getNewPacket(null, packet.readUnsignedByte())
|
||||
superPacket.readPacketData(new DataInputStream(new MCDataInputStream(packet)))
|
||||
superPacket.processPacket(netHandler)
|
||||
}
|
||||
partCount &= 0x7F
|
||||
|
||||
val pos = packet.readCoord
|
||||
val parts = ListBuffer[TMultiPart]()
|
||||
for(i <- 0 until partCount)
|
||||
val nparts = packet.readUnsignedByte
|
||||
val parts = new ListBuffer[TMultiPart]()
|
||||
for(i <- 0 until nparts)
|
||||
{
|
||||
val part:TMultiPart = MultiPartRegistry.readPart(packet)
|
||||
part.read(packet)
|
||||
part.readDesc(packet)
|
||||
parts+=part
|
||||
}
|
||||
|
||||
@@ -490,23 +446,22 @@ object TileMultipartObj
|
||||
tilemp.markRender()
|
||||
}
|
||||
|
||||
def handlePacket(world:World, packet:PacketCustom)
|
||||
def handlePacket(pos:BlockCoord, world:World, i:Int, packet:PacketCustom)
|
||||
{
|
||||
val pos = packet.readCoord
|
||||
var tilemp = BlockMultipart.getTile(world, pos.x, pos.y, pos.z)
|
||||
|
||||
packet.getType() match
|
||||
i match
|
||||
{
|
||||
case 3 => {
|
||||
case 253 => {
|
||||
val part = MultiPartRegistry.readPart(packet)
|
||||
part.read(packet)
|
||||
part.readDesc(packet)
|
||||
addPart(world, pos, part)
|
||||
}
|
||||
case 4 => if(tilemp != null) {
|
||||
case 254 => if(tilemp != null) {
|
||||
tilemp.remPart(tilemp.partList(packet.readUnsignedByte()))
|
||||
}
|
||||
case 5 => if(tilemp != null) {
|
||||
tilemp.partList(packet.readUnsignedByte()).read(packet)
|
||||
case _ => if(tilemp != null) {
|
||||
tilemp.partList(i).read(packet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,14 @@ import net.minecraft.server.MinecraftServer
|
||||
import codechicken.core.packet.PacketCustom
|
||||
import codechicken.multipart.MultiPartRegistry
|
||||
import net.minecraftforge.event.world._
|
||||
import cpw.mods.fml.common.ITickHandler
|
||||
import java.util.EnumSet
|
||||
import cpw.mods.fml.common.TickType
|
||||
import scala.collection.JavaConverters._
|
||||
import java.util.List
|
||||
import net.minecraft.entity.player.EntityPlayerMP
|
||||
|
||||
object MultipartEventHandler extends IConnectionHandler
|
||||
object MultipartEventHandler extends IConnectionHandler with ITickHandler
|
||||
{
|
||||
@ForgeSubscribe
|
||||
def tileEntityLoad(event:ChunkDataEvent.Load)
|
||||
@@ -23,6 +29,25 @@ object MultipartEventHandler extends IConnectionHandler
|
||||
MultipartSaveLoad.loadTiles(event.getChunk())
|
||||
}
|
||||
|
||||
@ForgeSubscribe
|
||||
def worldLoad(event:WorldEvent.Load)
|
||||
{
|
||||
MultipartSPH.onWorldLoad(event.world)
|
||||
}
|
||||
|
||||
@ForgeSubscribe
|
||||
def worldUnLoad(event:WorldEvent.Unload)
|
||||
{
|
||||
MultipartSPH.onWorldUnload(event.world)
|
||||
}
|
||||
|
||||
@ForgeSubscribe
|
||||
def chunkWatch(event:ChunkWatchEvent.Watch)
|
||||
{
|
||||
val cc = event.chunk
|
||||
MultipartSPH.onChunkWatch(event.player, event.player.worldObj.getChunkFromChunkCoords(cc.chunkXPos, cc.chunkZPos))
|
||||
}
|
||||
|
||||
def connectionReceived(loginHandler:NetLoginHandler, netManager:INetworkManager):String =
|
||||
{
|
||||
val packet = new PacketCustom(MultipartSPH.channel, 1)
|
||||
@@ -36,4 +61,14 @@ object MultipartEventHandler extends IConnectionHandler
|
||||
def connectionOpened(netHandler:NetHandler, server:String, port:Int, netManager:INetworkManager){}
|
||||
def connectionOpened(netHandler:NetHandler, server:MinecraftServer, netManager:INetworkManager){}
|
||||
def connectionClosed(netManager:INetworkManager){}
|
||||
|
||||
def ticks = EnumSet.of(TickType.SERVER)
|
||||
def getLabel = "Multipart"
|
||||
def tickStart(tickType:EnumSet[TickType], data:Object*){}
|
||||
def tickEnd(tickType:EnumSet[TickType], data:Object*)
|
||||
{
|
||||
MultipartSPH.onTickEnd(
|
||||
MinecraftServer.getServer.getConfigurationManager.playerEntityList
|
||||
.asInstanceOf[List[EntityPlayerMP]].asScala)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package codechicken.multipart.handler
|
||||
|
||||
import codechicken.core.packet.PacketCustom.ICustomPacketHandler.IClientPacketHandler
|
||||
import codechicken.core.packet.PacketCustom.ICustomPacketHandler.IServerPacketHandler
|
||||
import codechicken.core.packet.PacketCustom.IClientPacketHandler
|
||||
import codechicken.core.packet.PacketCustom.IServerPacketHandler
|
||||
import codechicken.core.packet.PacketCustom
|
||||
import net.minecraft.client.multiplayer.NetClientHandler
|
||||
import net.minecraft.client.Minecraft
|
||||
@@ -13,6 +13,19 @@ import codechicken.multipart.ControlKeyModifer
|
||||
import codechicken.multipart.TileMultipartObj
|
||||
import net.minecraft.network.packet.Packet255KickDisconnect
|
||||
import net.minecraft.server.MinecraftServer
|
||||
import net.minecraft.world.World
|
||||
import net.minecraft.world.chunk.Chunk
|
||||
import java.util.Map
|
||||
import net.minecraft.tileentity.TileEntity
|
||||
import codechicken.multipart.TileMultipart
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import scala.collection._
|
||||
import codechicken.core.vec.BlockCoord
|
||||
import codechicken.core.data.MCOutputStreamWrapper
|
||||
import java.io.DataOutputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import net.minecraft.world.WorldServer
|
||||
import net.minecraft.world.ChunkCoordIntPair
|
||||
|
||||
object MultipartCPH extends IClientPacketHandler
|
||||
{
|
||||
@@ -23,8 +36,8 @@ object MultipartCPH extends IClientPacketHandler
|
||||
packet.getType match
|
||||
{
|
||||
case 1 => handlePartRegistration(packet, netHandler)
|
||||
case 2 => TileMultipartObj.handleDescPacket(mc.theWorld, packet, netHandler)
|
||||
case 3 | 4 | 5 => TileMultipartObj.handlePacket(mc.theWorld, packet)
|
||||
case 2 => handleCompressedTileDesc(packet, mc.theWorld)
|
||||
case 3 => handleCompressedTileData(packet, mc.theWorld)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,11 +47,41 @@ object MultipartCPH extends IClientPacketHandler
|
||||
if(!missing.isEmpty)
|
||||
netHandler.handleKickDisconnect(new Packet255KickDisconnect("The following multiparts are not installed on this client: "+missing.mkString(", ")))
|
||||
}
|
||||
|
||||
def indexInChunk(cc:ChunkCoordIntPair, i:Int) = new BlockCoord(cc.chunkXPos<<4|i&0xF, i>>8, cc.chunkZPos<<4|(i&0xF0)>>4)
|
||||
|
||||
def handleCompressedTileDesc(packet:PacketCustom, world:World)
|
||||
{
|
||||
val cc = new ChunkCoordIntPair(packet.readInt, packet.readInt)
|
||||
while(packet.more)
|
||||
TileMultipartObj.handleDescPacket(world, indexInChunk(cc, packet.readShort), packet)
|
||||
}
|
||||
|
||||
def handleCompressedTileData(packet:PacketCustom, world:World)
|
||||
{
|
||||
while(packet.more)
|
||||
{
|
||||
val pos = packet.readCoord
|
||||
var i = packet.readUnsignedByte
|
||||
do
|
||||
{
|
||||
TileMultipartObj.handlePacket(pos, world, i, packet)
|
||||
i = packet.readUnsignedByte
|
||||
}
|
||||
while(i < 255)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object MultipartSPH extends IServerPacketHandler
|
||||
{
|
||||
val channel = MultipartMod;
|
||||
class MCByteStream(bout:ByteArrayOutputStream) extends MCOutputStreamWrapper(new DataOutputStream(bout))
|
||||
{
|
||||
def getBytes = bout.toByteArray
|
||||
}
|
||||
|
||||
val channel = MultipartMod
|
||||
private val updateMap = mutable.Map[World, mutable.Map[BlockCoord, MCByteStream]]()
|
||||
|
||||
def handlePacket(packet:PacketCustom, netHandler:NetServerHandler, sender:EntityPlayerMP)
|
||||
{
|
||||
@@ -47,4 +90,67 @@ object MultipartSPH extends IServerPacketHandler
|
||||
case 1 => ControlKeyModifer.map.put(sender, packet.readBoolean)
|
||||
}
|
||||
}
|
||||
|
||||
def onWorldLoad(world:World)
|
||||
{
|
||||
if(!world.isRemote)
|
||||
updateMap.put(world, mutable.Map())
|
||||
}
|
||||
|
||||
def onWorldUnload(world:World)
|
||||
{
|
||||
if(!world.isRemote)
|
||||
updateMap.remove(world)
|
||||
}
|
||||
|
||||
def getTileStream(world:World, pos:BlockCoord) =
|
||||
updateMap.getOrElse(world, null).getOrElseUpdate(pos, {
|
||||
val s = new MCByteStream(new ByteArrayOutputStream)
|
||||
s.writeCoord(pos)
|
||||
s
|
||||
})
|
||||
|
||||
def onTickEnd(players:Seq[EntityPlayerMP])
|
||||
{
|
||||
players.foreach(p => {
|
||||
val m = updateMap.getOrElse(p.worldObj, null)
|
||||
if(!m.isEmpty)
|
||||
{
|
||||
val manager = p.worldObj.asInstanceOf[WorldServer].getPlayerManager
|
||||
val packet = new PacketCustom(channel, 3).setChunkDataPacket().compressed()
|
||||
var send = false
|
||||
m.foreach(e =>
|
||||
if(manager.isPlayerWatchingChunk(p, e._1.x>>4, e._1.z>>4))
|
||||
{
|
||||
send = true
|
||||
packet.writeByteArray(e._2.getBytes)
|
||||
packet.writeByte(255)
|
||||
})
|
||||
if(send)
|
||||
packet.sendToPlayer(p)
|
||||
}
|
||||
})
|
||||
updateMap.foreach(_._2.clear())
|
||||
}
|
||||
|
||||
def onChunkWatch(player:EntityPlayer, chunk:Chunk)
|
||||
{
|
||||
val iterator = chunk.chunkTileEntityMap.asInstanceOf[Map[_, TileEntity]].values.iterator
|
||||
val packet = new PacketCustom(channel, 2).setChunkDataPacket().compressed()
|
||||
.writeInt(chunk.xPosition).writeInt(chunk.zPosition)
|
||||
|
||||
var empty = true
|
||||
while(iterator.hasNext)
|
||||
{
|
||||
val tile = iterator.next
|
||||
if(tile.isInstanceOf[TileMultipart])
|
||||
{
|
||||
packet.writeShort(tile.xCoord&0xF|tile.yCoord<<8|(tile.zCoord&0xF)<<4)
|
||||
tile.asInstanceOf[TileMultipart].writeDesc(packet)
|
||||
empty = false
|
||||
}
|
||||
}
|
||||
if(!empty)
|
||||
packet.sendToPlayer(player)
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import codechicken.multipart.TileMultipart
|
||||
import cpw.mods.fml.client.registry.KeyBindingRegistry
|
||||
import codechicken.multipart.ControlKeyHandler
|
||||
import cpw.mods.fml.common.network.NetworkRegistry
|
||||
import cpw.mods.fml.common.registry.TickRegistry
|
||||
|
||||
class MultipartProxy_serverImpl
|
||||
{
|
||||
@@ -43,6 +44,7 @@ class MultipartProxy_serverImpl
|
||||
MinecraftForge.EVENT_BUS.register(MultipartEventHandler)
|
||||
PacketCustom.assignHandler(MultipartSPH.channel, MultipartSPH)
|
||||
NetworkRegistry.instance.registerConnectionHandler(MultipartEventHandler)
|
||||
TickRegistry.registerTickHandler(MultipartEventHandler, Side.SERVER)
|
||||
}
|
||||
|
||||
def onTileClassBuilt(t:Class[_ <: TileEntity])
|
||||
|
||||
@@ -15,8 +15,9 @@ import codechicken.multipart.JCuboidPart;
|
||||
import codechicken.multipart.JNormalOcclusion;
|
||||
import codechicken.multipart.NormalOcclusionTest;
|
||||
import codechicken.multipart.TMultiPart;
|
||||
import codechicken.core.data.MCDataInput;
|
||||
import codechicken.core.data.MCDataOutput;
|
||||
import codechicken.core.inventory.InventoryUtils;
|
||||
import codechicken.core.packet.PacketCustom;
|
||||
import codechicken.core.vec.BlockCoord;
|
||||
import codechicken.core.vec.Cuboid6;
|
||||
import codechicken.core.vec.Vector3;
|
||||
@@ -56,13 +57,13 @@ public class TorchPart extends JCuboidPart implements JNormalOcclusion, IPartMet
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(PacketCustom packet)
|
||||
public void writeDesc(MCDataOutput packet)
|
||||
{
|
||||
packet.writeByte(meta);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(PacketCustom packet)
|
||||
public void readDesc(MCDataInput packet)
|
||||
{
|
||||
meta = packet.readByte();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user