Modding:Commands: Difference between revisions

From Vintage Story Wiki
m (Smile moved page Commands to Mod:Commands: Commands should be the commands currently in the game not how to add commands to the game.)
No edit summary
(2 intermediate revisions by 2 users not shown)
Line 5: Line 5:
The idea is to add a command which makes it easier for your mates to locate you. Therefore we will spawn some particles alongside a playing sound, so others can see and hear where you are at the moment.
The idea is to add a command which makes it easier for your mates to locate you. Therefore we will spawn some particles alongside a playing sound, so others can see and hear where you are at the moment.


You can download the required assets [http://wiki.vintagestory.at/images/1/16/Here_Assets.zip here]. Just extract them in your mods directory and you are good to go.
You can download the required assets [https://wiki.vintagestory.at/images/1/16/Here_Assets.zip here]. Just extract them in your mods directory and you are good to go.


== Implementation ==
== Implementation ==
Line 50: Line 50:
                 (IServerPlayer player, int groupId, CmdArgs args) =>
                 (IServerPlayer player, int groupId, CmdArgs args) =>
                     {
                     {
                         IEntityPlayer byEntity = player.Entity;
                         EntityPlayer byEntity = player.Entity;
                         byEntity.World.PlaySoundAt(sound, byEntity); // Play sound
                         byEntity.World.PlaySoundAt(sound, byEntity); // Play sound


                         Vec3d pos = byEntity.Pos.XYZ.Add(0, byEntity.EyeHeight(), 0); // Setting up position to spawn particles
                         Vec3d pos = byEntity.Pos.XYZ.Add(0, byEntity.EyeHeight, 0); // Setting up position to spawn particles
                         Random rand = new Random();
                         Random rand = new Random();
                         for (int i = 0; i < 100; i++) // Spawn 100 particles
                         for (int i = 0; i < 100; i++) // Spawn 100 particles
Line 59: Line 59:
                             Vec3d realPos = pos.AddCopy(-0.1 + rand.NextDouble() * 0.2, 0, -0.1 + rand.NextDouble() * 0.2);
                             Vec3d realPos = pos.AddCopy(-0.1 + rand.NextDouble() * 0.2, 0, -0.1 + rand.NextDouble() * 0.2);
                             Vec3f velocity = new Vec3f(-0.2F + (float) rand.NextDouble() * 0.4F, 0.4F + (float) rand.NextDouble() * 2F, -0.2F + (float) rand.NextDouble() * 0.4F);
                             Vec3f velocity = new Vec3f(-0.2F + (float) rand.NextDouble() * 0.4F, 0.4F + (float) rand.NextDouble() * 2F, -0.2F + (float) rand.NextDouble() * 0.4F);
                             byEntity.World.SpawnParticles(1, ColorUtil.ColorFromArgb(255, rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255)),
                             byEntity.World.SpawnParticles(1, ColorUtil.ColorFromRgba(255, rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255)),
                                 realPos, realPos,
                                 realPos, realPos,
                                 velocity, velocity, (float) rand.NextDouble()*1 + 1, 0.01F,
                                 velocity, velocity, (float) rand.NextDouble()*1 + 1, 0.01F,
Line 80: Line 80:
Feel free to try it out yourself:
Feel free to try it out yourself:


[http://wiki.vintagestory.at/images/4/47/Here_v1.0.zip Here_v1.0.zip]
[https://wiki.vintagestory.at/images/4/47/Here_v1.0.zip Here_v1.0.zip]

Revision as of 22:29, 8 May 2020

Ever wanted to know how to add a command to the game? If yes, this is the right place to get an answer.

Preparations

The idea is to add a command which makes it easier for your mates to locate you. Therefore we will spawn some particles alongside a playing sound, so others can see and hear where you are at the moment.

You can download the required assets here. Just extract them in your mods directory and you are good to go.

Implementation

First of all you need to add another mod to your workspace. I will use the modid here, so I will type in add here in modtools to create a new mod. Additionally I will create Command.cs in the src directory.

As always we need to create a class extending ModSystem:

public class CommandMod : ModSystem
{

}

Commands are processed by the server, so our mod only needs to load on the server's side:

        public override bool ShouldLoad(EnumAppSide side)
        {
            return side == EnumAppSide.Server;
        }

Now we need to register the command itself:

        public override void StartServerSide(ICoreServerAPI api)
        {
            base.StartServerSide(api);
            api.RegisterCommand("here", "spawns particles around the player", "",
                (IServerPlayer player, int groupId, CmdArgs args) =>
                    {

                    }, Privilege.chat);
        }

This command can be used by any player who is allowed to send a message (by default everyone). When a player types in /here, the command will be executed. Now we the only thing missing is the actual code to spawn particles and to play the sound.

        public override void StartServerSide(ICoreServerAPI api)
        {
            base.StartServerSide(api);
            AssetLocation sound = new AssetLocation("here", "sounds/partyhorn"); // Create sound location
            api.RegisterCommand("here", "spawns particles around the player", "",
                (IServerPlayer player, int groupId, CmdArgs args) =>
                    {
                        EntityPlayer byEntity = player.Entity;
                        byEntity.World.PlaySoundAt(sound, byEntity); // Play sound

                        Vec3d pos = byEntity.Pos.XYZ.Add(0, byEntity.EyeHeight, 0); // Setting up position to spawn particles
                        Random rand = new Random();
                        for (int i = 0; i < 100; i++) // Spawn 100 particles
                        {
                            Vec3d realPos = pos.AddCopy(-0.1 + rand.NextDouble() * 0.2, 0, -0.1 + rand.NextDouble() * 0.2);
                            Vec3f velocity = new Vec3f(-0.2F + (float) rand.NextDouble() * 0.4F, 0.4F + (float) rand.NextDouble() * 2F, -0.2F + (float) rand.NextDouble() * 0.4F);
                            byEntity.World.SpawnParticles(1, ColorUtil.ColorFromRgba(255, rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255)),
                                realPos, realPos,
                                velocity, velocity, (float) rand.NextDouble()*1 + 1, 0.01F,
                                1, EnumParticleModel.Cube);
                        }
                    }, Privilege.chat);
        }

If you want to learn more about how to use particles you can check out this tutorial.

Testing

Finally, we are ready to run our first test:

Download

Feel free to try it out yourself:

Here_v1.0.zip