mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-26 23:13:07 +00:00
Compare commits
9
Commits
1.7.16
...
worldregistry
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c95a612a26 | ||
|
|
58046590c0 | ||
|
|
92dec60858 | ||
|
|
471da2192b | ||
|
|
c758338831 | ||
|
|
2252aa49cb | ||
|
|
2e683823a3 | ||
|
|
f081e7b741 | ||
|
|
517ad942cf |
No files matched your search
+1
-1
@@ -9,7 +9,7 @@ dependencies {
|
|||||||
devOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.8.114-GTNH:dev") {
|
devOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.8.114-GTNH:dev") {
|
||||||
exclude group: 'com.github.GTNewHorizons', module: 'CodeChickenCore'
|
exclude group: 'com.github.GTNewHorizons', module: 'CodeChickenCore'
|
||||||
}
|
}
|
||||||
compileOnly("com.github.GTNewHorizons:GTNHLib:0.11.44:dev")
|
api("com.github.GTNewHorizons:GTNHLib:9.9.9:dev")
|
||||||
// devOnlyNonPublishable("com.github.GTNewHorizons:Angelica:2.2.10:dev")
|
// devOnlyNonPublishable("com.github.GTNewHorizons:Angelica:2.2.10:dev")
|
||||||
devOnlyNonPublishable("io.github.legacymoddingmc:unimixins:0.1.23:dev")
|
devOnlyNonPublishable("io.github.legacymoddingmc:unimixins:0.1.23:dev")
|
||||||
}
|
}
|
||||||
@@ -2,19 +2,14 @@ package codechicken.multipart.handler
|
|||||||
|
|
||||||
import codechicken.multipart.{BlockMultipart, TileCache, TileMultipart}
|
import codechicken.multipart.{BlockMultipart, TileCache, TileMultipart}
|
||||||
import cpw.mods.fml.common.eventhandler.{EventPriority, SubscribeEvent}
|
import cpw.mods.fml.common.eventhandler.{EventPriority, SubscribeEvent}
|
||||||
import cpw.mods.fml.common.gameevent.TickEvent
|
import cpw.mods.fml.common.gameevent.{PlayerEvent, TickEvent}
|
||||||
import cpw.mods.fml.relauncher.{Side, SideOnly}
|
import cpw.mods.fml.relauncher.{Side, SideOnly}
|
||||||
import net.minecraft.entity.player.EntityPlayerMP
|
|
||||||
import net.minecraft.server.MinecraftServer
|
|
||||||
import net.minecraft.tileentity.TileEntity
|
import net.minecraft.tileentity.TileEntity
|
||||||
import net.minecraft.util.MovingObjectPosition.MovingObjectType
|
import net.minecraft.util.MovingObjectPosition.MovingObjectType
|
||||||
import net.minecraft.world.ChunkPosition
|
import net.minecraft.world.ChunkPosition
|
||||||
import net.minecraftforge.client.event.DrawBlockHighlightEvent
|
import net.minecraftforge.client.event.DrawBlockHighlightEvent
|
||||||
import net.minecraftforge.event.world._
|
import net.minecraftforge.event.world._
|
||||||
|
|
||||||
import java.util.List
|
|
||||||
import scala.collection.JavaConverters._
|
|
||||||
|
|
||||||
object MultipartEventHandler {
|
object MultipartEventHandler {
|
||||||
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
@SubscribeEvent(priority = EventPriority.HIGHEST)
|
||||||
def tileEntityLoad(event: ChunkDataEvent.Load) {
|
def tileEntityLoad(event: ChunkDataEvent.Load) {
|
||||||
@@ -70,13 +65,19 @@ object MultipartEventHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
def playerLogout(event: PlayerEvent.PlayerLoggedOutEvent) {
|
||||||
|
MultipartSPH.onPlayerLogout(event.player)
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
def playerChangedDimension(event: PlayerEvent.PlayerChangedDimensionEvent) {
|
||||||
|
MultipartSPH.onPlayerChangedDimension(event.player, event.fromDim)
|
||||||
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
def serverTick(event: TickEvent.ServerTickEvent) {
|
def serverTick(event: TickEvent.ServerTickEvent) {
|
||||||
if (event.phase == TickEvent.Phase.END)
|
if (event.phase == TickEvent.Phase.END)
|
||||||
MultipartSPH.onTickEnd(
|
MultipartSPH.onTickEnd()
|
||||||
MinecraftServer.getServer.getConfigurationManager.playerEntityList
|
|
||||||
.asInstanceOf[List[EntityPlayerMP]]
|
|
||||||
.asScala
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,8 +24,9 @@ import java.io.DataOutputStream
|
|||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import net.minecraft.world.ChunkCoordIntPair
|
import net.minecraft.world.ChunkCoordIntPair
|
||||||
import MultipartProxy._
|
import MultipartProxy._
|
||||||
|
import com.gtnewhorizon.gtnhlib.api.world.WorldContextRegistry
|
||||||
|
import com.gtnewhorizon.gtnhlib.api.world.WorldContextRegistry.WorldAddress
|
||||||
import codechicken.multipart.PacketScheduler
|
import codechicken.multipart.PacketScheduler
|
||||||
import java.util.LinkedList
|
|
||||||
import scala.collection.JavaConversions._
|
import scala.collection.JavaConversions._
|
||||||
import net.minecraft.network.play.{INetHandlerPlayServer, INetHandlerPlayClient}
|
import net.minecraft.network.play.{INetHandlerPlayServer, INetHandlerPlayClient}
|
||||||
import net.minecraft.network.play.server.S40PacketDisconnect
|
import net.minecraft.network.play.server.S40PacketDisconnect
|
||||||
@@ -35,6 +36,22 @@ import net.minecraft.network.NetHandlerPlayServer
|
|||||||
class MultipartPH {
|
class MultipartPH {
|
||||||
val channel = MultipartMod
|
val channel = MultipartMod
|
||||||
val registryChannel = "ForgeMultipart"
|
val registryChannel = "ForgeMultipart"
|
||||||
|
|
||||||
|
/** Writes the address of a world into the ForgeMultipart payload. */
|
||||||
|
def writeWorld(packet: PacketCustom, world: World): PacketCustom = {
|
||||||
|
val address = WorldContextRegistry.addressOf(world)
|
||||||
|
packet.writeInt(address.hostDimensionId)
|
||||||
|
packet.writeInt(address.subId)
|
||||||
|
packet.writeString(address.namespace)
|
||||||
|
packet
|
||||||
|
}
|
||||||
|
|
||||||
|
def readWorldAddress(packet: PacketCustom): WorldAddress = {
|
||||||
|
val hostDimensionId = packet.readInt
|
||||||
|
val subId = packet.readInt
|
||||||
|
val namespace = packet.readString()
|
||||||
|
new WorldAddress(hostDimensionId, namespace, subId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object MultipartCPH extends MultipartPH with IClientPacketHandler {
|
object MultipartCPH extends MultipartPH with IClientPacketHandler {
|
||||||
@@ -46,8 +63,16 @@ object MultipartCPH extends MultipartPH with IClientPacketHandler {
|
|||||||
try {
|
try {
|
||||||
packet.getType match {
|
packet.getType match {
|
||||||
case 1 => handlePartRegistration(packet, netHandler)
|
case 1 => handlePartRegistration(packet, netHandler)
|
||||||
case 2 => handleCompressedTileDesc(packet, mc.theWorld)
|
case 2 =>
|
||||||
case 3 => handleCompressedTileData(packet, mc.theWorld)
|
handleCompressedTileDesc(
|
||||||
|
packet,
|
||||||
|
WorldContextRegistry.getClientWorld(readWorldAddress(packet))
|
||||||
|
)
|
||||||
|
case 3 =>
|
||||||
|
handleCompressedTileData(
|
||||||
|
packet,
|
||||||
|
WorldContextRegistry.getClientWorld(readWorldAddress(packet))
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
case e: RuntimeException
|
case e: RuntimeException
|
||||||
@@ -116,12 +141,20 @@ object MultipartSPH
|
|||||||
|
|
||||||
private val updateMap = Map[World, Map[BlockCoord, MCByteStream]]()
|
private val updateMap = Map[World, Map[BlockCoord, MCByteStream]]()
|
||||||
|
|
||||||
/** These maps are keyed by entityID so that new player instances with the
|
private type WatchMap = HashMap[EntityPlayerMP, Set[ChunkCoordIntPair]]
|
||||||
* same entity id don't conflict world references
|
with MultiMap[EntityPlayerMP, ChunkCoordIntPair]
|
||||||
|
|
||||||
|
private def newWatchMap: WatchMap =
|
||||||
|
new HashMap[EntityPlayerMP, Set[ChunkCoordIntPair]]
|
||||||
|
with MultiMap[EntityPlayerMP, ChunkCoordIntPair]
|
||||||
|
|
||||||
|
/** Watches are separated by world first, because a chunk coordinate only
|
||||||
|
* means something inside one world, and the same player may watch chunks in
|
||||||
|
* a host world and in virtual worlds inside it. The world is recorded when
|
||||||
|
* the watch happens and never re-derived from the player afterwards.
|
||||||
*/
|
*/
|
||||||
private val chunkWatchers = new HashMap[Int, Set[ChunkCoordIntPair]]
|
private val chunkWatchers = Map[World, WatchMap]()
|
||||||
with MultiMap[Int, ChunkCoordIntPair]
|
private val newWatchers = Map[World, WatchMap]()
|
||||||
private val newWatchers = Map[Int, LinkedList[ChunkCoordIntPair]]()
|
|
||||||
|
|
||||||
def handlePacket(
|
def handlePacket(
|
||||||
packet: PacketCustom,
|
packet: PacketCustom,
|
||||||
@@ -140,8 +173,11 @@ object MultipartSPH
|
|||||||
}
|
}
|
||||||
|
|
||||||
def onWorldUnload(world: World) {
|
def onWorldUnload(world: World) {
|
||||||
if (!world.isRemote)
|
if (!world.isRemote) {
|
||||||
updateMap.remove(world)
|
updateMap.remove(world)
|
||||||
|
chunkWatchers.remove(world)
|
||||||
|
newWatchers.remove(world)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def getTileStream(world: World, pos: BlockCoord) =
|
def getTileStream(world: World, pos: BlockCoord) =
|
||||||
@@ -163,58 +199,144 @@ object MultipartSPH
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
def onTickEnd(players: Seq[EntityPlayerMP]) {
|
def onTickEnd() {
|
||||||
PacketScheduler.sendScheduled()
|
PacketScheduler.sendScheduled()
|
||||||
|
|
||||||
for (p <- players if chunkWatchers.containsKey(p.getEntityId)) {
|
for ((world, m) <- updateMap if !m.isEmpty) {
|
||||||
updateMap.get(p.worldObj) match {
|
if (chunkWatchers.contains(world))
|
||||||
case Some(m) if !m.isEmpty =>
|
sendUpdates(
|
||||||
val chunks = chunkWatchers(p.getEntityId)
|
world,
|
||||||
val packet = new PacketCustom(channel, 3).compress()
|
m,
|
||||||
var send = false
|
world,
|
||||||
for (
|
pos => new ChunkCoordIntPair(pos.x >> 4, pos.z >> 4)
|
||||||
(pos, stream) <- m
|
)
|
||||||
if chunks(new ChunkCoordIntPair(pos.x >> 4, pos.z >> 4))
|
else
|
||||||
) {
|
// The world is not watched directly, it is embedded in a host world.
|
||||||
send = true
|
sendUpdates(
|
||||||
packet.writeByteArray(stream.getBytes)
|
world,
|
||||||
packet.writeByte(255) // terminator
|
m,
|
||||||
}
|
WorldContextRegistry.getHostWorld(world),
|
||||||
if (send) {
|
pos =>
|
||||||
packet.writeInt(Int.MaxValue) // terminator
|
WorldContextRegistry.getHostChunk(
|
||||||
packet.sendToPlayer(p)
|
world,
|
||||||
}
|
new ChunkCoordIntPair(pos.x >> 4, pos.z >> 4)
|
||||||
case _ =>
|
)
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
updateMap.foreach(_._2.clear())
|
updateMap.foreach(_._2.clear())
|
||||||
for (p <- players if newWatchers.containsKey(p.getEntityId)) {
|
|
||||||
for (c <- newWatchers(p.getEntityId)) {
|
for (
|
||||||
val chunk = p.worldObj.getChunkFromChunkCoords(c.chunkXPos, c.chunkZPos)
|
(world, watchers) <- newWatchers;
|
||||||
val pkt = getDescPacket(
|
(p, chunks) <- watchers;
|
||||||
chunk,
|
c <- chunks
|
||||||
chunk.chunkTileEntityMap
|
) {
|
||||||
.asInstanceOf[JMap[_, TileEntity]]
|
sendDescription(world, p, c)
|
||||||
.values
|
chunkWatchers.getOrElseUpdate(world, newWatchMap).addBinding(p, c)
|
||||||
.iterator
|
// Chunks embedded in this one become visible with it.
|
||||||
)
|
for (
|
||||||
if (pkt != null) pkt.sendToPlayer(p)
|
(subWorld, subChunks) <- WorldContextRegistry
|
||||||
chunkWatchers.addBinding(p.getEntityId, c)
|
.getVisibleChunks(world, c);
|
||||||
}
|
sc <- subChunks
|
||||||
|
) sendDescription(subWorld, p, sc)
|
||||||
}
|
}
|
||||||
newWatchers.clear()
|
newWatchers.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
def onChunkWatch(p: EntityPlayer, c: ChunkCoordIntPair) {
|
private def sendUpdates(
|
||||||
newWatchers.getOrElseUpdate(p.getEntityId, new LinkedList).add(c)
|
world: World,
|
||||||
|
updates: Map[BlockCoord, MCByteStream],
|
||||||
|
watchWorld: World,
|
||||||
|
hostChunk: BlockCoord => ChunkCoordIntPair
|
||||||
|
) {
|
||||||
|
for (
|
||||||
|
watchers <- chunkWatchers.get(watchWorld);
|
||||||
|
(p, chunks) <- watchers
|
||||||
|
) {
|
||||||
|
val packet = writeWorld(new PacketCustom(channel, 3).compress(), world)
|
||||||
|
var send = false
|
||||||
|
for ((pos, stream) <- updates) {
|
||||||
|
val c = hostChunk(pos)
|
||||||
|
if (c != null && chunks(c)) {
|
||||||
|
send = true
|
||||||
|
packet.writeByteArray(stream.getBytes)
|
||||||
|
packet.writeByte(255) // terminator
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (send) {
|
||||||
|
packet.writeInt(Int.MaxValue) // terminator
|
||||||
|
packet.sendToPlayer(p)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def onChunkUnWatch(p: EntityPlayer, c: ChunkCoordIntPair) {
|
private def sendDescription(
|
||||||
newWatchers.get(p.getEntityId) match {
|
world: World,
|
||||||
case Some(chunks) => chunks.remove(c)
|
p: EntityPlayerMP,
|
||||||
case _ =>
|
c: ChunkCoordIntPair
|
||||||
|
) {
|
||||||
|
val chunk = world.getChunkFromChunkCoords(c.chunkXPos, c.chunkZPos)
|
||||||
|
val pkt = getDescPacket(
|
||||||
|
chunk,
|
||||||
|
chunk.chunkTileEntityMap
|
||||||
|
.asInstanceOf[JMap[_, TileEntity]]
|
||||||
|
.values
|
||||||
|
.iterator
|
||||||
|
)
|
||||||
|
if (pkt != null) pkt.sendToPlayer(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
def onChunkWatch(p: EntityPlayerMP, c: ChunkCoordIntPair) {
|
||||||
|
val hostPlayer = WorldContextRegistry.getHostPlayer(p.worldObj, p)
|
||||||
|
newWatchers
|
||||||
|
.getOrElseUpdate(p.worldObj, newWatchMap)
|
||||||
|
.addBinding(hostPlayer, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
def onChunkUnWatch(p: EntityPlayerMP, c: ChunkCoordIntPair) {
|
||||||
|
val hostPlayer = WorldContextRegistry.getHostPlayer(p.worldObj, p)
|
||||||
|
removeBinding(newWatchers, p.worldObj, hostPlayer, c)
|
||||||
|
removeBinding(chunkWatchers, p.worldObj, hostPlayer, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
private def removeBinding(
|
||||||
|
map: Map[World, WatchMap],
|
||||||
|
world: World,
|
||||||
|
p: EntityPlayerMP,
|
||||||
|
c: ChunkCoordIntPair
|
||||||
|
) {
|
||||||
|
map.get(world) match {
|
||||||
|
case Some(watchers) =>
|
||||||
|
watchers.removeBinding(p, c)
|
||||||
|
if (watchers.isEmpty) map.remove(world)
|
||||||
|
case _ =>
|
||||||
}
|
}
|
||||||
chunkWatchers.removeBinding(p.getEntityId, c)
|
}
|
||||||
|
def onPlayerLogout(p: EntityPlayer) = p match {
|
||||||
|
case mp: EntityPlayerMP =>
|
||||||
|
for (map <- Seq(chunkWatchers, newWatchers)) {
|
||||||
|
for ((world, watchers) <- map.toSeq) {
|
||||||
|
watchers.remove(mp)
|
||||||
|
if (watchers.isEmpty) map.remove(world)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case _ =>
|
||||||
|
}
|
||||||
|
|
||||||
|
def onPlayerChangedDimension(p: EntityPlayer, fromDimension: Int) = p match {
|
||||||
|
case mp: EntityPlayerMP =>
|
||||||
|
for (map <- Seq(chunkWatchers, newWatchers)) {
|
||||||
|
for (
|
||||||
|
(world, watchers) <- map.toSeq
|
||||||
|
if WorldContextRegistry
|
||||||
|
.getHostWorld(world)
|
||||||
|
.provider
|
||||||
|
.dimensionId == fromDimension
|
||||||
|
) {
|
||||||
|
watchers.remove(mp)
|
||||||
|
if (watchers.isEmpty) map.remove(world)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case _ =>
|
||||||
}
|
}
|
||||||
|
|
||||||
def getDescPacket(chunk: Chunk, it: Iterator[TileEntity]): PacketCustom = {
|
def getDescPacket(chunk: Chunk, it: Iterator[TileEntity]): PacketCustom = {
|
||||||
@@ -232,8 +354,7 @@ object MultipartSPH
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (num != 0) {
|
if (num != 0) {
|
||||||
return new PacketCustom(channel, 2)
|
return writeWorld(new PacketCustom(channel, 2).compress(), chunk.worldObj)
|
||||||
.compress()
|
|
||||||
.writeInt(chunk.xPosition)
|
.writeInt(chunk.xPosition)
|
||||||
.writeInt(chunk.zPosition)
|
.writeInt(chunk.zPosition)
|
||||||
.writeShort(num)
|
.writeShort(num)
|
||||||
|
|||||||
Reference in new issue
Block a user