Ster
Civilian Zombie
Civilian Zombie
Posts: 61
Joined: Mon Feb 29, 2016 11:53 pm
x 18

(Tutorial) Events/Admin-Death-Announcer

Tue Jul 12, 2016 7:00 pm

This tutorial is for users who already know how to reference rocket/build a simple plugin.
I've commented most of the code, so this should be pretty self-explanatory, but if you experience any trouble, feel free to comment below.

Heart this post if this helped you!
-----------------------------------------------------------------------------------------------------------------
For Syntax Highlighting, view:
Testing.cs - http://pastebin.com/xSN0JbFB
Configuration.cs - http://pastebin.com/tn4aFVrw
-----------------------------------------------------------------------------------------------------------------

Testing.cs

Code: Select all

using Rocket.Core.Logging;
using Rocket.Core.Plugins;
using Rocket.Core.Steam;
using Rocket.Unturned;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Effects;
using Rocket.Unturned.Enumerations;
using Rocket.Unturned.Events;
using Rocket.Unturned.Commands;
using Rocket.Unturned.Items;
using Rocket.Unturned.Player;
using SDG.Unturned;
using UnityEngine;
using Steamworks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Ster.Test
{
    class Testing : RocketPlugin<Configuration> //Here we are creating our "main" class, which will be deriving from "RocketPlugin".
    {
        public static Testing Instance; //we create our "Testing" Instance here.
        protected override void Load() //this is our Load method, it will be called when the plugin is loaded.
        {
            Instance = this;
            if (Testing.Instance.Configuration.Instance.Enabled) //we are checking the value of the variable "Enabled" in our configuration.
            {
                UnturnedPlayerEvents.OnPlayerDeath += PlayerDied; //here we are subscribing the OnPlayerDeath event to our PlayerDied method.
                Logger.Log("[Admin-Death-Announcer] Log:  Plugin Loaded.");
            }
            else //this code is run when Enabled = false
            {
                Logger.Log("[Admin-Death-Announcer] Log:  Plugin Unloading because Enabled = 'false'.");
                Unload(); //the method to unload our plugin. Runs all code in the unload method.
            }
        }

        private void PlayerDied(UnturnedPlayer player, EDeathCause cause, ELimb limb, CSteamID murderer) //creating our PlayerDied method with the proper parameters from the event.
        {
            if (player.IsAdmin) //checks if the player who died is an admin.
            {
                foreach (SteamPlayer plr in Provider.Players) //we are grabbing all users online and executing the below code for each of them.
                {
                    UnturnedPlayer looped = UnturnedPlayer.FromSteamPlayer(plr); //we are converting the SteamPlayer into an UnturnedPlayer.
                    UnturnedChat.Say(looped, "The Admin '" + player.CharacterName + "' has died!", Color.cyan); //our message that will be sent to the looped player.
                }
                UnturnedChat.Say(murderer, "Oh dear. You killed an Admin!", Color.red); //this message is only sent to the player who killed the admin.
            }
        }

        //Advertising - FearRising.com
        //Note - I am referencing a lot of libs here so I don't need to reference them later. Feel free to clean the ones that are not in use if you release your plugin.

        protected override void Unload() //This is our unload method. All code inside will be executed when the server stops, the plugin is unloaded, or if we call this method (like earlier)
        {
            UnturnedPlayerEvents.OnPlayerDeath -= PlayerDied; //unsubscribing the OnPlayerDeath event from our PlayerDied method.
            Logger.Log("[Admin-Death-Announcer] Log:  Plugin Unloaded.");
        }
    }
}




Configuration.cs

Code: Select all

using Rocket.API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ster.Test
{
    public class Configuration : IRocketPluginConfiguration
    {
        public bool Enabled; //if false this plugin will not load.

        public void LoadDefaults()
        {
            Enabled = true;
        }
    }
}
1 x

a4834833
Civilian Zombie
Civilian Zombie
Posts: 77
Joined: Sat Nov 07, 2015 9:24 am
x 7

Re: (Tutorial) Events/Admin-Death-Announcer

Fri Jul 15, 2016 5:07 am

Nice Tutorial ,
Can you help Me With My Project And Give Me Example with add player to permission group via code?
0 x

birinorama
Mega Zombie
Mega Zombie
Posts: 217
Joined: Sat Nov 21, 2015 12:11 am
x 16

Re: (Tutorial) Events/Admin-Death-Announcer

Fri Jul 15, 2016 4:21 pm

a4834833 wrote:Nice Tutorial ,
Can you help Me With My Project And Give Me Example with add player to permission group via code?


CommandWindow.ConsoleInput.onInputText.Invoke(string.Concat("p add ", unturnedPlayer.Id, " groupname"));

this would be easiest, "brute/hardcoded" you could also make this so you can modify in config file :P
0 x

a4834833
Civilian Zombie
Civilian Zombie
Posts: 77
Joined: Sat Nov 07, 2015 9:24 am
x 7

Re: (Tutorial) Events/Admin-Death-Announcer

Sat Jul 16, 2016 11:20 am

birinorama wrote:
a4834833 wrote:Nice Tutorial ,
Can you help Me With My Project And Give Me Example with add player to permission group via code?


CommandWindow.ConsoleInput.onInputText.Invoke(string.Concat("p add ", unturnedPlayer.Id, " groupname"));

this would be easiest, "brute/hardcoded" you could also make this so you can modify in config file :P


CommandWindow.Consoleinput.OnInputText.Invoke (string.concat ("[add " , unturnedPlayer.Id , "groupname")); Wont work
0 x

birinorama
Mega Zombie
Mega Zombie
Posts: 217
Joined: Sat Nov 21, 2015 12:11 am
x 16

Re: (Tutorial) Events/Admin-Death-Announcer

Sat Jul 16, 2016 11:46 am

a4834833 wrote:
birinorama wrote:
a4834833 wrote:Nice Tutorial ,
Can you help Me With My Project And Give Me Example with add player to permission group via code?


CommandWindow.ConsoleInput.onInputText.Invoke(string.Concat("p add ", unturnedPlayer.Id, " groupname"));

this would be easiest, "brute/hardcoded" you could also make this so you can modify in config file :P


CommandWindow.Consoleinput.OnInputText.Invoke (string.concat ("[add " , unturnedPlayer.Id , "groupname")); Wont work


The line you copied is a bit wrong because it's "p add ".... and not "[add "....
if that is just mistake here then a bit more context on what you want to achieve and how would be needed because there are few cases I see my line actually not to be useful :P
0 x

Ster
Civilian Zombie
Civilian Zombie
Posts: 61
Joined: Mon Feb 29, 2016 11:53 pm
x 18

Re: (Tutorial) Events/Admin-Death-Announcer

Sat Jul 16, 2016 4:25 pm

a4834833 wrote:Nice Tutorial ,
Can you help Me With My Project And Give Me Example with add player to permission group via code?


Thank you!

I don't mind helping you with your project, just PM me the time you'll be available (include timezone in PM). I won't be back home until late tonight, so i'll explain how to "add player to a permission group" when I get home, or tomorrow.
0 x

Return to “Development Tutorials & Guides”

Who is online

Users browsing this forum: No registered users and 1 guest

Advertisement