Skip to content

Commit

Permalink
updated game protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ReezeBL committed Apr 9, 2016
1 parent 20512e6 commit 7f014c6
Show file tree
Hide file tree
Showing 38 changed files with 1,234 additions and 31 deletions.
30 changes: 28 additions & 2 deletions MoBot/MoBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Compile Include="Protocol\Handlers\ClientHandler.cs" />
<Compile Include="Protocol\Handlers\IHandler.cs" />
<Compile Include="Protocol\Packet.cs" />
<Compile Include="Protocol\PacketBlockChange.cs" />
<Compile Include="Protocol\PacketBuffer.cs" />
<Compile Include="Protocol\Packets\Handshake\PacketEncriptionResponse.cs" />
<Compile Include="Protocol\Packets\Handshake\PacketLoginStart.cs" />
Expand All @@ -80,17 +81,42 @@
<Compile Include="Protocol\Packets\Handshake\PacketHandshake.cs" />
<Compile Include="Protocol\Packets\Handshake\PacketResponse.cs" />
<Compile Include="Protocol\Packets\Play\PacketChat.cs" />
<Compile Include="Protocol\Packets\Play\PacketChunkData.cs" />
<Compile Include="Protocol\Packets\Play\PacketCustomPayload.cs" />
<Compile Include="Protocol\Packets\Play\PacketDestroyEntities.cs" />
<Compile Include="Protocol\Packets\Play\PacketEntity.cs" />
<Compile Include="Protocol\Packets\Play\PacketEntityTeleport.cs" />
<Compile Include="Protocol\Packets\Play\PacketHeldItemChange.cs" />
<Compile Include="Protocol\Packets\Play\PacketJoinGame.cs" />
<Compile Include="Protocol\Packets\Play\PacketKeepAlive.cs" />
<Compile Include="Protocol\Packets\Play\PacketMapChunk.cs" />
<Compile Include="Protocol\Packets\Play\PacketMultiBlockChange.cs" />
<Compile Include="Protocol\Packets\Play\PacketPlayerAbilities.cs" />
<Compile Include="Protocol\Packets\Play\PacketPlayerPosLook.cs" />
<Compile Include="Protocol\WritingThread.cs" />
<Compile Include="Protocol\Packets\Play\PacketSetSlot.cs" />
<Compile Include="Protocol\Packets\Play\PacketSpawnMob.cs" />
<Compile Include="Protocol\Packets\Play\PacketUpdateHelath.cs" />
<Compile Include="Protocol\Packets\Play\PacketWindowItems.cs" />
<Compile Include="Protocol\Threading\BaseThread.cs" />
<Compile Include="Protocol\Threading\WritingThread.cs" />
<Compile Include="Structure\Actions\ActionChatMessage.cs" />
<Compile Include="Structure\Actions\ActionConnect.cs" />
<Compile Include="Structure\Actions\ActionMessage.cs" />
<Compile Include="Structure\Controller.cs" />
<Compile Include="Structure\Decompressor.cs" />
<Compile Include="Structure\Game\Entity.cs" />
<Compile Include="Structure\Game\GameController.cs" />
<Compile Include="Structure\Game\Item.cs" />
<Compile Include="Structure\Game\LivingEntity.cs" />
<Compile Include="Structure\Game\Mob.cs" />
<Compile Include="Structure\Game\Player.cs" />
<Compile Include="Structure\Game\World\Block.cs" />
<Compile Include="Structure\Game\World\Chunk.cs" />
<Compile Include="Structure\Game\World\Section.cs" />
<Compile Include="Structure\Game\World\World.cs" />
<Compile Include="Structure\Model.cs" />
<Compile Include="Structure\Actions\SysAction.cs" />
<Compile Include="Structure\ReadingThread.cs" />
<Compile Include="Protocol\Threading\ReadingThread.cs" />
<Compile Include="Structure\Viewer.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
2 changes: 1 addition & 1 deletion MoBot/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<rules>
<!-- Основные log -->
<logger name="*" minlevel="Trace" writeTo="fullLog" />
<logger name="*" minlevel="Info" writeTo="consoleLog" />
<logger name="*" minlevel="Info" writeTo="shortLog" />

<!-- Вспомогательные log -->
<logger name="*" minlevel="Info" writeTo="consoleLog" />
Expand Down
130 changes: 128 additions & 2 deletions MoBot/Protocol/Handlers/ClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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);
}
}
}
}
}
35 changes: 35 additions & 0 deletions MoBot/Protocol/PacketBlockChange.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
7 changes: 4 additions & 3 deletions MoBot/Protocol/Packets/Play/PacketChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
41 changes: 41 additions & 0 deletions MoBot/Protocol/Packets/Play/PacketChunkData.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
32 changes: 32 additions & 0 deletions MoBot/Protocol/Packets/Play/PacketDestroyEntities.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
41 changes: 41 additions & 0 deletions MoBot/Protocol/Packets/Play/PacketEntity.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Loading

0 comments on commit 7f014c6

Please sign in to comment.