Exemple #1
0
        internal static bool ExecuteEvent(string name, string json)
        {
            try
            {
                if (name == "finish-wrapper")
                {
                    Server.Start();
                    Server.Logger.Success("Finish-Trigger received! Wrapper has been finished and is now completely functional!");
                    return(false);
                }

                if (name == "trigger-event")
                {
                    return(Server.ExecuteServerEvent(new ReturnData(json)));
                }

                if (name == "interop-luaexp")
                {
                    LuaInterop.CallInterop(json);
                }

                if (name == "trigger-remote-event")
                {
                    ReturnData data      = new ReturnData(json);
                    string[]   args      = data.ValuesAsStrings("args");
                    IPlayer    player    = Server.PlayerPool.GetEntity(data.Value <int>("player"));
                    string     eventName = data.Value <string>("eventName");
                    Registry <RemoteEvent> .Item remoteItem = Server.RemoteEventRegistry.GetItem(item => item.Data.Key == eventName);
                    if (remoteItem == null)
                    {
                        return(false);
                    }
                    remoteItem.Invoke(Converts.Convert(args, remoteItem.Invoker.GetParameters(), player, false, remoteItem.Invoker.Name + " -> " + remoteItem.Data.Key));
                }

                if (name == "trigger-command")
                {
                    ReturnData data   = new ReturnData(json);
                    string[]   args   = data.ValuesAsStrings("args");
                    IPlayer    player = Server.PlayerPool.GetEntity(data.Value <int>("player"));
                    Registry <Command> .Item commandItem =
                        Server.CommandRegistry.GetItem(item => item.Data.Name == data.Value <string>("commandName"));
                    if (commandItem == null)
                    {
                        Server.ExecuteInternalServerEvent(EventType.PlayerCommandFailed, player,
                                                          data.Value <string>("commandName"), args, CommandError.NotExisting);
                        return(false);
                    }

                    if (Server.ExecuteInternalServerEvent(EventType.PlayerPreCommand, player, data.Value <string>("commandName"), args))
                    {
                        int requiredParams = commandItem.Invoker.GetParameters().Count(info => !info.IsOptional) - 1;
                        if (requiredParams > args.Length)
                        {
                            Server.ExecuteInternalServerEvent(EventType.PlayerCommandFailed, player,
                                                              data.Value <string>("commandName"), args, CommandError.TooFewArguments);
                            return(false);
                        }

                        object[] arr = Converts.Convert(args, commandItem.Invoker.GetParameters(), player, true,
                                                        "Command");
                        if (arr == null)
                        {
                            return(false);
                        }
                        commandItem.Invoke(arr);
                    }
                }
            }
            catch (Exception e)
            {
                Server.Logger.Error("An unhandled exception occurred!", e);
            }
            return(false);
        }