-
Notifications
You must be signed in to change notification settings - Fork 65
Setting up a server
Craft.Net.Server makes it relatively easy to set up a Minecraft server. There are a number of things you need:
- At least one level
- An endpoint
- Sever settings
The last one is optional - the default settings will be used if you do not specify your own. Here's a common server setup:
var minecraftServer = new MinecraftServer(new IPEndPoint(IPAddress.Any, 25565));
var generator = new FlatlandGenerator();
// Creates a level in the "world" directory, using the flatland generator
// You may omit "world" to create a level in memory.
minecraftServer.AddLevel(new Level(generator, "world"));
minecraftServer.Start();
This would start a server on port 25565, with a flatland world. The level will be saved periodically; the exact period can be customized with Level.SaveFrequency
. There are also several events that you can take advantage of to customize the server.
-
MinecraftServer.PlayerLoggedIn
fires when a player successfully logs in. SetPlayerLogInEventArgs.Handled
to true to suppress the default "Player has joined the game" chat message. -
MinecraftServer.PlayerLoggedOut
is similar. SetPlayerLogInEventArgs.Handled
to true to suppress the default "Player has left the game" chat message. -
MinecraftServer.ChatMessage
will fire when players chat on the server. SetChatMessageEventArgs.Handled
to prevent the default " message" chat from being sent to connected players. -
MinecraftServer.PacketSent
fires immediately after a packet is sent by the server, and includes the packet details. -
MinecraftServer.PacketRecieved
fires immediately after a packet is received by the server, before it is processed by the server packet handlers. -
MinecraftServer.PlayerDeath
fires when a player dies. SetPlayerDeathEventArgs.Handled
to true to suppress the default death message.
You can add your own packet handlers to replace the default ones. Use MinecraftServer.RegisterPacketHandler
to register a method whose signature is (MinecraftClient client, MinecraftServer server, IPacket packet)
.
Make a class that derives from Craft.Net.Server.PluginChannel
and use MinecraftServer.RegisterPluginChannel
to register your channel. Some plugin channels are included for you: