public void HandleEvent(IEventEmitter emitter, UserChatEvent @event)
        {
            var session = GetScriptContext(@event.User);

            if (session == null)
            {
                return;
            }

            @event.IsCancelled = true;

            if (@event.Message == "exit")
            {
                StopSession(@event.User);
                return;
            }

            var cmd    = @event.Message;
            var result = session.Eval(cmd);

            if (result.HasReturn)
            {
                @event.User.SendMessage("> " + result.Return, ConsoleColor.Gray);
            }
        }
Exemple #2
0
        internal bool _EmitPlayerChat(object user, string text)
        {
            if (user == null || !(user is User castedUser) || !castedUser.LoggedIn)
            {
                return(true);
            }

            EcoPlayer ecoPlayer = (EcoPlayer)playerManager.GetOnlinePlayerById(castedUser.SteamId);

            if (ecoPlayer == null)
            {
                logger.LogWarning("An unknown player has chatted. Please report this to a Rocket.Eco developer!");
                return(false);
            }

            if (text.StartsWith("/", StringComparison.InvariantCulture))
            {
                logger.LogDebug($"Emitting PreCommandExecutionEvent [{ecoPlayer.Id}, {text}]");
                PreCommandExecutionEvent commandEvent = new PreCommandExecutionEvent(ecoPlayer.User, text.Remove(0, 1));
                eventManager.Emit(this, commandEvent);

                if (commandEvent.IsCancelled)
                {
                    ecoPlayer.SendErrorMessage("Execution of your command has been cancelled!");

                    return(true);
                }

                taskScheduler
                .ScheduleNextFrame(this, () =>
                {
                    bool wasHandled = true;

                    try
                    {
                        wasHandled = commandHandler.HandleCommand(ecoPlayer.User, text.Remove(0, 1), string.Empty);
                    }
                    catch (NotEnoughPermissionsException)
                    {
                        ecoPlayer.SendErrorMessage("You do not have enough permission to execute this command!");
                    }
                    catch (Exception e)
                    {
                        logger.LogError($"{ecoPlayer.Name} failed to execute the command `{text.Remove(0, 1).Split(' ')[0]}`!");
                        logger.LogError($"{e.Message}\n{e.StackTrace}");

                        ecoPlayer.SendErrorMessage("A runtime error occurred while executing this command, please contact an administrator!");

                        return;
                    }

                    if (!wasHandled)
                    {
                        ecoPlayer.SendErrorMessage("That command could not be found!");
                    }
                }, "Player Chat Event");

                return(true);
            }

            logger.LogDebug($"Emitting UserChatEvent [{ecoPlayer.Id}, {text}]");
            UserChatEvent chatEvent = new UserChatEvent(ecoPlayer.User, text)
            {
                IsCancelled = false
            };

            eventManager.Emit(this, chatEvent);

            string commandCancelled = chatEvent.IsCancelled ? "[CANCELLED] " : "";

            logger.LogInformation($"{commandCancelled}[{ecoPlayer.Id}] {ecoPlayer.Name}: {text}");

            return(chatEvent.IsCancelled);
        }