How to write a server?

Hello there!

How do you write a Minecraft Server?

I’ve been looking around the internet on a tutorial or any explanation as to how you create a server, and I’ve found nothing. http://wiki.vg only shows how to write a client, not a server, but it does have a lot of stuff on the packet system in Minecraft. I have no idea how to implement that in Java or any other language, though.

Thanks,
Phase.

Edit:
Just to be clear, this is not running a server or making a plugin, this is coding a server like Bukkit or Sponge.

I would advise against making your own server from scratch. It requires a knowledge and understanding of java, and is a lot of work for one person on your own. Your best bet would be forking someone elses project on github and start from there.

1 Like

I’m fluent in Java, and have enough experience in making one.

it takes a lot of work to create a working server implementation like Bukkit or Sponge, considering the features they provide. there won’t be a tutorial on it out there, its not really a common enough thing to make for anyone to make a tutorial out of. its lots to take in to just make one server, part of it trying to figure out what the hell a chunk of deobfuscated code means, the other part trying to modify it and create all the necessary classes to make it at the very least stable.

you shouldn’t be trying to make your own server like Bukkit or Sponge, especially alone. it’ll take a lot of time if its a solo project, and considering Sponge is alive yours won’t be being used so much, and if its a solo project just for practice, then i can recommend going for something smaller instead of trying to recreate one for a game already in existance. if you don’t know how to create at least things like forgemods or a non-notchian client, or even how to use the system of packets that you said you don’t know how to implement, you shouldn’t be shooting so high.

I’ve done plenty of Forge mods, Bukkit plugins, and Clients before, but never a Server. I understand the packet system well, but I have found no explanation on how to implement it into Java, like waiting for a client to connect to the server. There has to be some documentation on it somewhere.

Yes it may be overthrown by Sponge or Spigot, but it’d be nice to learn it. I don’t want to go for something “smaller”, I’ve done plenty of big things in the past.

1 Like

You got to remember that you have to decompile the minecraft_server.jar and then deobfuscated the code from it, only then have can you start actually coding it. You can’t really do this alone no matter how good you are byself will take to long. so go to github, fork a server type of your choosing then change it around to your liking.

I’d rather not fork one, but write my own.

you can look at the sources of both Sponge and Bukkit (if its still available), they’re both open-source and you can try checking out what they do there and apply it to what you want to do. i don’t think you have much other options than that.

1 Like

Learn about sockets & possibly serialization if you don’t know about it. Those (at least, Sockets) are the key parts of servers.

You seem like you would know about that … It’s something I’ve tried but abandoned due to boredom and lack of planning. That being said, plan out a basic structure of what you want your server to look like before you write much code. Decide which design patterns you will follow (IIRC one of the design patterns in the server + client is a singleton MinecraftServer class: so you might consider that).

In terms of packets, what I’ve done in the past is create a factory for building packets, serializing, and deserializing them … >> To me, it’s better to have to write minimal code for defining new packets, since modification of them becomes a lot easier. Especially when you have a sizable server such as Minecraft with too many packets to keep track of at once.

I lost track of what I was writing, so if anything’s unclear let me know. Hope this guides you :wink:

1 Like

Sponge is not the place to ask this question (at least not a good one). You are better off asking Forge (luckily I’m here). Sponge is not an implementation of a minecraft server, rather an implementation of SpongeAPI layered atop Forge, which is atop of Minecraft server (so none of the people at Sponge actually know how to make a server except for SpaceManiac).

I’m a TridentSDK developer. Since Trident is clean reimplementation of the minecraft server, I would be the most qualified to answer at the moment, so don’t call me out for trolling because I am not.

Trident was developed around game order. At first, we figured that we needed a base the server runs on. This means connections, world loading, processes, and plugins. We started out with connections because it is the easiest. Netty was the backend, and we open a server bootstrap bound to IP 127.0.0.1 and port 25565. This allows clients to connect to the target at the external IP that is redirected through the connection made by the server bootstrap on localhost. This is started using 2 NioEventLoops.

Next, we did loggin/CLI options. As we have not had the chance to fully control the server yet, we held back on command implementation until world loading finishes. Currently, JOptSimple parses /help and the other runtime parameters.

