1. {QaD}Lurch

    {QaD}Lurch Naked Wanderer

    So, having actually *found* some time to putz around in the Assembly, I'm in the process of writing a plugin for taking on some admin tasks while myself or the other admins are either not around or too busy actually playing the game.

    Sheriff compiles just fine... However, I got this rather obtuse message from Oxide after the compile:
    Code (Text):
    [Oxide] 11:58 [Info] Sheriff was compiled successfully in 1680ms
    [Oxide] 11:58 [Error] Unable to load Sheriff. System.ArgumentException: An element with the same key already exists in the dictionary.
      at System.Collections.Generic.Dictionary`2[System.String,System.String].Add (System.String key, System.String value) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.Sheriff..ctor () [0x00000] in <filename unknown>:0
      at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (object,object[],System.Exception&)
      at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
    What dictionary?

    I suppose the next thing someone's going to tell me is that we can only run one mod at a time....
     
  2. Wulf

    Wulf Community Admin Community Admin Oxide Developer

    Did use the config API, datafile API, or Lang API?
     
  3. {QaD}Lurch

    {QaD}Lurch Naked Wanderer

    None of the above, just a simple class. I take it I missed an important item?
     
  4. Wulf

    Wulf Community Admin Community Admin Oxide Developer

    Could you upload you plugin here?
     
  5. {QaD}Lurch

    {QaD}Lurch Naked Wanderer

    Sure, but I'm going to 'nerf' some code as to not give away things I'm checking for - no sense in having a Sheriff around if everyone knows what he's keepin an eye on. ;-) Give me a few.
    --- Double Post Merged, Jun 7, 2016 ---
    Here's the stripped version of Sheriff:
    Code (C):
    using System.Collections.Generic;
    using System.Reflection;
    using System;
    using System.Linq;
    using System.Data;
    using UnityEngine;
    using Oxide.Core;

    namespace Oxide.Plugins
    {
        //
        // Some code adapted from OpenFunRus's Teleport plugin.
        //
        [Info("Sheriff", "{QaD}Lurch", 0.1)]
        [Description("System logger and game policy enforcement agent")]

        class NSheriff : SevenDaysPlugin
        {

            #region Vars

            // Nerfed - this code still compiles and still throws the Dictionary error. We must have missed a doc somewhere.

            int _currentDay;
            int _currentHour;
            int _currentMin;

            // Last player to log in info
            EntityPlayer _lastPlayer;

            string _lastPlayerName;
            string _lastPlayerID;

            // Basic colors
            Dictionary<string,string> myChatColorSchema = new Dictionary<string, string> {
                    {"White", "[FFFFFF]"},
                    {"Cyan",  "[00D9FF]"},
                    {"Red",   "[FF0D00]"},
                    {"Orange","[F78F19]"},
                    {"Green", "[08D40C]"},
                    {"Teal",  "[19F75F]"},
                    {"Blue",  "[0838D4]"},
                    {"Gold",  "[E3CA27]"},
                    {"Blue",  "[0838D4]"},
                    {"Purple","[C828D4]"}};


            /// <summary>
            /// Summary player object for Sheriff's tracking purposes
            /// </summary>
            class aPlayer
            {

                #region General properties
                // Nerfed
                #endregion

                /// <summary>
                /// Player as seen by Game engine
                /// </summary>
                /// <param name="_cInfo">ClientInfo object from game engine</param>
                public aPlayer(ClientInfo _cInfo)
                {
                    // Nerfed
                 
                }

                /// <summary>
                /// Call this method when we see the user and their record exists
                /// </summary>
                public void userLoggedIn()
                {
                    // Nerfed

                }

                //
                // nerf nerf nerfity nerf
                //

            }


            #endregion

            #region Oxide Player Hooks

            /// <summary>
            /// Event handler - Client connection
            /// </summary>
            /// <param name="_player"></param>
            void OnPlayerConnected(EntityPlayer _entPlayer)
            {
                ClientInfo _cInfo = ConsoleHelper.ParseParamIdOrName(_entPlayer.EntityName);
                EntityPlayer _player = GameManager.Instance.World.Players.dict[_cInfo.entityId];

                _lastPlayer = _player;
                _lastPlayerName = _cInfo.playerName;
             
                if (GameManager.Instance.adminTools.IsAdmin(_cInfo.playerId))
                {
                    // Nerfed
                }

                //Nerfed

            }

            void OnPlayerDisconnected(EntityPlayer _player)
            {
                // Nerfed
            }

            void OnPlayerChat(string message, string name)
            {
                if (!string.IsNullOrEmpty(message) && name != null && name != "" && name != "Server" && message.StartsWith("/"))
                {
                    ClientInfo _cInfo = ConsoleHelper.ParseParamIdOrName(name);
                    EntityPlayer _player = GameManager.Instance.World.Players.dict[_cInfo.entityId];
                    string myPos = (int)_player.position.x + "," + (int)_player.position.y + "," + (int)_player.position.z;
                    Vector3i _pos = Vector3i.Parse(myPos);
                    bool LandProtectionPlayer = GameManager.Instance.World.CanPlaceBlockAt(_pos, GameManager.Instance.GetPersistentPlayerList().GetPlayerData(_cInfo.playerId));
                    string _filter = "[ffffffff][/url][/b][/i][/u][/s][/sub][/sup][ff]";
                    if (message.EndsWith(_filter))
                    {
                        message = message.Remove(message.Length - _filter.Length);
                    }
                    //
                    // Note: Interesting to check if there's potential thread issues at play here
                    //
                    if(name == _cInfo.playerName)
                    {
                        if (message.StartsWith("/"))
                        {
                            DateTime nowtime = DateTime.Now;
                            message = message.Replace("/", "");
                            string msg = message.ToLower();
                            //
                            // GENERAL UTILS
                            //
                            if(msg == "help")
                            {
                                displayHelp();
                            }
                            if(msg == "7day" || msg == "day7" || msg == "7")
                            {
                                isSeventhDay();
                            }
                            //
                            //
                            //


                            // Nerfed - get contents from nerf.txt





                        }
                    }
                }
            }


            // Nerfed - nerfed_methods.cs
         

            #endregion

            #region Game Engine Hooks

            // TBD: Need to load Unity DLL first

            #endregion

            #region Logging

            //
            // Because there's no real logging in 7DTD
            //
            // TBD: Not sure of the hosted server will allow direct file access. This might be fun :)

            #endregion

            #region Helpers

            /// <summary>
            /// Simple help display TBD: Add help class
            /// </summary>
            void displayHelp()
            {

                var txt = "All commands start with '/'  /help (this)   /7day Tells if today is a 7th day ";

                PrintToChat(txt);

            }


            /// <summary>
            /// Displays information about current world day
            /// </summary>
            void isSeventhDay()
            {

                var _today = GetWorldTime();
                var _txt = "";

                switch(_today % 7)
                {
                    case 0:
                        _txt = "It is the 7th day, the night of the blood moon.";
                        break;

                    case 1:
                        _txt = "Today is day one of the seven day cycle.";
                        break;

                    case 2:
                        _txt = "Today is day two of the seven day cycle.";
                        break;
                    case 3:
                        _txt = "Today is day three. Four days now remain until the eviscerating hordes visit death upon these tortured lands...";
                        break;

                    case 4:
                        _txt = "It is now day four of the seven day cycle. Are you really this bad at math or just anxious?";
                        break;
                    case 5:
                        _txt = "Today is day five of the seven day cycle - beware the wandering hordes!";
                        break;
                    case 6:
                        _txt = "It is day six of the seven day cycle, one day remains, are you prepared to die?";
                        break;

                }

                PrintToChat(_txt);
            }

            /// <summary>
            /// Grabs current in-world time
            /// </summary>
            /// <returns>Integer of current day</returns>
            int GetWorldTime()
            {
                ulong world = GameManager.Instance.World.worldTime;

                _currentDay = GameUtils.WorldTimeToDays(world);
                _currentHour = GameUtils.WorldTimeToHours(world);
                _currentMin = GameUtils.WorldTimeToMinutes(world);

                return _currentDay;
            }

            /// <summary>
            /// Sends a single chat message to an individual player
            /// </summary>
            /// <param name="_cInfo">ClientInfo object of player</param>
            /// <param name="_color">HTML Hex value of color to display chat message with</param>
            /// <param name="_notice">Text of chat message</param>
            void SendPrivateChatMessage(ClientInfo _cInfo, string _color, string _notice)
            {
                _cInfo.SendPackage(new NetPackageGameMessage(EnumGameMessages.Chat, string.Format("{0} {1} [FFFFFF]", _color, _notice), "Server", false, "", false));
            }

            #endregion
        }
    }
     
    Last edited by a moderator: Jun 8, 2016
  6. {QaD}Lurch

    {QaD}Lurch Naked Wanderer

    Any ideas what's colliding?
     
  7. Wulf

    Wulf Community Admin Community Admin Oxide Developer

    It's erroring on your dictionary myChatColorSchema, not sure why.
     
  8. {QaD}Lurch

    {QaD}Lurch Naked Wanderer

    Hmm. One of the first things that I did was to change the name of that Dictionary and names of the individual members just to be sure there was no namespace collisions there. I'll remove it and try re-compiling and keep fingers crossed. If it's choking on a Dictionary<> there's a larger issue at hand inside Oxide.
     
  9. Wulf

    Wulf Community Admin Community Admin Oxide Developer

    The issue isn't with Oxide, that's a standard C# error within your plugin alone. I had another set of eyes look at the code (Mughisi), and it appears you are trying to define Blue twice, which is what is causing the error as keys need to be unique in a dictionary.
     
  10. Mughisi

    Mughisi Community Mod Oxide Developer

    You have the key blue defined twice as a key in your dictionary myChatColorSchema. Removing the duplicate value should solve your issue.
    Code (C):

            Dictionary<string, string> myChatColorSchema = new Dictionary<string, string> {
                    {"White", "[FFFFFF]"},
                    {"Cyan",  "[00D9FF]"},
                    {"Red",   "[FF0D00]"},
                    {"Orange","[F78F19]"},
                    {"Green", "[08D40C]"},
                    {"Teal",  "[19F75F]"},
                    {"Blue",  "[0838D4]"},  <---------------
                    {"Gold",  "[E3CA27]"},
                    {"Blue",  "[0838D4]"},  <---------------
                    {"Purple","[C828D4]"}};
     
    {QaD}Lurch and Wulf like this.
  11. {QaD}Lurch

    {QaD}Lurch Naked Wanderer

    Remind me to just go outside and shoot myself. :)

    Ugh. Bitten by cut & paste. I usually find these things - I guess not this time. Thanks for finding that - should have found it myself all things considered. The good news then is that I can go back to using a Dictionary like it was intended.

    I must confess part of my surprise here is besides not finding it myself, that the regular compiler didn't see anything wrong with it - it had to be caught higher up at the Oxide level. The error was caught in the Telnet window session I have open when I'm sitting at my workstation at home. The Compiler output otherwise looked clean, so I fell into an assumption trap there.

    Bad Lurch. Baaad.

    Four weeks of working with .Net 1.1 for you....