ItsMods

Full Version: Remove Threading/tidyup
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
could some kind soul please remove the threading from this and perhap a tidy up of the code if needed please.

As is, it works but due to the Threading it obviously crashes the server.

Code:
using System;
using System.Threading;
using Addon;

namespace RW
{
    public class plugin_test : CPlugin
    {
        public override void OnServerFrame()
        {
            
        }

        public override void OnServerLoad()
        {
            
        }

        public override ChatType OnSay(string Message, ServerClient Client)
        {
          
            return ChatType.ChatContinue;
        }

        public static void SafeSleep(int AmountSec)
        {
            int total = 0;
            while (total != AmountSec)
            {
                total++;
                Thread.Sleep(1000);
            }

        }
        public override void OnPlayerSpawned(ServerClient Client)
        {
            onInit(Client);
            onmsg(Client);
            
        }
        void onSpam(Object x)
        {
            while (true)
            {
                //SafeSleep(45);
                //ServerSay("^2RWMod: ^4Type !info to get info", true);
                //iPrintLn("^2Created by ^5JayDi", null);
                //SafeSleep(20);
                //ServerSay("^2RWMod: ^4Random Weapon Mod ^5by JayDi", true);
                //iPrintLn("^1INFECTED", null);
                //SafeSleep(10);
                //ServerSay("^2RWMod: ^3Invite your friends!", true);
                //iPrintLn("^2VERSION: 1.0", null);
            }
        }
        void onInit(ServerClient Client)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(weapz), Client);
        }
        void onmsg(ServerClient Client)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(msgz), Client);
        }
        void msgz(Object arg)
        {
            //ServerClient Client = (ServerClient)arg;
            //iPrintLnBold("^1Welcome to the ^2Random Weapon ^3Mod ^1by ^4JayDi", Client);
            //SafeSleep(1);
            //iPrintLnBold("^2ITSMODS.COM", Client);
        }

        void weapz(Object arg)
        {
            Thread.Sleep(200);
            ServerClient Client = (ServerClient)arg;
            // magic trick to avoid running in infected and gungame (by: Pieter/JoSchaap)
            bool younoworkokay = false;
            String gametype = GetDvar("g_gametype");
            if (gametype == "gun")
            {
                younoworkokay = true;
            }
            if (gametype == "infect")
            {
                younoworkokay = false;
                if (Client.Team != Teams.Allies)
                {
                    younoworkokay = true;
                }
            }
            if (younoworkokay != true)
            {
                                                
                Client.Other.ClearPerks(); // Clear perks ffs

                string[] AllSecondaries = { "iw5_fmg9_mp",
                                        "iw5_mp9_mp",
                                        "iw5_skorpion_mp", };
                                        //"iw5_g18_mp",
                                        //"iw5_mp412_mp",
                                        //"iw5_deserteagle_mp",
                                        //"iw5_usp45_mp",
                                        //"iw5_p99_mp",
                                        //"iw5_fnfiveseven_mp",
                                        //"rpg_mp",
                                        //"iw5_smaw_mp",
                                        //"stinger_mp",
                                        //"javelin_mp",
                                        //"xm25_mp",
                string[] AllPrimaries = { "iw5_g36c_mp",
                                    "iw5_m4_mp",
                                    "iw5_fad_mp",
                                    "iw5_acr_mp",
                                    "iw5_type95_mp",
                                    "iw5_mk14_mp",
                                    "iw5_scar_mp",
                                    "iw5_cm901_mp",
                                    "iw5_mp7_mp",
                                    "iw5_p90_mp",
                                    "iw5_ump45_mp",
                                    "iw5_barrett_mp_barrettscope",
                                    "iw5_rsass_mp_rsassscope",
                                    "iw5_dragunov_mp_dragunovscope",
                                    "iw5_msr_mp_msrscope",
                                    "iw5_as50_mp_as50scope", };
                                    //"iw5_ak47_mp",
                                    //"iw5_m16_mp",
                                    //"iw5_sa80_mp",
                                    //"iw5_pp90m1_mp",
                                    //"iw5_striker_mp",
                                    //"iw5_m60_mp",
                                    //"iw5_ksg_mp",
                                    //"iw5_mp5_mp",
                                    //"iw5_pecheneg_mp",
                                    //"iw5_mg36_mp",
                                    //"iw5_m9_mp",
                                    //"iw5_mk46_mp",
                                    //"iw5_spas12_mp",
                                    //"riotshield_mp",
                                    //"iw5_aa12_mp",
                                    //"iw5_1887_mp",
                                    //"iw5_usas12_mp",
                                    //"ac130_105mm_mp", };
                string[] AllPerks = {  "specialty_assists",
                                    "specialty_fastreload",
                                    "specialty_twoprimaries",
                                    "specialty_quickdraw",
                                    "specialty_blastshield",
                                    "specialty_autospot",
                                    "specialty_stalker",
                                    "specialty_bulletaccuracy", };
                                    //"specialty_scavenger",
                                    //"specialty_hardline",
                                    //"specialty_blindeye",
                                    //"specialty_coldblooded",
                                    //"specialty_paint",
                                    //"specialty_quieter",
                                    //"specialty_detectexplosive",
                string[] AllOffhand = { "flash_grenade_mp",
                                    "concussion_grenade_mp",
                                    "trophy_mp", };
                                    //"smoke_grenade_mp",
                                    //"specialty_scrambler",
                                    //"specialty_portable_radar",
                                    //"specialty_tacticalinsertion",
                                    //"emp_grenade_mp",
                string[] AllEquipment = { "c4_mp",
                                    "claymore_mp",
                                    "bouncingbetty_mp", };
                                    //"frag_grenade_mp",
                                    //"semtex_mp",
                                    //"specialty_portable_radar",
                                    //"throwingknife_mp",
                // ========== Attachments ================//
                string[] assAttachs = { "_acog",
                                    "_reflex",
                                    "_silencer",
                                    "_gl",
                                    "_fmj",
                                    "_eotech", };
                                    //"_hybrid",
                                    //"_heartbeat",
                                    //"_thermal",
                                    //"_shotgun",
                                    //"_xmags",
                string[] smgAttachs = { "_acogsmg",
                                    "_reflexsmg",
                                    "_rof",
                                    "_eotechsmg", };
                                    //"_hamrhybrid",
                                    //"_thermalsmg",

                string[] lmgAttachs = { "_reflexlmg",
                                    "_eotechlmg",
                                    "_grip",
                                    "_acog",};
                                    //"_thermal",

                string[] sniperAttachs = { "_zoomscope",
                                         "_acog", };
                                        //"_heartbeat",
                                       //"_thermal",
                                       //"_xmags",

                string[] shotgunAttachs = { "_grip",
                                        "_reflex",
                                        "_eotech", };
                                        //"_xmags",

                string[] pistolAttachs = { "_akimbo",
                                       "_tactical", };

                string[] mpAttachs = { "_akimbo",
                                   "_reflex",
                                   "_eotech", };
                                   //"_xmags",
                string[] camo = { "camo0",
                                "camo01",
                                "camo02",
                                "camo03",
                                "camo04",
                                "camo05",
                                "camo06",
                                "camo07",
                                "camo8",
                                "camo9",
                                "camo10",
                                "camo11", };

                // =============== Init ================= //
                bool assault = false;
                bool sniper = false;
                bool smg = false;
                bool lmg = false;
                bool shotgun = false;
                bool pistol = false;
                bool explosive = false;
                bool other = false;
                bool mpistol = false;
                bool special = false;
                bool riot = false;


                // ====== Get Random Weapon ===== //
                // Primary
                string primary = getRandom(AllPrimaries);
                int primarywep = GetWeapon(primary);
                // Secondary
                string secondary = getRandom(AllSecondaries);
                int secondarywep = GetWeapon(secondary);
                // Perk
                string perk = getRandom(AllPerks);
                int myperk = GetPerk(perk);
                // Offhand
                string offhand = getRandom(AllOffhand);
                int offhandwep = GetWeapon(offhand);
                // Equipment
                string equipment = getRandom(AllEquipment);
                int equipmentwep = GetWeapon(equipment);

                //int badwep = 0;
                //Client.Other.PrimaryWeapon = badwep;
                //Client.Other.CurrentWeapon = badwep;

                //int badwep2 = GetWeapon("iw5_barrett_mp_barrettscope");
                //Client.Other.PrimaryWeapon = badwep2;
                //Client.Other.CurrentWeapon = badwep2;

                // ======= OMG SHITLOAD OF CODES GOING NEXT ======== //

                Client.Other.SetPerk(myperk);
                Client.Other.OffhandWeapon = offhandwep;
                Client.Other.Equipment = equipmentwep;




                iPrintLnBold("^2You have new weapons, perks, & equipment. ", Client);
                if (primary == "iw5_mg36_mp")
                {
                    lmg = true;

                    int WepID = GetWeapon(primary + getRandom(lmgAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(lmgAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_sa80_mp")
                {
                    lmg = true;

                    int WepID = GetWeapon(primary + getRandom(lmgAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(lmgAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_mk46_mp")
                {
                    lmg = true;

                    int WepID = GetWeapon(primary + getRandom(lmgAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(lmgAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_m60_mp")
                {
                    lmg = true;

                    int WepID = GetWeapon(primary + getRandom(lmgAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(lmgAttachs) + "_" + getRandom(camo), Client);
                }
                //shotgunz
                else if (primary == "iw5_striker_mp")
                {
                    shotgun = true;

                    int WepID = GetWeapon(primary + getRandom(shotgunAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(shotgunAttachs) + "_" + getRandom(camo), Client);
                }
                

                //sniperz
                else if (primary == "iw5_msr_mp")
                {
                    sniper = true;

                    int WepID = GetWeapon(primary + getRandom(sniperAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(sniperAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_dragunov_mp")
                {

                    sniper = true;

                    int WepID = GetWeapon(primary + getRandom(sniperAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(sniperAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_rsass_mp")
                {
                    sniper = true;

                    int WepID = GetWeapon(primary + getRandom(sniperAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(sniperAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_barrett_mp")
                {
                    sniper = true;

                    int WepID = GetWeapon(primary + getRandom(sniperAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(sniperAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_l96a1_mp")
                {
                    sniper = true;

                    int WepID = GetWeapon(primary + getRandom(sniperAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(sniperAttachs) + "_" + getRandom(camo), Client);
                }
                // smgzzz
                else if (primary == "iw5_ump45_mp")
                {
                    smg = true;

                    int WepID = GetWeapon(primary + getRandom(smgAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(smgAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_pp90m1_mp")
                {
                    smg = true;

                    int WepID = GetWeapon(primary + getRandom(smgAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(smgAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_p90_mp")
                {
                    smg = true;

                    int WepID = GetWeapon(primary + getRandom(smgAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(smgAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_m9_mp")
                {
                    smg = true;

                    int WepID = GetWeapon(primary + getRandom(smgAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(smgAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_mp7_mp")
                {
                    smg = true;

                    int WepID = GetWeapon(primary + getRandom(smgAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(smgAttachs) + "_" + getRandom(camo), Client);
                }

                else if (primary == "iw5_mp5_mp")
                {
                    smg = true;

                    int WepID = GetWeapon(primary + getRandom(smgAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(smgAttachs) + "_" + getRandom(camo), Client);
                }
                // assault
                else if (primary == "iw5_cm901_mp")
                {
                    assault = true;

                    int WepID = GetWeapon(primary + getRandom(assAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(assAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_g36c_mp")
                {
                    assault = true;

                    int WepID = GetWeapon(primary + getRandom(assAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(assAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_scar_mp")
                {
                    assault = true;

                    int WepID = GetWeapon(primary + getRandom(assAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(assAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_mk14_mp")
                {
                    assault = true;

                    int WepID = GetWeapon(primary + getRandom(assAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(assAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_type95_mp")
                {
                    assault = true;

                    int WepID = GetWeapon(primary + getRandom(assAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(assAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_fad_mp")
                {
                    assault = true;

                    int WepID = GetWeapon(primary + getRandom(assAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(assAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_m4_mp")
                {
                    assault = true;

                    int WepID = GetWeapon(primary + getRandom(assAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(assAttachs) + "_" + getRandom(camo), Client);
                }
                else if (primary == "iw5_ak47_mp")
                {
                    assault = true;

                    int WepID = GetWeapon(primary + getRandom(assAttachs) + "_" + getRandom(camo));
                    Client.Other.PrimaryWeapon = WepID;
                    Client.Other.CurrentWeapon = WepID;
                    iPrintLn("^1" + primary + getRandom(assAttachs) + "_" + getRandom(camo), Client);
                }
                // otherzz
                else if (primary == "riotshield_mp")
                {
                    riot = true;

                    Client.Other.PrimaryWeapon = primarywep;
                    Client.Other.CurrentWeapon = primarywep;
                }

                // ======== Auto-Pistols =============//


                if (secondary == "iw5_usp45_mp")
                {
                    pistol = true;
                    int WepID = GetWeapon(secondary + getRandom(pistolAttachs));
                    Client.Other.SecondaryWeapon = WepID;

                }
                if (secondary == "iw5_mp412_mp")
                {
                    pistol = true;
                    int WepID = GetWeapon(secondary + getRandom(pistolAttachs));
                    Client.Other.SecondaryWeapon = WepID;
                }
                if (secondary == "iw5_p99_mp")
                {
                    pistol = true;
                    int WepID = GetWeapon(secondary + getRandom(pistolAttachs));
                    Client.Other.SecondaryWeapon = WepID;
                }
                  // Automachine pistols
                if (secondary == "iw5_fmg9_mp")
                {
                    mpistol = true;
                    int WepID = GetWeapon(secondary + getRandom(mpAttachs));
                    Client.Other.SecondaryWeapon = WepID;
                }
                if (secondary == "iw5_mp9_mp")
                {
                    mpistol = true;
                    int WepID = GetWeapon(secondary + getRandom(mpAttachs));
                    Client.Other.SecondaryWeapon = WepID;
                }
                if (secondary == "iw5_skorpion_mp")
                {
                    mpistol = true;
                    int WepID = GetWeapon(secondary + getRandom(mpAttachs));
                    Client.Other.SecondaryWeapon = WepID;
                }
                              
                Ammoz(Client);
            }
        }

        // ============== Other =================== //
        public static void Ammoz(ServerClient client)
        {
            client.Ammo.PrimaryAmmoClip = 298;
            client.Ammo.SecondaryAmmoClip = 240;
        }

        string getRandom(string[] input)
        {
            Random r = new Random();
            return input[randomFix().Next(0, input.Length)];
        }
        public static Random randomFix()
        {
            Thread.Sleep(10);
            return new Random();
        }

        
        
    }
}
Use search, already was asked..
Use onAddon(server)Frame
Why would threading crash the server?
(09-16-2012, 10:14)SuperNovaAO Wrote: [ -> ]Why would threading crash the server?

We don't know lol
My friend said, his server was crashing like every hour, but he deleted all threadings from plugin now its holds for >10 hours
(09-16-2012, 10:14)SuperNovaAO Wrote: [ -> ]Why would threading crash the server?

Thread allocates memory. MW3 then has some weird problem allocating memory and crashes. (Trying to commit memory in a region which has already been commited, then failing with the error 487)