Documentation - GrantShotwell/MCSharp GitHub Wiki
Creating a Datapack
Program Settings
In the settings.txt file, make sure you have defined a datapack folder and name.
// ex. C:\Users\___\AppData\Roaming\.minecraft\saves\___\datapacks
default_datapacks_folder = C:\Users\USER_FOLDER\AppData\Roaming\.minecraft\saves\WORLD_NAME\datapacks
// ex. example-datapack
default_datapack_name = DATAPACK_NAME
.mcsharp
File Location
All .mcsharp
files should be located in `datapacks\DATAPACK_NAME\data\DATAPACK_ID\scripts
Program.mcsharp
This is how your 'Program' class should look:
public static struct Program {
// Runs a single time on datapack load, before Main().
public static void Load() { }
// Runs every tick.
public static void Main() { }
}
Compiling Your MCSharp Code
Simply open the MCSharp.exe program and run 'compile' or 'c', and the datapack will be created from your .mcsharp
files.
Syntax
The syntax for MCSharp has been designed to be as close as possible to C# syntax.
Classes and Structs
Structs represent a set of variables, while classes represent an entity ('referenced' by a selector) that holds a set of variables.
Structs
Structs represent a set of variables.
public struct MyStruct { int x = 1; int y = 2; }
public static struct Program {
public static void Load() {
// This code:
MyStruct myStruct;
// Is equivalent to this code:
int x = 1;
int y = 2;
// (except for a difference in scope)
}
}
Definition
Structs can be defined the same way as C# types.
public struct MyStruct {
// ...
}
Classes
(Classes have not been implemented yet.)
Classes represent a selector that points to one or more entities that store objective-based primitives. If a selector grabs more than one entity for the object, the values will be added together.
Definition
(wip)
Important Distinctions
Because struct types are a "static" set of variables, they are more efficient to exist in-game than class types which are in-game entities.
Class objects can be created in any amount because the /summon
command exists, but the amount of struct objects are limited to how many you are willing to define individually in your code.
Passing a struct object to/from some method involves passing each field one-by-one. Passing a class object involves passing its object id, which will be used by a selector.
Members
Members (fields, properties, methods) can be defined and used in the same way as C# members.
Fields
Fields are the values that are saved within an object. Everything else within an object definition are only ways of manipulating its fields.
It is generally good practice to keep your fields private, and use properties to modify fields.
private int timeOfCreation = World.GetTime("gametime");
Properties
A property is code that changes the value of and/or gives the value of something the declaring object represents. You can define 'get' and 'set' methods for a property, but you don't need to define both. Like normal methods, the code within a property is in its own scope.
It is generally good practice to use properties that access fields, instead of accessing your fields directly.
public static PlayerCount {
get {
objective dummy = new Objective("dummy");
Scoreboard.SetScore("@a", 1);
return Scoreboard.GetScore("@a", dummy);
}
}
Properties are generally used to access private fields, and create representations of values that have no direct field to match.
private int radius;
public Radius {
get { return radius; }
set { radius = value; }
}
public Diameter {
get { return radius * 2; }
set { radius = value / 2; }
}
Methods
A method is code that does something; usually to the given arguments or the object itself. Methods can also return a value. The code within a method is within its own scope.
public int OnePlus(int n) {
int m = n + 1;
return m;
}
Built-in Library
Many types are built-in to run the various commands Minecraft has.