Example #1
0
func (chunk *Chunk) reqUnsubscribeChunk(entityId EntityId, sendPacket bool) {
	if player, ok := chunk.subscribers[entityId]; ok {
		chunk.subscribers[entityId] = nil, false

		// Call any observers registered with AddOnUnsubscribe.
		if observers, ok := chunk.onUnsub[entityId]; ok {
			chunk.onUnsub[entityId] = nil, false
			for _, observer := range observers {
				observer.Unsubscribed(entityId)
			}
		}

		if sendPacket {
			buf := new(bytes.Buffer)
			proto.WritePreChunk(buf, &chunk.loc, ChunkUnload)
			// TODO send PacketEntityDestroy packets for spawns in this chunk.
			player.TransmitPacket(buf.Bytes())
		}
	}
}
Example #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())
	}
}