Skip to content

Commit

Permalink
Merge pull request #655 from SpiralP/add-cpe-plugin-message
Browse files Browse the repository at this point in the history
Add new CPE type PluginMessage
  • Loading branch information
UnknownShadow200 committed Nov 6, 2021
2 parents e0529d2 + 8427110 commit b5b8675
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 11 deletions.
4 changes: 2 additions & 2 deletions MCGalaxy/Events/PlayerEvents.cs
Expand Up @@ -15,7 +15,7 @@
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System;
using BlockID = System.UInt16;

namespace MCGalaxy.Events.PlayerEvents {
Expand Down Expand Up @@ -316,5 +316,5 @@ public sealed class OnGettingCanSeeEvent : IEvent<OnGettingCanSee> {
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
}
}
}
}
10 changes: 10 additions & 0 deletions MCGalaxy/Events/ServerEvents.cs
Expand Up @@ -111,4 +111,14 @@ public sealed class OnChatEvent : IEvent<OnChat> {
}
}
}

public delegate void OnPluginMessageReceived(Player p, byte channel, byte[] data);
/// <summary> Called when a player sends a PluginMessage CPE packet to the server. </summary>
public sealed class OnPluginMessageReceivedEvent : IEvent<OnPluginMessageReceived> {

public static void Call(Player p, byte channel, byte[] data) {
if (handlers.Count == 0) return;
CallCommon(pl => pl(p, channel, data));
}
}
}
2 changes: 2 additions & 0 deletions MCGalaxy/Network/CPESupport.cs
Expand Up @@ -90,6 +90,7 @@ public class CpeExt
public const string VelocityControl = "VelocityControl";
public const string CustomParticles = "CustomParticles";
public const string CustomModels = "CustomModels";
public const string PluginMessages = "PluginMessages";
}

public sealed class CpeExtension
Expand Down Expand Up @@ -147,6 +148,7 @@ public sealed class CpeExtension
new CpeExtension(CpeExt.VelocityControl, "Allows adjusting velocity of players"),
new CpeExtension(CpeExt.CustomParticles, "Allows defining and spawning custom particles"),
new CpeExtension(CpeExt.CustomModels, "Allows defining custom models for entities", 2),
new CpeExtension(CpeExt.PluginMessages, "Allows sending and receiving plugin messages from clients"),
#if TEN_BIT_BLOCKS
new CpeExtension(CpeExt.ExtBlocks, "Allows using block IDs over 255 in block definitions"),
#endif
Expand Down
1 change: 1 addition & 0 deletions MCGalaxy/Network/Packets/Opcode.cs
Expand Up @@ -76,5 +76,6 @@ public static class Opcode {
public const byte CpeDefineModel = 50;
public const byte CpeDefineModelPart = 51;
public const byte CpeUndefineModel = 52;
public const byte CpePluginMessage = 53;
}
}
11 changes: 11 additions & 0 deletions MCGalaxy/Network/Packets/Packet.cs
Expand Up @@ -638,6 +638,17 @@ public static class Packet
return buffer;
}

public const int PluginMessageDataLength = 64;
public static byte[] PluginMessage(byte channel, byte[] data) {
byte[] buffer = new byte[66];
int i = 0;
buffer[i++] = Opcode.CpePluginMessage;
buffer[i++] = channel;
Array.Copy(data, 0, buffer, i, PluginMessageDataLength);

return buffer;
}

#endregion


Expand Down
19 changes: 10 additions & 9 deletions MCGalaxy/Network/Player.Networking.cs
Expand Up @@ -49,15 +49,16 @@ public partial class Player : IDisposable, INetProtocol

int HandlePacket(byte[] buffer, int offset, int left) {
switch (buffer[offset]) {
case Opcode.Ping: return 1;
case Opcode.Handshake: return HandleLogin(buffer, offset, left);
case Opcode.SetBlockClient: return HandleBlockchange(buffer, offset, left);
case Opcode.EntityTeleport: return HandleMovement(buffer, offset, left);
case Opcode.Message: return HandleChat(buffer, offset, left);
case Opcode.CpeExtInfo: return HandleExtInfo(buffer, offset, left);
case Opcode.CpeExtEntry: return HandleExtEntry(buffer, offset, left);
case Opcode.CpePlayerClick: return HandlePlayerClicked(buffer, offset, left);
case Opcode.CpeTwoWayPing: return HandleTwoWayPing(buffer, offset, left);
case Opcode.Ping: return 1;
case Opcode.Handshake: return HandleLogin(buffer, offset, left);
case Opcode.SetBlockClient: return HandleBlockchange(buffer, offset, left);
case Opcode.EntityTeleport: return HandleMovement(buffer, offset, left);
case Opcode.Message: return HandleChat(buffer, offset, left);
case Opcode.CpeExtInfo: return HandleExtInfo(buffer, offset, left);
case Opcode.CpeExtEntry: return HandleExtEntry(buffer, offset, left);
case Opcode.CpePlayerClick: return HandlePlayerClicked(buffer, offset, left);
case Opcode.CpeTwoWayPing: return HandleTwoWayPing(buffer, offset, left);
case Opcode.CpePluginMessage: return HandlePluginMessage(buffer, offset, left);

case Opcode.CpeCustomBlockSupportLevel:
return left < 2 ? 0 : 2; // only ever one level anyways
Expand Down
13 changes: 13 additions & 0 deletions MCGalaxy/Player/Player.Handlers.cs
Expand Up @@ -23,6 +23,7 @@
using MCGalaxy.Commands.Chatting;
using MCGalaxy.DB;
using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Events.ServerEvents;
using MCGalaxy.Games;
using MCGalaxy.Maths;
using MCGalaxy.Network;
Expand Down Expand Up @@ -325,6 +326,18 @@ public partial class Player : IDisposable
}
return size;
}

int HandlePluginMessage(byte[] buffer, int offset, int left) {
const int size = 1 + 1 + Packet.PluginMessageDataLength;
if (left < size) return 0;

byte channel = buffer[offset + 1];
byte[] data = new byte[Packet.PluginMessageDataLength];
Array.Copy(buffer, offset + 2, data, 0, Packet.PluginMessageDataLength);
OnPluginMessageReceivedEvent.Call(this, channel, data);

return size;
}

int CurrentEnvProp(EnvProp i, Zone zone) {
int value = Server.Config.GetEnvProp(i);
Expand Down

0 comments on commit b5b8675

Please sign in to comment.