public static void MapChunkBulk(MinecraftClient client, IPacket _packet) { var packet = (MapChunkBulkPacket)_packet; var metadataStrem = new MemoryStream(packet.ChunkMetadata); var minecraftStream = new MinecraftStream(metadataStrem); var data = ZlibStream.UncompressBuffer(packet.ChunkData); int chunkLength = BlockDataLength + (NibbleDataLength * 2) + (Chunk.Width * Chunk.Depth); if (packet.LightIncluded) chunkLength += NibbleDataLength; for (int i = 0; i < packet.ChunkCount; i++) { int x = minecraftStream.ReadInt32(); int z = minecraftStream.ReadInt32(); ushort primaryBitMap = minecraftStream.ReadUInt16(); ushort addBitMap = minecraftStream.ReadUInt16(); // TODO int offset = i * chunkLength; // Read chunk data var chunk = new Chunk(World.GetRelativeChunkPosition(new Vector3(x, 0, z))); var sectionCount = 0; // Get the total sections included in the packet for (int y = 0; y < 16; y++) { if ((primaryBitMap & (1 << y)) > 0) sectionCount++; } // Run through the sections // TODO: Support block IDs >255 for (int y = 0; y < 16; y++) { if ((primaryBitMap & (1 << y)) > 0) { // Add this section Array.Copy(data, offset + (y * BlockDataLength), chunk.Sections[y].Blocks, 0, BlockDataLength); Array.Copy(data, offset + (y * BlockDataLength + (BlockDataLength * sectionCount)), chunk.Sections[y].Metadata.Data, 0, NibbleDataLength); Array.Copy(data, offset + (y * BlockDataLength + (BlockDataLength * sectionCount + NibbleDataLength)), chunk.Sections[y].BlockLight.Data, 0, NibbleDataLength); if (packet.LightIncluded) { Array.Copy(data, offset + (y * BlockDataLength + (BlockDataLength * sectionCount + (NibbleDataLength * 2))), chunk.Sections[y].SkyLight.Data, 0, NibbleDataLength); } } } Array.Copy(data, offset + chunkLength - chunk.Biomes.Length, chunk.Biomes, 0, chunk.Biomes.Length); client.World.SetChunk(new Vector3(x, 0, z), chunk); client.OnChunkRecieved(new ChunkRecievedEventArgs(new Vector3(x, 0, z), new ReadOnlyChunk(chunk))); } }
public void Connect(IPEndPoint endPoint) { if (Client != null && Client.Connected) throw new InvalidOperationException("Already connected to a server!"); EndPoint = endPoint; Client = new TcpClient(); Client.Connect(EndPoint); NetworkStream = Client.GetStream(); Stream = new MinecraftStream(new BufferedStream(NetworkStream)); NetworkWorkerThread = new Thread(NetworkWorker); NetworkWorkerThread.Start(); var handshake = new HandshakePacket(PacketReader.ProtocolVersion, Session.Username, EndPoint.Address.ToString(), EndPoint.Port); SendPacket(handshake); }
public static MetadataDictionary FromStream(MinecraftStream stream) { var value = new MetadataDictionary(); byte key = 0; while (key != 127) { key = stream.ReadUInt8(); if (key == 127) break; byte type = (byte)((key & 0xE0) >> 5); byte index = (byte)(key & 0x1F); var entryType = EntryTypes[type]; value[index] = (MetadataEntry)Activator.CreateInstance(entryType, index); value[index].FromStream(stream); } return value; }
public override void WriteTo(MinecraftStream stream) { stream.WriteUInt8(GetKey()); stream.WriteInt16(Value.Id); if (Value.Id != -1) { stream.WriteInt8(Value.Count); stream.WriteInt16(Value.Metadata); if (Value.Nbt != null) { var data = Value.Nbt.SaveToBuffer(NbtCompression.GZip); stream.WriteInt16((short)data.Length); stream.WriteUInt8Array(data); } else stream.WriteInt16(-1); } }
public static ServerPing DoPing(IPEndPoint endPoint) { var client = new TcpClient(); client.Connect(endPoint); var stream = new MinecraftStream(client.GetStream()); var ping = new ServerListPingPacket(1); ping.WritePacket(stream); var response = PacketReader.ReadPacket(stream); client.Close(); // TODO: Handle old pings var pong = (DisconnectPacket)response; var parts = pong.Reason.Substring(3).Split('\0'); var result = new ServerPing(); result.ProtocolVersion = int.Parse(parts[0]); result.ServerVersion = parts[1]; result.MotD = parts[2]; result.CurrentPlayers = int.Parse(parts[3]); result.MaxPlayers = int.Parse(parts[4]); return result; }
public override void Decode(MinecraftStream stream) { var chunkSectionPos = stream.ReadLong(); ChunkX = (int)(chunkSectionPos >> 42); var sectionY = (int)(chunkSectionPos << 44 >> 44); ChunkZ = (int)(chunkSectionPos << 22 >> 42); var inverse = stream.ReadBool(); //ChunkX = stream.ReadInt(); // ChunkZ = stream.ReadInt(); int recordCount = stream.ReadVarInt(); Records = new BlockUpdate[recordCount]; for (int i = 0; i < Records.Length; i++) { var encoded = stream.ReadVarLong(); // long encoded = rawId << 12 | (blockLocalX << 8 | blockLocalZ << 4 | blockLocalY) var rawId = encoded >> 12; var x = (int)((encoded >> 8) & 0xF); var y = (int)(encoded & 0xF); var z = (int)((encoded >> 4) & 0xF); //byte horizontalPos = (byte)stream.ReadByte(); BlockUpdate update = new BlockUpdate(); update.X = (ChunkX << 4) + x; update.Z = (ChunkZ << 4) + z; update.Y = (sectionY << 4) + y; update.BlockId = (uint)rawId; Records[i] = update; } }
private void ReadAddPlayerEntries(int count, MinecraftStream stream) { AddPlayerEntries = new AddPlayerEntry[count]; for (int i = 0; i < count; i++) { var newEntry = new AddPlayerEntry(); newEntry.UUID = stream.ReadUuid(); newEntry.Name = stream.ReadString(); int propertyLength = stream.ReadVarInt(); newEntry.Properties = new PlayerListProperty[propertyLength]; for (int ii = 0; ii < propertyLength; ii++) { newEntry.Properties[ii] = new PlayerListProperty() { Name = stream.ReadString(), Value = stream.ReadString(), IsSigned = stream.ReadBool() }; if (newEntry.Properties[ii].IsSigned) { newEntry.Properties[ii].Signature = stream.ReadString(); } } newEntry.Gamemode = stream.ReadVarInt(); newEntry.Ping = stream.ReadVarInt(); newEntry.HasDisplayName = stream.ReadBool(); if (newEntry.HasDisplayName) { newEntry.DisplayName = stream.ReadString(); } AddPlayerEntries[i] = newEntry; } }
public override async Task WriteAsync(MinecraftStream stream) { await stream.WriteUuidAsync(this.Uuid); await stream.WriteStringAsync(this.Name, 16); await stream.WriteVarIntAsync(this.Properties.Count); foreach (SkinProperties props in this.Properties) { await stream.WriteAsync(await props.ToArrayAsync()); } await stream.WriteVarIntAsync(this.Gamemode); await stream.WriteVarIntAsync(this.Ping); await stream.WriteBooleanAsync(this.HasDisplayName); if (this.HasDisplayName) { await stream.WriteChatAsync(this.DisplayName); } }
public static async Task <Packet> ReadFromStreamAsync(MinecraftStream stream) { int length = await stream.ReadVarIntAsync(); byte[] receivedData = new byte[length]; await stream.ReadAsync(receivedData, 0, length); int packetId = 0; byte[] packetData = new byte[0]; using (var packetStream = new MinecraftStream(receivedData)) { try { packetId = await packetStream.ReadVarIntAsync(); int arlen = 0; if (length - packetId.GetVarintLength() > -1) { arlen = length - packetId.GetVarintLength(); } packetData = new byte[arlen]; await packetStream.ReadAsync(packetData, 0, packetData.Length); } catch { throw; } } return(new EmptyPacket(packetId, packetData)); }
private async Task <(int id, byte[] data)> GetNextPacketAsync() { int length = await this.minecraftStream.ReadVarIntAsync(); byte[] receivedData = new byte[length]; await this.minecraftStream.ReadAsync(receivedData, 0, length); int packetId = 0; byte[] packetData = Array.Empty <byte>(); using (var packetStream = new MinecraftStream(receivedData)) { try { packetId = await packetStream.ReadVarIntAsync(); int arlen = 0; if (length - packetId.GetVarIntLength() > -1) { arlen = length - packetId.GetVarIntLength(); } packetData = new byte[arlen]; await packetStream.ReadAsync(packetData, 0, packetData.Length); } catch { throw; } } return(packetId, packetData); }
public override void Decode(MinecraftStream stream) { Event = (CombatEvent)stream.ReadVarInt(); switch (Event) { case CombatEvent.EnterCombat: break; case CombatEvent.EndCombat: Duration = stream.ReadVarInt(); EntityId = stream.ReadInt(); break; case CombatEvent.EntityDead: PlayerId = stream.ReadVarInt(); EntityId = stream.ReadInt(); Message = stream.ReadString(); break; default: throw new ArgumentOutOfRangeException(); } }
public async ValueTask <PluginMessageStore> HandleAsync() { using var stream = new MinecraftStream(this.PluginData); var result = this.Channel switch { "minecraft:brand" => new PluginMessageStore { Type = PluginMessageType.Brand, Value = await stream.ReadStringAsync() }, "minecraft:register" => new PluginMessageStore//Payload should be a list of strings { Type = PluginMessageType.Register, Value = this.PluginData }, "minecraft:unregister" => new PluginMessageStore { Type = PluginMessageType.UnRegister }, _ => throw new System.NotImplementedException() }; return(result); }
public static ServerPing DoPing(IPEndPoint endPoint) { var client = new TcpClient(); client.Connect(endPoint); var stream = new MinecraftStream(client.GetStream()); var ping = new ServerListPingPacket(1); ping.WritePacket(stream); var response = PacketReader.ReadPacket(stream); client.Close(); // TODO: Handle old pings var pong = (DisconnectPacket)response; var parts = pong.Reason.Substring(3).Split('\0'); var result = new ServerPing(); result.ProtocolVersion = int.Parse(parts[0]); result.ServerVersion = parts[1]; result.MotD = parts[2]; result.CurrentPlayers = int.Parse(parts[3]); result.MaxPlayers = int.Parse(parts[4]); return(result); }
public override void Encode(MinecraftStream stream) { }
public override void FromStream(MinecraftStream stream) { Value = Slot.FromStream(stream); }
public override void WriteTo(MinecraftStream stream, byte index) { stream.WriteUInt8(GetKey(index)); stream.WriteUInt8(Value); }
public override void WriteTo(MinecraftStream stream, byte index) { stream.WriteUInt8(GetKey(index)); stream.WriteUInt8(Value); }
public override void Decode(MinecraftStream stream) { Flags = (byte)stream.ReadByte(); FlyingSpeed = stream.ReadFloat(); FiedOfViewModifier = stream.ReadFloat(); }
public override void WriteTo(MinecraftStream stream) { stream.WriteUInt8(GetKey()); stream.WriteUInt8(Value); }
public abstract Task HandleAsync(MinecraftStream stream);
public override void FromStream(MinecraftStream stream) { Value = ItemStack.FromStream(stream); }
/// <summary> /// Retrieves the requested chunk from the region, or /// generates it if a world generator is provided. /// </summary> /// <param name="position">The position of the requested local chunk coordinates.</param> public Chunk GetChunk(Vector3 position) { // TODO: This could use some refactoring lock (Chunks) { if (!Chunks.ContainsKey(position)) { if (regionFile != null) { // Search the stream for that region lock (regionFile) { var chunkData = GetChunkFromTable(position); if (chunkData == null) { if (WorldGenerator == null) throw new ArgumentException("The requested chunk is not loaded.", "position"); GenerateChunk(position); return Chunks[position]; } regionFile.Seek(chunkData.Item1, SeekOrigin.Begin); int length = new MinecraftStream(regionFile).ReadInt32(); // TODO: Avoid making new objects here, and in the WriteInt32 int compressionMode = regionFile.ReadByte(); switch (compressionMode) { case 1: // gzip break; case 2: // zlib var nbt = new NbtFile(); nbt.LoadFromStream(regionFile, NbtCompression.ZLib, null); var chunk = Chunk.FromNbt(position, nbt); chunk.ParentRegion = this; Chunks.Add(position, chunk); break; default: throw new InvalidDataException("Invalid compression scheme provided by region file."); } } } else if (WorldGenerator == null) throw new ArgumentException("The requested chunk is not loaded.", "position"); else Chunks.Add(position, WorldGenerator.GenerateChunk(position, this)); } return Chunks[position]; } }
public void WriteTo(MinecraftStream stream) { foreach (var entry in entries) entry.Value.WriteTo(stream); stream.WriteUInt8(0x7F); }
static LogProvider() { logProviders = new List<ILogProvider>(); MemoryStream = new MemoryStream(); MinecraftStream = new MinecraftStream(MemoryStream); }
public override void FromStream(MinecraftStream stream) { Value = stream.ReadSingle(); }
public override void Decode(MinecraftStream stream) { Message = stream.ReadString(); }
public override void Encode(MinecraftStream stream) { stream.WriteString(Message); }
public override void Decode(MinecraftStream stream) { KeepAliveid = stream.ReadLong(); }
public override async Task HandleAsync(MinecraftStream stream) => await stream.ReadStringAsync();
/// <inheritdoc /> public override void Decode(MinecraftStream stream) { ExperienceBar = stream.ReadFloat(); Level = stream.ReadVarInt(); TotalExperience = stream.ReadVarInt(); }
/// <inheritdoc /> public override void Decode(MinecraftStream stream) { ChunkX = stream.ReadVarInt(); ChunkZ = stream.ReadVarInt(); }
public abstract void FromStream(MinecraftStream stream);
public override void FromStream(MinecraftStream stream) { Value = stream.ReadUInt8(); }
public Task WriteAsync(MinecraftStream stream) => Task.CompletedTask;
public override void FromStream(MinecraftStream stream) { Value = stream.ReadUInt8(); }
public abstract void WriteTo(MinecraftStream stream);
public Task ReadAsync(MinecraftStream stream) => Task.CompletedTask;
public override void WriteTo(MinecraftStream stream) { stream.WriteUInt8(GetKey()); stream.WriteUInt8Array(Encoding.ASCII.GetBytes(Value)); }
public override void Encode(MinecraftStream stream) { stream.WriteLong(KeepAliveid); }
public async Task ReadAsync(MinecraftStream stream) { this.Hand = (Hand)await stream.ReadVarIntAsync(); }
/// <inheritdoc /> public override void Encode(MinecraftStream stream) { throw new System.NotImplementedException(); }
public async Task ReadAsync(MinecraftStream stream) { this.ClickedSlot = await stream.ReadShortAsync(); this.ClickedItem = await stream.ReadSlotAsync(); }
public override async Task WriteAsync(MinecraftStream stream) { await base.WriteAsync(stream); await stream.WriteEntityMetdata(18, EntityMetadataType.Boolean, this.HasChest); }
public override async Task WriteAsync(MinecraftStream stream) { await base.WriteAsync(stream); await stream.WriteStringAsync(this.DisplayName); }
public abstract void WriteTo(MinecraftStream stream, byte index);
public override void Decode(MinecraftStream stream) { EntityId = stream.ReadVarInt(); HeadYaw = (sbyte)stream.ReadByte(); }
public async Task ReadAsync(MinecraftStream stream) { this.KeepAliveId = await stream.ReadLongAsync(); }
public override void Encode(MinecraftStream stream) { stream.WriteVarInt(EntityId); stream.WriteByte((byte)HeadYaw); }