Then, the server object is constructed. This loads in all of the processes, including server task executor, scheduling, connection listeners, and various API libraries.

The most important is protocol. We were highly focused on protocol because that’s what makes a server, a a server. The ability to communicate with clients (after connection pools are initialized). Basically, we had to replicate all of the packets on the protocol spec on wiki.vg, and correct the encoding a tiny bit. I won’t go into much detail, but we also handled client logins and encryption, along with the decoders/encoders along the netty IO connections. This was the hardest part to figure out in my personal opinion. There is a nice protocol order on the protocol FAQ on wiki.vg, that was a good reference for player joining, which came next.

All of the packets have an action method that can be overridden to perform an action when the packet is reviewed by the server. Once the join game packet is sent by the client, the server can now send the world chunks, block lighting, entities, metadata, and other players. Also note that you need to listen for the player’s update packet (which I am pretty sure has KeepAlice somewhere in there) which prompts you to update the world and send tick modifications to the client.

We haven’t gotten to world loading as we are discussing API design and when to make decisions, so I don’t know much about that. What we have started is RegionFile decoding, which allows us to parse world folders of default minecraft maps, which is extremely cool (but difficult). We have the NBT decoding done, and are looking on Minecraft wiki for information on the world file format (heh clean room :P).

I was in charge of multithreading. This is central to the server, as the existing minecraft server did not have very advanced multithreading capabilities. Task handling is based on ConcurrentTaskExecutor which is a thread pool that only loops and has a set pool size, in order to utilize the CPU consistently. We discussed the amount of threads to use to be around 16, because most processors cannot support more than 16 threads at a single time. More threads, and they would compete for CPU cycles, which may degrade performance instead of increasing it. I am currently looking at a separate performance concurrent queue because the current one takes 500ns to take the last item out. I’ve also reimplemented the scheduler (but tests show that it is 2x slower than the Bukkit scheduler… Yeesh) which originally had synchronized everywhere, even on atomic variables (hehe, not to blame the other developer for that - he had a cool tick class though).

All in all, just start with the connections/protocol.

3 Likes

I was just reading through Sponge’s source when I noticed the entire thing was a Forge mod… Thanks for the exact explanation I needed! Does Trident have a forum? I saw the post on the Bukkit Forums and it seemed pretty cool.

You could look into the Glowstone source code

1 Like

Yes, you can find out where on the post on Bukkit, but I can’t tell you the link exactly because that would be advertising. You could also definitely look at Glowstone, its been around for 2-3 years I believe.

How’s it advertising? Bukkit is dead.

Bukkit and Sponge aren’t servers as such. They just hook into / are built on the vanilla minecraft server.

2 Likes

I guess it would be advertising as it’s described by @DarkArcana in his rules post

Don’t advertise in any form on any Sponge system. This includes […] some piece of software you use for fun […]

But I’d hafta say, if you came across Sponge without already knowing about Bukkit, I’d be very surprised XD Bit off-topic, sorry!

I doubt it would count as advertising, though.
Correct me, if I’m wrong, @DarkArcana.

Discussing things you use for fun, not in an effort to draw people away from where you are but to that other thing you use, is not a form of advertisement. It’s discussion. But we still retain the right to call something advertisement or discussion if it seems like it to us one way or the other.

If I post the link, is it advertising…?

Depends on context. Think of two competing models, or services, such as xbox vs playstation. You’re on xbox forums. It’s probably fine to discuss the playstation and how it operates in relation to the xbox, but it’s probably not alright to post links and try to persuade people to buy one there. Think of going to the Forge forums and advertising Sponge as a new alternative and trying to persuade users to abandon one for the other. That kind of behavior is generally unacceptable. In the case of server lists, the whole point is to get people to join your server versus another. So you present competing models and the point is to advertise.

When you look at the context, you can determine whether someone is advertising or just discussing something. For example:

Person A: “What server do you play on and why?”
Person B: “I play on XYZ server at IP address with ‘this’ gamemode because reasons.”

vs

Person A: “You should use X server host because Y!”

So this makes a clear distinction between what is obviously discussion and what is advertisement. Sometimes context can be a little hard to discern so it falls on a moderator to decide.