Jump to content

Cancel event in MCGalaxy plugin using C#

Posted

I am trying to make a tone indicators plugin, where when people say something like '\j', it puts JOKING in their chat message

But I can't figure out how to cancel events, since I don't want the player to send the chat message and instead have the server send it with the word "JOKING" in it.

Here's my code incase needed:
 

using System;
using MCGalaxy;
using MCGalaxy.Events.ServerEvents;
using MCGalaxy.Events.PlayerEvents;


namespace PluginToneIndicators
{
    public sealed class ToneIndicators : Plugin 
    {
        public override string name { get { return "ToneIndicators"; } }
        public override string MCGalaxy_Version { get { return "0.1"; } }
        public override string creator { get { return "AllergenX"; } }
        
        public override void Load(bool startup) {
            OnPlayerChatEvent.Register(HandlePlayerChat, Priority.High);
        }
        
        public override void Unload(bool shutdown) {
            OnPlayerChatEvent.Unregister(HandlePlayerChat);
        }

        static void HandlePlayerChat(Player p, string message)
        {
            if (message.Contains("\\j") == true)
            {
                // Cancel the event somehow
                // Ignore the rest of the code from this comment
                Chat.Message(ChatScope.Global, "&aJOKING", null, null, true);
            }
        }
    }
}

 

Solved by Goodly

Go to solution

Featured Replies

p.cancelchat = true;

This is how the chat event should be cancelled.

Edited by deewend

  • Solution

To modify messages sent by players, use OnChatEvent and modify the ref "msg" argument. I believe you will need to be using the latest build of MCGalaxy to do this, though. You can find latest builds here: https://123dmwm.com/MCGalaxy/

https://github.com/ClassiCube/MCGalaxy/blob/master/MCGalaxy/Events/ServerEvents.cs#L107

For instance

public static void OnChat(ChatScope scope, Player source, ref string msg, object arg, ref ChatMessageFilter filter, bool relay) {
	if (message.CaselessContains("sign")) msg += " I STEPPED ON THE SIGN!";
}

 

READ THE RULES BEFORE YOU POST

Create an account or sign in to comment