This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Protocol.HandlerReceive.cs
139 lines (110 loc) · 4.98 KB
/
Protocol.HandlerReceive.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Aragas.Core.Data;
using Aragas.Core.Packets;
using MineLib.Core.Data;
using MineLib.Core.Data.Anvil;
using MineLib.Core.Data.Structs;
using MineLib.Core.Events.ReceiveEvents;
using MineLib.Core.Events.ReceiveEvents.Anvil;
namespace ProtocolModern
{
public sealed partial class Protocol
{
private Dictionary<Type, List<Func<ProtobufPacket, Task>>> CustomPacketHandlers { get; }
public override void RegisterReceiving(Type packetType, Func<ProtobufPacket, Task> func)
{
if (!packetType.GetTypeInfo().IsSubclassOf(typeof(ProtobufPacket)))
throw new InvalidOperationException("Type type must implement Aragas.Core.Packets.ProtobufPacket");
if (CustomPacketHandlers.ContainsKey(packetType))
CustomPacketHandlers[packetType].Add(func);
else
CustomPacketHandlers.Add(packetType, new List<Func<ProtobufPacket, Task>> {func});
}
protected override void DeregisterReceiving(Type packetType, Func<ProtobufPacket, Task> func)
{
if (!packetType.GetTypeInfo().IsSubclassOf(typeof(ProtobufPacket)))
throw new InvalidOperationException("Type type must implement Aragas.Core.Packets.ProtobufPacket");
if (CustomPacketHandlers.ContainsKey(packetType))
CustomPacketHandlers[packetType].Remove(func);
}
protected override void DoReceiving(Type packetType, ProtobufPacket packet)
{
if (!packetType.GetTypeInfo().IsSubclassOf(typeof(ProtobufPacket)))
throw new InvalidOperationException("Type type must implement Aragas.Core.Packets.ProtobufPacket");
if (CustomPacketHandlers.ContainsKey(packetType))
foreach (var func in CustomPacketHandlers[packetType])
func(packet);
}
#region InnerReceiving
private void OnChatMessage(string message)
{
Minecraft.DoReceiveEvent(typeof (ChatMessageEvent), new ChatMessageEvent(message));
}
#region Anvil
private void OnChunk(Chunk chunk)
{
Minecraft.DoReceiveEvent(typeof (ChunkEvent), new ChunkEvent(chunk));
}
private void OnChunkArray(Chunk[] chunks)
{
Minecraft.DoReceiveEvent(typeof (ChunkArrayEvent), new ChunkArrayEvent(chunks));
}
private void OnBlockChange(Position location, int block)
{
Minecraft.DoReceiveEvent(typeof(BlockChangeEvent), new BlockChangeEvent(location, block));
}
private void OnMultiBlockChange(Coordinates2D chunkLocation, Record[] records)
{
Minecraft.DoReceiveEvent(typeof(MultiBlockChangeEvent), new MultiBlockChangeEvent(chunkLocation, records));
}
private void OnBlockAction(Position location, int block, object blockAction)
{
Minecraft.DoReceiveEvent(typeof(BlockActionEvent), new BlockActionEvent(location, block, blockAction));
}
private void OnBlockBreakAction(int entityID, Position location, byte stage)
{
Minecraft.DoReceiveEvent(typeof(BlockBreakActionEvent), new BlockBreakActionEvent(entityID, location, stage));
}
#endregion
private void OnPlayerPosition(Vector3 position)
{
Minecraft.DoReceiveEvent(typeof (PlayerPositionEvent), new PlayerPositionEvent(position));
}
private void OnPlayerLook(Vector3 look)
{
Minecraft.DoReceiveEvent(typeof (PlayerLookEvent), new PlayerLookEvent(look));
}
private void OnHeldItemChange(byte slot)
{
Minecraft.DoReceiveEvent(typeof(HeldItemChangeEvent), new HeldItemChangeEvent(slot));
}
private void OnSpawnPoint(Position location)
{
Minecraft.DoReceiveEvent(typeof(SpawnPointEvent), new SpawnPointEvent(location));
}
private void OnUpdateHealth(float health, int food, float foodSaturation)
{
Minecraft.DoReceiveEvent(typeof(UpdateHealthEvent), new UpdateHealthEvent(health, food, foodSaturation));
}
private void OnRespawn(object gameInfo)
{
Minecraft.DoReceiveEvent(typeof(RespawnEvent), new RespawnEvent(gameInfo));
}
private void OnAction(int entityID, int action)
{
Minecraft.DoReceiveEvent(typeof(ActionEvent), new ActionEvent(entityID, action));
}
private void OnSetExperience(float experienceBar, int level, int totalExperience)
{
Minecraft.DoReceiveEvent(typeof(SetExperienceEvent), new SetExperienceEvent(experienceBar, level, totalExperience));
}
private void OnTimeUpdate(long worldAge, long timeOfDay)
{
Minecraft.DoReceiveEvent(typeof(TimeUpdateEvent), new TimeUpdateEvent(worldAge, timeOfDay));
}
#endregion InnerReceiving
}
}