Ejemplo n.º 1
0
func (chunk *Chunk) reqSubscribeChunk(entityId EntityId, player gamerules.IPlayerClient, notify bool) {
	if _, ok := chunk.subscribers[entityId]; ok {
		// Already subscribed.
		return
	}

	chunk.subscribers[entityId] = player

	// Transmit the chunk data to the new player.
	buf := new(bytes.Buffer)
	// chunk.shard.pktSerial.WritePacketsBuffer(buf, &proto.PacketPreChunk{ //@TODO 1.5.1
	//     ChunkLoc: chunk.loc,
	//     Mode:     ChunkInit,
	// })
	player.TransmitPacket(buf.Bytes())
	player.TransmitPacket(chunk.chunkPacket())
	if notify {
		player.NotifyChunkLoad()
	}

	// Send spawn packets for all entities in the chunk to the player.
	if len(chunk.entities) > 0 {
		buf := new(bytes.Buffer)
		for _, entity := range chunk.entities {
			pkts := entity.SpawnPackets(nil)
			chunk.shard.pktSerial.WritePacketsBuffer(buf, pkts...)
		}
		player.TransmitPacket(buf.Bytes())
	}

	// Spawn existing players for new player.
	if len(chunk.playersData) > 0 {
		buf := new(bytes.Buffer)
		for _, existing := range chunk.playersData {
			if existing.entityId != entityId {
				pkts := existing.SpawnPackets(nil)
				chunk.shard.pktSerial.WritePacketsBuffer(buf, pkts...)
			}
		}
		player.TransmitPacket(buf.Bytes())
	}
}
Ejemplo n.º 2
0
func (chunk *Chunk) reqSubscribeChunk(entityId EntityId, player gamerules.IPlayerClient, notify bool) {
	if _, ok := chunk.subscribers[entityId]; ok {
		// Already subscribed.
		return
	}

	chunk.subscribers[entityId] = player

	buf := new(bytes.Buffer)
	proto.WritePreChunk(buf, &chunk.loc, ChunkInit)
	player.TransmitPacket(buf.Bytes())

	player.TransmitPacket(chunk.chunkPacket())
	if notify {
		player.NotifyChunkLoad()
	}

	// Send spawns packets for all entities in the chunk.
	if len(chunk.entities) > 0 {
		buf := new(bytes.Buffer)
		for _, e := range chunk.entities {
			e.SendSpawn(buf)
		}
		player.TransmitPacket(buf.Bytes())
	}

	// Spawn existing players for new player.
	if len(chunk.playersData) > 0 {
		playersPacket := new(bytes.Buffer)
		for _, existing := range chunk.playersData {
			if existing.entityId != entityId {
				existing.sendSpawn(playersPacket)
			}
		}
		player.TransmitPacket(playersPacket.Bytes())
	}
}