Skip to content

Creating the Datapack and the first Namespace

Shiny Bunny edited this page Jun 19, 2019 · 2 revisions

Creating the Datapack

When you create a console application in Visual Studio, something similar to this should be presented to you:

using System;
using System.otherstuff;

namespace ProjectName {
    public class App {
        public static void Main(string[] args) {
            Console.WriteLine("Hello, World");
            Console.ReadLine();
        }
    }
}

We're going to change most code here in a second, but let's first talk about the namespaces you'll need to use: They all start with MCFunctionAPI and have subcategories such as MCFunctionAPI.blocks, MCFunctionAPI.scoreboard etc...

For most stuff you'll use the basic MCFunctionAPI namespace and its contents.

Now that we got that out of the way, lets go back to the code:

First we need to include the MCFunctionAPI namespace as we said, so we'll add a using MCFunctionAPI; in the using list.

Then, you'll need to have your main class extend the Datapack class.

Your code should now look something like this:

using System;
using System.otherstuff;
using MCFunctionAPI;

namespace ProjectName {
    public class App : Datapack {
        public static void Main(string[] args) {
            Console.WriteLine("Hello, World");
            Console.ReadLine();
        }
    }
}

You may notice that you're getting an error, and that's because the Datapack class is an abstract class, and requires you to override two methods: GetDescription() and GetName().

using System;
using System.otherstuff;
using MCFunctionAPI;

namespace ProjectName {
    public class App : Datapack {
        public static void Main(string[] args) {
            Console.WriteLine("Hello, World");
            Console.ReadLine();
        }
    }

    public override string GetName() {
        return "example";
    }

    public override string GetDescription() {
        return "An Example Pack!";
    }
}

Next, in the Main method, we'll need to create an instance of our class.

App app = new App();

And now, we'll need to create our first Namespace.

Namespace main = app.CreateNamespace("main");

Let's also get rid of the WriteLine and ReadLine as we don't need those anymore..

And so the final code should look something like this:

using System;
using System.otherstuff;
using MCFunctionAPI;

namespace ProjectName {
    public class App : Datapack {
        public static void Main(string[] args) {
            App app = new App();
            Namespace main = app.CreateNamespace("main");
        }

        public override string GetName() {
            return "example";
        }

        public override string GetDescription() {
            return "An Example Pack!";
        }
    }
}

Clone this wiki locally