diff --git a/MoBot/MoBot.csproj b/MoBot/MoBot.csproj index 410ebd2..78c689d 100644 --- a/MoBot/MoBot.csproj +++ b/MoBot/MoBot.csproj @@ -70,6 +70,7 @@ + @@ -80,17 +81,42 @@ + + + + + + + + - + + + + + + + + + + + + + + + + + + - + Form diff --git a/MoBot/NLog.config b/MoBot/NLog.config index 61c8d1d..4374814 100644 --- a/MoBot/NLog.config +++ b/MoBot/NLog.config @@ -40,7 +40,7 @@ - + diff --git a/MoBot/Protocol/Handlers/ClientHandler.cs b/MoBot/Protocol/Handlers/ClientHandler.cs index 94e541e..ac6cfb1 100644 --- a/MoBot/Protocol/Handlers/ClientHandler.cs +++ b/MoBot/Protocol/Handlers/ClientHandler.cs @@ -18,6 +18,10 @@ using System.Net.Sockets; using MoBot.Protocol.Packets.Play; using Newtonsoft.Json.Linq; +using MoBot.Structure.Game; +using MoBot.Structure.Actions; +using MinecraftEmuPTS.Encription; +using MinecraftEmuPTS.GameData; namespace MoBot.Protocol.Handlers { @@ -138,12 +142,10 @@ public void HandlePacketLoginSucess(PacketLoginSuccess packetLoginSuccess) { model.mainChannel.ChangeState(Channel.State.Play); } - public void HandlePacketKeepAlive(PacketKeepAlive packetKeepAlive) { model.SendPacket(packetKeepAlive); } - public void HandlePacketCustomPayload(PacketCustomPayload packetCustomPayload) { if(packetCustomPayload.channel == "FML|HS") @@ -191,5 +193,129 @@ public void HandlePacketCustomPayload(PacketCustomPayload packetCustomPayload) log.Info("Unhandled FmlDescriptor : {0}", Discriminator); } } + public void HandlePacketJoinGame(PacketJoinGame packetJoinGame) + { + model.controller.CreatePlayer(packetJoinGame.EntityID, model.username); + } + public void HandlePacketPlayerAbliities(PacketPlayerAbilities packetPlayerAbilities) + { + return; + } + public void HandlePacketHeldItemChange(PacketHeldItemChange packetHeldItemChange) + { + model.controller.player.HeldItem = packetHeldItemChange.Slot; + } + public void HandlePacketPlayerPosLook(PacketPlayerPosLook packetPlayerPosLook) + { + model.controller.player.x = packetPlayerPosLook.X; + model.controller.player.y = packetPlayerPosLook.Y; + model.controller.player.z = packetPlayerPosLook.Z; + model.controller.player.yaw = packetPlayerPosLook.yaw; + model.controller.player.pitch = packetPlayerPosLook.pitch; + } + public void HandlePacketWindowItems(PacketWindowItems packetWindowItems) + { + if(packetWindowItems.WindowID == 0) + { + packetWindowItems.Items.CopyTo(model.controller.player.inventory,0); + } + } + public void HandlePacketSetSlot(PacketSetSlot packetSetSlot) + { + if(packetSetSlot.WindowID == 0) + { + model.controller.player.inventory[packetSetSlot.Slot] = packetSetSlot.item; + } + } + public void HandlePacketSpawnMoob(PacketSpawnMob packetSpawnMob) + { + Mob mob = model.controller.createMob(packetSpawnMob.EntityID, packetSpawnMob.Type); + mob.x = packetSpawnMob.X; + mob.y = packetSpawnMob.Y; + mob.z = packetSpawnMob.Z; + } + public void HandlePacketChat(PacketChat packetChat) + { + model.viewer.OnNext(new ActionChatMessage { JSONMessage = packetChat.message }); + } + public void HandlePacketMapChunk(PacketMapChunk packetMapChunk) + { + byte[] mas = new byte[packetMapChunk.DataLength - 2]; + Array.Copy(packetMapChunk.ChunkData, 2, mas, 0, packetMapChunk.DataLength - 2); + Decompressor dc = new Decompressor(mas); + byte[] dced = dc.decompress(); + + for (int i = 0; i < packetMapChunk.ChunkNumber; i++) + { + dced = packetMapChunk.chunks[i].getData(dced); + model.controller.world.AddChunk(packetMapChunk.chunks[i]); + } + } + public void HandlePacketChunkData(PacketChunkData packetChunkData) + { + if (packetChunkData.RemoveChunk) + { + model.controller.world.RemoveChunk(packetChunkData.x, packetChunkData.z); + } + else + { + byte[] mas = new byte[packetChunkData.Length - 2]; + Array.Copy(packetChunkData.ChunkData, 2, mas, 0, packetChunkData.Length - 2); + Decompressor dc = new Decompressor(mas); + byte[] dced = dc.decompress(); + + packetChunkData.chunk.getData(dced); + model.controller.world.AddChunk(packetChunkData.chunk); + } + } + public void HandlePacketEntity(PacketEntity packetEntity) + { + LivingEntity entity = model.controller.entityList[packetEntity.EntityID] as LivingEntity; + entity.x += packetEntity.x; + entity.y += packetEntity.y; + entity.z += packetEntity.z; + } + public void HandlePacketEntityTeleport(PacketEntityTeleport packetEntityTeleport) + { + LivingEntity entity = model.controller.entityList[packetEntityTeleport.EntityID] as LivingEntity; + entity.x = packetEntityTeleport.x; + entity.y = packetEntityTeleport.y; + entity.z = packetEntityTeleport.z; + } + public void HandlePacketDestroyEntities(PacketDestroyEntities packetDestroyEntities) + { + foreach(int ID in packetDestroyEntities.IDList) + { + model.controller.entityList.Remove(ID); + } + } + public void HandlePacketBlockChange(PacketBlockChange packetBlockChange) + { + model.controller.world.UpdateBlock(packetBlockChange.X, packetBlockChange.Y, packetBlockChange.Z, packetBlockChange.BlockID); + } + public void HandlePacketUpdateHealth(PacketUpdateHelath packetUpdateHelath) + { + model.controller.player.Health = packetUpdateHelath.Health; + model.controller.player.Food = packetUpdateHelath.Food; + model.controller.player.Saturation = packetUpdateHelath.Saturation; + } + public void HandlePacketMultiBlockChange(PacketMultiBlockChange packetMultiBlockChange) + { + Chunk chunk = model.controller.world.GetChunk(packetMultiBlockChange.chunkXPosiiton, packetMultiBlockChange.chunkZPosition); + if(packetMultiBlockChange.metadata != null) + { + PacketBuffer buff = new PacketBuffer(packetMultiBlockChange.metadata); + for (int i = 0; i < packetMultiBlockChange.size; i++) + { + short short1 = buff.ReadShort(); + short short2 = buff.ReadShort(); + int ID = short2 >> 4 & 4095; + int x = short1 >> 12 & 15; + int z = short1 >> 8 & 15; + int y = short1 & 255; + chunk.updateBlock(x, y, z, ID); + } + } + } } } diff --git a/MoBot/Protocol/PacketBlockChange.cs b/MoBot/Protocol/PacketBlockChange.cs new file mode 100644 index 0000000..7b7973e --- /dev/null +++ b/MoBot/Protocol/PacketBlockChange.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; + +namespace MoBot.Protocol +{ + class PacketBlockChange : Packet + { + public int X, Z; + public byte Y; + public int BlockID; + public byte BlockMetadata; + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketBlockChange(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + X = buff.ReadInt(); + Y = buff.ReadByte(); + Z = buff.ReadInt(); + BlockID = buff.ReadVarInt(); + BlockMetadata = buff.ReadByte(); + } + + public override void WritePacketData(PacketBuffer buff) + { + throw new NotImplementedException(); + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketChat.cs b/MoBot/Protocol/Packets/Play/PacketChat.cs index fc32fea..8d29a50 100644 --- a/MoBot/Protocol/Packets/Play/PacketChat.cs +++ b/MoBot/Protocol/Packets/Play/PacketChat.cs @@ -9,19 +9,20 @@ namespace MoBot.Protocol.Packets.Play { class PacketChat : Packet { + public String message; public override void HandlePacket(IHandler handler) { - throw new NotImplementedException(); + handler.HandlePacketChat(this); } public override void ReadPacketData(PacketBuffer buff) { - throw new NotImplementedException(); + message = buff.ReadString(); } public override void WritePacketData(PacketBuffer buff) { - throw new NotImplementedException(); + buff.WriteString(message); } } } diff --git a/MoBot/Protocol/Packets/Play/PacketChunkData.cs b/MoBot/Protocol/Packets/Play/PacketChunkData.cs new file mode 100644 index 0000000..9c4ab98 --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketChunkData.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; +using MinecraftEmuPTS.GameData; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketChunkData : Packet + { + public Chunk chunk; + public byte[] ChunkData; + public int Length; + public bool RemoveChunk; + public int x, z; + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketChunkData(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + x = buff.ReadInt(); + z = buff.ReadInt(); + bool groundUp = buff.ReadBool(); + short pbitmap = buff.ReadShort(); + short abitmap = buff.ReadShort(); + Length = buff.ReadInt(); + ChunkData = buff.ReadBytes(Length); + chunk = new Chunk(x, z, pbitmap, abitmap, groundUp, true); + RemoveChunk = pbitmap == 0; + } + + public override void WritePacketData(PacketBuffer buff) + { + throw new NotImplementedException(); + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketDestroyEntities.cs b/MoBot/Protocol/Packets/Play/PacketDestroyEntities.cs new file mode 100644 index 0000000..258f2fc --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketDestroyEntities.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketDestroyEntities : Packet + { + public byte Length; + public int[] IDList; + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketDestroyEntities(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + Length = buff.ReadByte(); + IDList = new int[Length]; + for (int i = 0; i < Length; i++) + IDList[i] = buff.ReadInt(); + } + + public override void WritePacketData(PacketBuffer buff) + { + throw new NotImplementedException(); + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketEntity.cs b/MoBot/Protocol/Packets/Play/PacketEntity.cs new file mode 100644 index 0000000..89eece7 --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketEntity.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketEntity : Packet + { + public int EntityID; + public double x, y, z; + + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketEntity(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + EntityID = buff.ReadInt(); + } + + public override void WritePacketData(PacketBuffer buff) + { + throw new NotImplementedException(); + } + + public class PacketEntityMove : PacketEntity + { + public override void ReadPacketData(PacketBuffer buff) + { + base.ReadPacketData(buff); + x = (sbyte)buff.ReadByte() / 32.0; + y = (sbyte)buff.ReadByte() / 32.0; + z = (sbyte)buff.ReadByte() / 32.0; + } + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketEntityTeleport.cs b/MoBot/Protocol/Packets/Play/PacketEntityTeleport.cs new file mode 100644 index 0000000..d138757 --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketEntityTeleport.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketEntityTeleport : Packet + { + public int EntityID; + public double x, y, z; + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketEntityTeleport(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + EntityID = buff.ReadInt(); + x = buff.ReadInt() / 32.0; + y = buff.ReadInt() / 32.0; + z = buff.ReadInt() / 32.0; + } + + public override void WritePacketData(PacketBuffer buff) + { + throw new NotImplementedException(); + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketHeldItemChange.cs b/MoBot/Protocol/Packets/Play/PacketHeldItemChange.cs new file mode 100644 index 0000000..3980dcf --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketHeldItemChange.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketHeldItemChange : Packet + { + public byte Slot; + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketHeldItemChange(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + Slot = buff.ReadByte(); + } + + public override void WritePacketData(PacketBuffer buff) + { + buff.WriteByte(Slot); + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketJoinGame.cs b/MoBot/Protocol/Packets/Play/PacketJoinGame.cs index 36df891..32417a0 100644 --- a/MoBot/Protocol/Packets/Play/PacketJoinGame.cs +++ b/MoBot/Protocol/Packets/Play/PacketJoinGame.cs @@ -9,14 +9,26 @@ namespace MoBot.Protocol.Packets.Play { class PacketJoinGame : Packet { + public int EntityID; + public byte Gamemode; + public int Dimension; + public byte Difficulty; + public byte MaxPlayers; + public String LevelType; + public override void HandlePacket(IHandler handler) { - throw new NotImplementedException(); + handler.HandlePacketJoinGame(this); } public override void ReadPacketData(PacketBuffer buff) { - throw new NotImplementedException(); + EntityID = buff.ReadInt(); + Gamemode = buff.ReadByte(); + Dimension = buff.ReadByte(); + Difficulty = buff.ReadByte(); + MaxPlayers = buff.ReadByte(); + LevelType = buff.ReadString(); } public override void WritePacketData(PacketBuffer buff) diff --git a/MoBot/Protocol/Packets/Play/PacketMapChunk.cs b/MoBot/Protocol/Packets/Play/PacketMapChunk.cs new file mode 100644 index 0000000..11a8b64 --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketMapChunk.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; +using MinecraftEmuPTS.GameData; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketMapChunk : Packet + { + public short ChunkNumber; + public int DataLength; + public bool Flag; + public Chunk[] chunks; + public byte[] ChunkData; + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketMapChunk(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + ChunkNumber = buff.ReadShort(); + DataLength = buff.ReadInt(); + Flag = buff.ReadBool(); + ChunkData = buff.ReadBytes(DataLength); + chunks = new Chunk[ChunkNumber]; + for(int i=0; i 0) + metadata = buff.ReadBytes(length); + } + + public override void WritePacketData(PacketBuffer buff) + { + throw new NotImplementedException(); + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketPlayerAbilities.cs b/MoBot/Protocol/Packets/Play/PacketPlayerAbilities.cs new file mode 100644 index 0000000..2c7b3d2 --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketPlayerAbilities.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketPlayerAbilities : Packet + { + public byte Flags; + public float FlyingSpeed; + public float ViewModifier; + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketPlayerAbliities(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + Flags = buff.ReadByte(); + FlyingSpeed = buff.ReadSingle(); + ViewModifier = buff.ReadSingle(); + } + + public override void WritePacketData(PacketBuffer buff) + { + buff.WriteByte(Flags); + buff.WriteSingle(FlyingSpeed); + buff.WriteSingle(ViewModifier); + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketPlayerPosLook.cs b/MoBot/Protocol/Packets/Play/PacketPlayerPosLook.cs index 506cb9c..50fda3d 100644 --- a/MoBot/Protocol/Packets/Play/PacketPlayerPosLook.cs +++ b/MoBot/Protocol/Packets/Play/PacketPlayerPosLook.cs @@ -9,19 +9,35 @@ namespace MoBot.Protocol.Packets.Play { class PacketPlayerPosLook : Packet { + public double X; + public double Y; + public double Z; + public float pitch; + public float yaw; + public bool onGround; public override void HandlePacket(IHandler handler) { - throw new NotImplementedException(); + handler.HandlePacketPlayerPosLook(this); } public override void ReadPacketData(PacketBuffer buff) { - throw new NotImplementedException(); + X = buff.ReadDouble(); + Y = buff.ReadDouble(); + Z = buff.ReadDouble(); + yaw = buff.ReadSingle(); + pitch = buff.ReadSingle(); + onGround = buff.ReadBool(); } public override void WritePacketData(PacketBuffer buff) { - throw new NotImplementedException(); + buff.WriteDouble(X); + buff.WriteDouble(Y); + buff.WriteDouble(Z); + buff.WriteSingle(yaw); + buff.WriteSingle(pitch); + buff.WriteBool(onGround); } } } diff --git a/MoBot/Protocol/Packets/Play/PacketSetSlot.cs b/MoBot/Protocol/Packets/Play/PacketSetSlot.cs new file mode 100644 index 0000000..0198100 --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketSetSlot.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; +using MoBot.Structure.Game; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketSetSlot : Packet + { + public byte WindowID; + public short Slot; + public Item item; + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketSetSlot(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + WindowID = buff.ReadByte(); + Slot = buff.ReadShort(); + item = Packet.ReadItem(buff); + } + + public override void WritePacketData(PacketBuffer buff) + { + throw new NotImplementedException(); + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketSpawnMob.cs b/MoBot/Protocol/Packets/Play/PacketSpawnMob.cs new file mode 100644 index 0000000..42a5bc2 --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketSpawnMob.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketSpawnMob : Packet + { + public int EntityID; + public byte Type; + public double X, Y, Z; + + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketSpawnMoob(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + EntityID = buff.ReadVarInt(); + Type = buff.ReadByte(); + X = buff.ReadInt() / 32.0; + Y = buff.ReadInt() / 32.0; + Z = buff.ReadInt() / 32.0; + } + + public override void WritePacketData(PacketBuffer buff) + { + throw new NotImplementedException(); + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketUpdateHelath.cs b/MoBot/Protocol/Packets/Play/PacketUpdateHelath.cs new file mode 100644 index 0000000..7c8a472 --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketUpdateHelath.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketUpdateHelath : Packet + { + public float Health; + public short Food; + public float Saturation; + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketUpdateHealth(this); + } + + public override void ReadPacketData(PacketBuffer buff) + { + Health = buff.ReadSingle(); + Food = buff.ReadShort(); + Saturation = buff.ReadSingle(); + } + + public override void WritePacketData(PacketBuffer buff) + { + throw new NotImplementedException(); + } + } +} diff --git a/MoBot/Protocol/Packets/Play/PacketWindowItems.cs b/MoBot/Protocol/Packets/Play/PacketWindowItems.cs new file mode 100644 index 0000000..1ec5f91 --- /dev/null +++ b/MoBot/Protocol/Packets/Play/PacketWindowItems.cs @@ -0,0 +1,38 @@ +using MoBot.Structure.Game; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MoBot.Protocol.Handlers; + +namespace MoBot.Protocol.Packets.Play +{ + class PacketWindowItems : Packet + { + public byte WindowID; + public short ItemCount; + public Item[] Items; + + public override void ReadPacketData(PacketBuffer buff) + { + WindowID = buff.ReadByte(); + ItemCount = buff.ReadShort(); + Items = new Item[ItemCount]; + for(int i = 0; i < ItemCount; i++) + { + Items[i] = Packet.ReadItem(buff); + } + } + + public override void WritePacketData(PacketBuffer buff) + { + throw new NotImplementedException(); + } + + public override void HandlePacket(IHandler handler) + { + handler.HandlePacketWindowItems(this); + } + } +} diff --git a/MoBot/Protocol/Threading/BaseThread.cs b/MoBot/Protocol/Threading/BaseThread.cs new file mode 100644 index 0000000..ce97e0a --- /dev/null +++ b/MoBot/Protocol/Threading/BaseThread.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MoBot.Protocol.Threading +{ + class BaseThread + { + protected bool Process = true; + public void Stop() + { + Process = false; + } + } +} diff --git a/MoBot/Structure/ReadingThread.cs b/MoBot/Protocol/Threading/ReadingThread.cs similarity index 90% rename from MoBot/Structure/ReadingThread.cs rename to MoBot/Protocol/Threading/ReadingThread.cs index b14ce3d..e57b65f 100644 --- a/MoBot/Structure/ReadingThread.cs +++ b/MoBot/Protocol/Threading/ReadingThread.cs @@ -1,4 +1,5 @@ using MoBot.Protocol; +using MoBot.Structure; using System; using System.Collections.Generic; using System.Linq; @@ -6,9 +7,9 @@ using System.Threading; using System.Threading.Tasks; -namespace MoBot.Structure +namespace MoBot.Protocol.Threading { - class ReadingThread + class ReadingThread : BaseThread { private object queueLocker = new object(); public Thread readThread { get; private set; } @@ -21,7 +22,7 @@ public ReadingThread(Model model) this.model = model; readThread = new Thread(() => { - while (true) + while (Process) { Packet packet = model.mainChannel.GetPacket(); if (packet != null) @@ -37,7 +38,7 @@ public ReadingThread(Model model) { IsBackground = true }; processThread = new Thread(() => { - while (true) + while (Process) { lock (queueLocker) { diff --git a/MoBot/Protocol/WritingThread.cs b/MoBot/Protocol/Threading/WritingThread.cs similarity index 91% rename from MoBot/Protocol/WritingThread.cs rename to MoBot/Protocol/Threading/WritingThread.cs index 378c150..18a8e03 100644 --- a/MoBot/Protocol/WritingThread.cs +++ b/MoBot/Protocol/Threading/WritingThread.cs @@ -6,9 +6,9 @@ using System.Threading; using System.Threading.Tasks; -namespace MoBot.Protocol +namespace MoBot.Protocol.Threading { - class WritingThread + class WritingThread : BaseThread { private Model model; public Object queueLocker { get; private set; } = new object(); @@ -19,7 +19,7 @@ public WritingThread(Model model) this.model = model; thread = new Thread(() => { - while (true) + while (Process) { lock (queueLocker) { diff --git a/MoBot/Structure/Actions/ActionChatMessage.cs b/MoBot/Structure/Actions/ActionChatMessage.cs new file mode 100644 index 0000000..955c71c --- /dev/null +++ b/MoBot/Structure/Actions/ActionChatMessage.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MoBot.Structure.Actions +{ + class ActionChatMessage : SysAction + { + public String JSONMessage; + } +} diff --git a/MoBot/Structure/Controller.cs b/MoBot/Structure/Controller.cs index bb63cc6..d733dfd 100644 --- a/MoBot/Structure/Controller.cs +++ b/MoBot/Structure/Controller.cs @@ -8,10 +8,15 @@ namespace MoBot.Structure { class Controller { - internal Model model; - internal void HandleConnect() + public Model model; + public void HandleConnect() { model.Connect("151.80.33.194", 24444, "NoliSum"); } + + public void HandleChatMessage(String message) + { + model.controller.SendChatMessage(message); + } } } diff --git a/MoBot/Structure/Decompressor.cs b/MoBot/Structure/Decompressor.cs new file mode 100644 index 0000000..1d93e9c --- /dev/null +++ b/MoBot/Structure/Decompressor.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Text; + +namespace MinecraftEmuPTS.Encription +{ + class Decompressor + { + // ZLib Decompressor. + byte[] thisdata; + + public Decompressor(byte[] data) + { + thisdata = data; + } + + public byte[] decompress() + { + using (var compressedStream = new MemoryStream(thisdata)) + using (var zipStream = new DeflateStream(compressedStream, CompressionMode.Decompress)) + using (var resultStream = new MemoryStream()) + { + zipStream.CopyTo(resultStream); + return resultStream.ToArray(); + } + } + } +} diff --git a/MoBot/Structure/Game/Entity.cs b/MoBot/Structure/Game/Entity.cs new file mode 100644 index 0000000..c44b5c0 --- /dev/null +++ b/MoBot/Structure/Game/Entity.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MoBot.Structure.Game +{ + class Entity + { + } +} diff --git a/MoBot/Structure/Game/GameController.cs b/MoBot/Structure/Game/GameController.cs new file mode 100644 index 0000000..c9dcf7a --- /dev/null +++ b/MoBot/Structure/Game/GameController.cs @@ -0,0 +1,39 @@ +using MinecraftEmuPTS.GameData; +using MoBot.Protocol.Packets.Play; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MoBot.Structure.Game +{ + class GameController + { + private Model model; + public GameController(Model model) + { + this.model = model; + } + + public Dictionary entityList { get; private set; } = new Dictionary(); + public Player player { get; private set; } + public World world { get; private set; } = new World(); + public void CreatePlayer(int UID, String name = "") + { + player = new Player(); + entityList.Add(UID, player); + } + public Mob createMob(int ID, byte Type = 0) + { + Mob mob = new Mob() { Type = Type }; + entityList.Add(ID, mob); + return mob; + } + + public void SendChatMessage(String message) + { + model.SendPacket(new PacketChat { message = message }); + } + } +} diff --git a/MoBot/Structure/Game/Item.cs b/MoBot/Structure/Game/Item.cs new file mode 100644 index 0000000..c79db25 --- /dev/null +++ b/MoBot/Structure/Game/Item.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MoBot.Structure.Game +{ + class Item + { + public short ID = 0; + public byte ItemCount; + public short ItemDamage; + + public byte[] NBTData; + } +} diff --git a/MoBot/Structure/Game/LivingEntity.cs b/MoBot/Structure/Game/LivingEntity.cs new file mode 100644 index 0000000..1c7afbc --- /dev/null +++ b/MoBot/Structure/Game/LivingEntity.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MoBot.Structure.Game +{ + class LivingEntity : Entity + { + public double x, y, z; + public float yaw, pitch; + public float Health; + } +} diff --git a/MoBot/Structure/Game/Mob.cs b/MoBot/Structure/Game/Mob.cs new file mode 100644 index 0000000..f40ade1 --- /dev/null +++ b/MoBot/Structure/Game/Mob.cs @@ -0,0 +1,7 @@ +namespace MoBot.Structure.Game +{ + class Mob : LivingEntity + { + public byte Type; + } +} \ No newline at end of file diff --git a/MoBot/Structure/Game/Player.cs b/MoBot/Structure/Game/Player.cs new file mode 100644 index 0000000..dabd991 --- /dev/null +++ b/MoBot/Structure/Game/Player.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MoBot.Structure.Game +{ + class Player : LivingEntity + { + public Item[] inventory = new Item[45]; + public int HeldItem = 0; + + public short Food; + public float Saturation; + } +} diff --git a/MoBot/Structure/Game/World/Block.cs b/MoBot/Structure/Game/World/Block.cs new file mode 100644 index 0000000..0013c29 --- /dev/null +++ b/MoBot/Structure/Game/World/Block.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MinecraftEmuPTS.GameData +{ + class Block + { + public static List CollidableBlocks; + public int ID; + public string Name; + public int x; + public int y; + public int z; + public int cx; + public int cz; + + public Block(int id, int X, int Y, int Z,int CX, int CZ) + { + ID = id; + x = X; + y = Y; + z = Z; + cx = CX; + cz = CZ; + + Name = ""; + } + public static void Init() + { + CollidableBlocks = new List(); + CollidableBlocks.Add(0); + CollidableBlocks.Add(6); + CollidableBlocks.Add(30); + CollidableBlocks.Add(31); + CollidableBlocks.Add(32); + CollidableBlocks.Add(50); + CollidableBlocks.Add(51); + CollidableBlocks.Add(65); + CollidableBlocks.Add(66); + CollidableBlocks.Add(69); + CollidableBlocks.Add(70); + CollidableBlocks.Add(72); + CollidableBlocks.Add(76); + CollidableBlocks.Add(77); + CollidableBlocks.Add(106); + CollidableBlocks.Add(131); + CollidableBlocks.Add(143); + CollidableBlocks.Add(147); + CollidableBlocks.Add(148); + } + } +} diff --git a/MoBot/Structure/Game/World/Chunk.cs b/MoBot/Structure/Game/World/Chunk.cs new file mode 100644 index 0000000..dbb3953 --- /dev/null +++ b/MoBot/Structure/Game/World/Chunk.cs @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MinecraftEmuPTS.GameData +{ + class Chunk + { + public int x, z, numBlocks, aBlocks; + public bool lighting, groundup = false; + public List
sections; + + public short pbitmap, abitmap; + private byte[] blocks; + + public Chunk(int X, int Z, short Pbitmap, short Abitmap, bool inLighting, bool Groundup) + { + // Create chunk sections. + groundup = Groundup; + lighting = inLighting; + pbitmap = Pbitmap; + abitmap = Abitmap; + x = X; + z = Z; + sections = new List
(); + + numBlocks = 0; + aBlocks = 0; + + for (int i = 0; i < 16; i++) + { + if (Convert.ToBoolean(Pbitmap & (1 << i))) + { + numBlocks++; // "Sections" + sections.Add(new Section((byte)i)); + } + } + + for (int i = 0; i < 16; i++) + { + if (Convert.ToBoolean(Abitmap & (1 << i))) + { + aBlocks++; + } + } + numBlocks = numBlocks * 4096; + } + private void populate() + { + int offset = 0, current = 0; + + for (int i = 0; i < 16; i++) + { + if (Convert.ToBoolean(pbitmap & (1 << i))) + { + + byte[] temp = new byte[4096]; + + Array.Copy(blocks, offset, temp, 0, 4096); + Section mySection = sections[current]; + + mySection.blocks = temp; + offset += 4096; + current += 1; + } + } + } + public int getBlockId(int Bx, int By, int Bz) + { + Section thisSection = GetSectionByNumber(By); + return thisSection.getBlock(getXinSection(Bx), GetPositionInSection(By), getZinSection(Bz)).ID; + } + public Block getBlock(int Bx, int By, int Bz) + { + Section thisSection = GetSectionByNumber(By); + return thisSection.getBlock(getXinSection(Bx), GetPositionInSection(By), getZinSection(Bz)); + } + + public void updateBlock(int Bx, int By, int Bz, int id) + { + Section thisSection = GetSectionByNumber(By); + thisSection.setBlock(getXinSection(Bx), GetPositionInSection(By), getZinSection(Bz), id); + } + public byte[] getData(byte[] deCompressed) + { + blocks = new byte[numBlocks]; + byte[] temp; + int removeable = numBlocks + aBlocks * 2048; + + if (lighting) + removeable += (numBlocks / 2); + if (groundup) + removeable += 256; + Array.Copy(deCompressed, 0, blocks, 0, numBlocks); + temp = new byte[deCompressed.Length - (numBlocks + removeable)]; + Array.Copy(deCompressed, (numBlocks + removeable), temp, 0, temp.Length); + populate(); + return temp; + } + + #region Helping Methods + private Section GetSectionByNumber(int blockY) + { + Section thisSection = null; + + foreach (Section y in sections) + { + if (y.y == blockY / 16) + { + thisSection = y; + break; + } + } + + if (thisSection == null) + { // Add a new section, if it doesn't exist yet. + thisSection = new Section((byte)(blockY / 16)); + sections.Add(thisSection); + } + + return thisSection; + } + private int getXinSection(int BlockX) + { + return BlockX - (x * 16); + } + private int GetPositionInSection(int blockY) + { + return blockY & (16 - 1); // Credits: SirCmpwn Craft.net + } + private int getZinSection(int BlockZ) + { + return BlockZ - (z * 16); + } + #endregion + } +} diff --git a/MoBot/Structure/Game/World/Section.cs b/MoBot/Structure/Game/World/Section.cs new file mode 100644 index 0000000..989cce7 --- /dev/null +++ b/MoBot/Structure/Game/World/Section.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MinecraftEmuPTS.GameData +{ + class Section + { + public byte[] blocks; + public byte y; + + public Section(byte Y) + { + y = Y; + blocks = new byte[4096]; + } + + public void setBlock(int x, int y, int z, int id) + { + int index = x + (z * 16) + (y * 256); + blocks[index] = (byte)id; + } + + public Block getBlock(int x, int y, int z) + { + int index = x + (z * 16) + (y * 16 * 16); + Block thisBlock = new Block((int)blocks[index], x, y, z, (int)Math.Floor(decimal.Divide(x, 16)), (int)Math.Floor(decimal.Divide(z, 16))); + + return thisBlock; + } + } +} diff --git a/MoBot/Structure/Game/World/World.cs b/MoBot/Structure/Game/World/World.cs new file mode 100644 index 0000000..75c645d --- /dev/null +++ b/MoBot/Structure/Game/World/World.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MinecraftEmuPTS.GameData +{ + class World + { + private List chunks = new List(); + private object chunkLocker = new object(); + public void AddChunk(Chunk c) + { + lock (chunkLocker) + { + chunks.Add(c); + } + } + public void RemoveChunk(int x, int z) + { + lock (chunkLocker) + { + Chunk c = GetChunk(x, z); + if (c != null) + chunks.Remove(c); + } + } + public Block GetBlock(int x, int y, int z) + { + Chunk chunk = this.GetChunk(x, z); + if (chunk != null) + { + return chunk.getBlock(x, y, z); + } + else + return null; + } + public void UpdateBlock(int x, int y, int z, int ID) + { + Chunk chunk = this.GetChunk(x, z); + lock (chunkLocker) + { + if (chunk != null) + { + chunk.updateBlock(x, y, z, ID); + } + } + } + public Chunk GetChunk(int x, int z) + { + lock (chunkLocker) + { + int chunkX = (int)Math.Floor(decimal.Divide(x, 16)); + int chunkZ = (int)Math.Floor(decimal.Divide(z, 16)); + + Chunk thisChunk = null; + + foreach (Chunk b in chunks) + { + if (b.x == chunkX & b.z == chunkZ) + { + thisChunk = b; + break; + } + } + return thisChunk; + } + } + } +} diff --git a/MoBot/Structure/Model.cs b/MoBot/Structure/Model.cs index 668ee1f..64e00ea 100644 --- a/MoBot/Structure/Model.cs +++ b/MoBot/Structure/Model.cs @@ -10,26 +10,32 @@ using MoBot.Protocol.Packets.Handshake; using Newtonsoft.Json.Linq; using MoBot.Protocol.Handlers; +using MoBot.Protocol.Threading; namespace MoBot.Structure { class Model : IObservable { - Viewer viewer; + public IObserver viewer { get; private set; } public Channel mainChannel { get; private set; } public IHandler handler { get; private set; } public String username { get; private set; } public JArray modList { get; private set; } private WritingThread threadWrite; private ReadingThread threadRead; + public Game.GameController controller + { + get; private set; + } public IDisposable Subscribe(IObserver observer) { - viewer = observer as Viewer; + viewer = observer; return null; } public void Connect(String ServerIP, int port, String name) { + #region InitVariables dynamic response = Ping(ServerIP, port); modList = response.modinfo.modList; TcpClient client = new TcpClient(ServerIP, port); @@ -38,15 +44,19 @@ public void Connect(String ServerIP, int port, String name) handler = new ClientHandler(this); threadWrite = new WritingThread(this); threadRead = new ReadingThread(this); + controller = new Game.GameController(this); + #endregion + #region BeginConnect + viewer.OnNext(new ActionConnect { Connected = true }); SendPacket(new PacketHandshake { hostname = ServerIP, port = (ushort)port, nextState = 2, protocolVersion = (int)response.version.protocol}); SendPacket(new PacketLoginStart { Name = name }); + #endregion } public void Disconnect() { viewer.OnNext(new ActionConnect { Connected = false }); - threadWrite.thread.Join(); - threadRead.processThread.Join(); - threadRead.processThread.Join(); + threadWrite.Stop(); + threadRead.Stop(); } public void Message(String message) { diff --git a/MoBot/Structure/Viewer.Designer.cs b/MoBot/Structure/Viewer.Designer.cs index 29a77ad..14970c5 100644 --- a/MoBot/Structure/Viewer.Designer.cs +++ b/MoBot/Structure/Viewer.Designer.cs @@ -30,6 +30,8 @@ private void InitializeComponent() { this.consoleWindow = new System.Windows.Forms.RichTextBox(); this.buttonConnect = new System.Windows.Forms.Button(); + this.chatTextBox = new System.Windows.Forms.TextBox(); + this.buttonSendMessage = new System.Windows.Forms.Button(); this.SuspendLayout(); // // consoleWindow @@ -50,17 +52,37 @@ private void InitializeComponent() this.buttonConnect.UseVisualStyleBackColor = true; this.buttonConnect.Click += new System.EventHandler(this.buttonConnect_Click); // + // chatTextBox + // + this.chatTextBox.Location = new System.Drawing.Point(12, 293); + this.chatTextBox.Name = "chatTextBox"; + this.chatTextBox.Size = new System.Drawing.Size(427, 20); + this.chatTextBox.TabIndex = 2; + // + // buttonSendMessage + // + this.buttonSendMessage.Location = new System.Drawing.Point(457, 293); + this.buttonSendMessage.Name = "buttonSendMessage"; + this.buttonSendMessage.Size = new System.Drawing.Size(101, 20); + this.buttonSendMessage.TabIndex = 3; + this.buttonSendMessage.Text = "Send Message"; + this.buttonSendMessage.UseVisualStyleBackColor = true; + this.buttonSendMessage.Click += new System.EventHandler(this.buttonSendMessage_Click); + // // Viewer // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(583, 336); + this.Controls.Add(this.buttonSendMessage); + this.Controls.Add(this.chatTextBox); this.Controls.Add(this.buttonConnect); this.Controls.Add(this.consoleWindow); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.Name = "Viewer"; this.Text = "MoBot"; this.ResumeLayout(false); + this.PerformLayout(); } @@ -68,5 +90,7 @@ private void InitializeComponent() private System.Windows.Forms.RichTextBox consoleWindow; private System.Windows.Forms.Button buttonConnect; + private System.Windows.Forms.TextBox chatTextBox; + private System.Windows.Forms.Button buttonSendMessage; } } \ No newline at end of file diff --git a/MoBot/Structure/Viewer.cs b/MoBot/Structure/Viewer.cs index de82cf2..711cfe3 100644 --- a/MoBot/Structure/Viewer.cs +++ b/MoBot/Structure/Viewer.cs @@ -31,24 +31,79 @@ public void OnError(Exception error) public void OnNext(SysAction value) { - if(value is ActionConnect) + if (this.InvokeRequired) + { + this.Invoke(new Action(OnNext), value); + return; + } + if (value is ActionConnect) { var connect = value as ActionConnect; if (connect.Connected) consoleWindow.AppendText("Client connected!" + Environment.NewLine); - else - consoleWindow.AppendText("Client disnnected!" + Environment.NewLine); } - else if(value is ActionMessage) + else if (value is ActionMessage) { var message = value as ActionMessage; consoleWindow.AppendText(message.message + Environment.NewLine); } + else if (value is ActionChatMessage) + { + var message = value as ActionChatMessage; + dynamic parsed = Newtonsoft.Json.Linq.JObject.Parse(message.JSONMessage); + consoleWindow.AppendText(message.JSONMessage + Environment.NewLine); + } } private void buttonConnect_Click(object sender, EventArgs e) { mainController.HandleConnect(); } + + private void buttonSendMessage_Click(object sender, EventArgs e) + { + if(chatTextBox.Text != "") + { + mainController.HandleChatMessage(chatTextBox.Text); + chatTextBox.Text = ""; + } + } + + + private static string strip_codes(string text) + { + // Strips the color codes from text. + string smessage = text; + if (smessage.Contains("§")) + { + + smessage = smessage.Replace("§0", ""); + smessage = smessage.Replace("§1", ""); + smessage = smessage.Replace("§2", ""); + smessage = smessage.Replace("§3", ""); + smessage = smessage.Replace("§4", ""); + smessage = smessage.Replace("§5", ""); + smessage = smessage.Replace("§6", ""); + smessage = smessage.Replace("§7", ""); + smessage = smessage.Replace("§8", ""); + smessage = smessage.Replace("§9", ""); + smessage = smessage.Replace("§a", ""); + smessage = smessage.Replace("§b", ""); + smessage = smessage.Replace("§c", ""); + smessage = smessage.Replace("§d", ""); + smessage = smessage.Replace("§e", ""); + smessage = smessage.Replace("§f", ""); + smessage = smessage.Replace("§l", ""); + smessage = smessage.Replace("§r", ""); + smessage = smessage.Replace("§A", ""); + smessage = smessage.Replace("§B", ""); + smessage = smessage.Replace("§C", ""); + smessage = smessage.Replace("§D", ""); + smessage = smessage.Replace("§E", ""); + smessage = smessage.Replace("§F", ""); + + } + return smessage; + } } }