Beispiel #1
0
        /// <summary>
        /// Sets the option's value, it's not recommended to call this directly, call derivatives instead.
        /// </summary>
        /// <remarks>
        /// Does nothing when the value type differs or when the value matches the current value.
        /// </remarks>
        /// <param name="value">The new value</param>
        /// <param name="raiseEvents">Whether or not to raise events</param>
        private protected void SetValue(object value, bool raiseEvents)
        {
            if (value?.GetType() != Value?.GetType() || Value == value)
            {
                return;                                                         // Refuse value updates that don't match the option type
            }
            if (raiseEvents && OnValueChanged != null && AmongUsClient.Instance?.AmHost == true && PlayerControl.LocalPlayer)
            {
                object lastValue = value;

                OptionOnValueChangedEventArgs args = OnValueChangedEventArgs(value, Value);
                foreach (EventHandler <OptionOnValueChangedEventArgs> handler in OnValueChanged.GetInvocationList())
                {
                    handler(this, args);

                    if (args.Value.GetType() != value.GetType())
                    {
                        args.Value  = lastValue;
                        args.Cancel = false;

                        EssentialsPlugin.Logger.LogWarning($"A handler for option \"{Name}\" attempted to change value type, ignored.");
                    }

                    lastValue = args.Value;

                    if (args.Cancel)
                    {
                        return;              // Handler cancelled value change.
                    }
                }

                value = args.Value;
            }

            if (OldValue != Value)
            {
                OldValue = Value;
            }

            Value = value;

            if (GameSetting != null && AmongUsClient.Instance?.AmHost == true && PlayerControl.LocalPlayer)
            {
                Rpc.Send(new (string, CustomOptionType, object)[] { this });                                                                                            //Rpc.Send(this);
        /// <summary>
        /// Sets the option's value.
        /// </summary>
        /// <remarks>
        /// Does nothing when the value type differs or when the value matches the current value.
        /// </remarks>
        /// <param name="value">The new value</param>
        /// <param name="raiseEvents">Whether or not to raise events</param>
        protected virtual void SetValue(object value, bool raiseEvents)
        {
            if (value?.GetType() != Value?.GetType() || Value == value)
            {
                return;                                                         // Refuse value updates that don't match the option type.
            }
            if (raiseEvents && OnValueChanged != null && AmongUsClient.Instance?.AmHost == true && PlayerControl.LocalPlayer)
            {
                object lastValue = value;

                OptionOnValueChangedEventArgs args = OnValueChangedEventArgs(value, Value);
                foreach (EventHandler <OptionOnValueChangedEventArgs> handler in OnValueChanged.GetInvocationList())
                {
                    handler(this, args);

                    if (args.Value.GetType() != value.GetType())
                    {
                        args.Value  = lastValue;
                        args.Cancel = false;

                        EssentialsPlugin.Logger.LogWarning($"A handler for option \"{Name}\" attempted to change value type, ignored.");
                    }

                    lastValue = args.Value;

                    if (args.Cancel)
                    {
                        return;              // Handler cancelled value change.
                    }
                }

                value = args.Value;
            }

            if (OldValue != Value)
            {
                OldValue = Value;
            }

            Value = value;

            //if (SendRpc && GameSetting != null && AmongUsClient.Instance?.AmHost == true && PlayerControl.LocalPlayer) Rpc.Send(new (string, CustomOptionType, object)[] { this });
            if (SendRpc && GameObject != null && AmongUsClient.Instance?.AmHost == true && PlayerControl.LocalPlayer)
            {
                Rpc.Instance.Send(this);
            }

            UpdateGameObject();

            if (raiseEvents)
            {
                ValueChanged?.SafeInvoke(this, ValueChangedEventArgs(value, Value), nameof(ValueChanged));
            }

            if (GameObject == null)
            {
                return;                     // Game object does not exist, menu is closed
            }
            // Refresh the value of all options in the menu, in case an option affects another.
            try
            {
                GameOptionsMenu optionsMenu = Object.FindObjectOfType <GameOptionsMenu>();

                if (optionsMenu == null)
                {
                    return;
                }

                for (int i = 0; i < optionsMenu.Children.Length; i++)
                {
                    OptionBehaviour optionBehaviour = optionsMenu.Children[i];
                    optionBehaviour.enabled = false;
                    optionBehaviour.enabled = true;
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// Sets the option's value, it's not recommended to call this directly, call derivatives instead.
        /// </summary>
        /// <remarks>
        /// Does nothing when the value type differs or when the value matches the current value.
        /// </remarks>
        /// <param name="value">The new value</param>
        /// <param name="raiseEvents">Whether or not to raise events</param>
        private protected void SetValue(object value, bool raiseEvents)
        {
            if (value?.GetType() != Value?.GetType() || Value == value)
            {
                return;                                                         // Refuse value updates that don't match the option type
            }
            if (raiseEvents && OnValueChanged != null && AmongUsClient.Instance && PlayerControl.LocalPlayer && AmongUsClient.Instance.AmHost)
            {
                object lastValue = value;

                OptionOnValueChangedEventArgs args = OnValueChangedEventArgs(value, Value);
                foreach (EventHandler <OptionOnValueChangedEventArgs> handler in OnValueChanged.GetInvocationList())
                {
                    handler(this, args);

                    if (args.Value.GetType() != value.GetType())
                    {
                        args.Value  = lastValue;
                        args.Cancel = false;

                        EssentialsPlugin.Logger.LogWarning($"A handler for option \"{Name}\" attempted to change value type, ignored.");
                    }

                    lastValue = args.Value;

                    if (args.Cancel)
                    {
                        return;              // Handler cancelled value change.
                    }
                }

                value = args.Value;
            }

            if (OldValue != Value)
            {
                OldValue = Value;
            }

            Value = value;

            if (GameSetting != null && AmongUsClient.Instance && PlayerControl.LocalPlayer && AmongUsClient.Instance.AmHost)
            {
                Rpc.Instance.Send(new Rpc.Data(this));
            }

            try
            {
                if (GameSetting is ToggleOption toggle)
                {
                    bool newValue = (bool)Value;

                    toggle.oldValue = newValue;
                    if (toggle.CheckMark != null)
                    {
                        toggle.CheckMark.enabled = newValue;
                    }
                }
                else if (GameSetting is NumberOption number)
                {
                    float newValue = (float)Value;

                    number.Value          = number.Field_3 = newValue;
                    number.ValueText.Text = ToString();
                }
                else if (GameSetting is StringOption str)
                {
                    int newValue = (int)Value;

                    str.Value          = str.oldValue = newValue;
                    str.ValueText.Text = ToString();
                }
                else if (GameSetting is KeyValueOption kv)
                {
                    int newValue = (int)Value;

                    kv.Selected       = kv.oldValue = newValue;
                    kv.ValueText.Text = ToString();
                }
            }
            catch (Exception e)
            {
                EssentialsPlugin.Logger.LogWarning($"Failed to update game setting value for option \"{Name}\": {e}");
            }

            if (raiseEvents)
            {
                ValueChanged?.SafeInvoke(this, ValueChangedEventArgs(value, Value));
            }

            /*{
             *  OptionValueChangedEventArgs args = ValueChangedEventArgs(value, Value);
             *  foreach (EventHandler<OptionValueChangedEventArgs> handler in ValueChanged.GetInvocationList()) handler(this, args);
             * }*/

            try
            {
                if (GameSetting != null)
                {
                    Object.FindObjectOfType <GameOptionsMenu>()?.Method_16();                     // RefreshChildren();
                }
            }
            catch
            {
            }
        }