Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

Join us at AWS re:Invent 2024! Learn how to use MongoDB for AI use cases.
MongoDB Developer
MongoDB
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
MongoDBchevron-right

Getting started with Minecraft and MongoDB

Aasawari Sahasrabuddhe4 min read • Published Jul 15, 2024 • Updated Jul 15, 2024
MongoDBJava
SNIPPET
Facebook Icontwitter iconlinkedin icon
Rate this quickstart
star-empty
star-empty
star-empty
star-empty
star-empty
Minecraft, a renowned sandbox game, empowers players to create custom modifications (mods) that enhance the gameplay experience. Originally developed in Java, Minecraft has expanded to other programming languages, with the Java Edition remaining the most popular among users. Given the growing player base, MongoDB emerges as an ideal database solution to manage and sync player details seamlessly with the Minecraft application.
In this tutorial, we will explore how to connect your Minecraft Java application to MongoDB. By the end, you will have a robust setup that integrates MongoDB as your database for managing player data.
The complete code snippet and sample project discussed in this tutorial are available in the GitHub repository.

Prerequisites

  1. Create a free Atlas cluster: Sign up and create your free Atlas cluster to obtain your MongoDB connection string.
  2. Download the Minecraft server: Use PaperMC to download the Minecraft server and enhance Minecraft’s ecosystem.
  3. Download the latest JDK: We will be using Java Version 22 for this tutorial.
  4. Update your MongoDB Java Driver version: For this tutorial, we will be using version 5.1.0.
Let's get started on this exciting integration journey!

Starting the Minecraft Server

Once the jar file for the PaperMC is downloaded, copy the jar into a new folder and start the server using the below command:
1java -jar paper-1.20.6-147.jar
This will download all the dependencies and create multiple folders. Change the eula=true in the eula.txt file.
Once this is set, you are all set to start the server using the above command.
In the next step, we will try to understand how you can make the Java application connect with MongoDB.

Connecting the Java application with MongoDB

Add your dependencies for PaperMC and spigot in your pom.xml.
Below is an example to download the dependencies.
1 <dependencies>
2 <dependency>
3 <groupId>org.spigotmc</groupId>
4 <artifactId>spigot-api</artifactId>
5 <version>1.20.4-R0.1-SNAPSHOT</version>
6 <scope>provided</scope>
7 </dependency>
8 <dependency>
9 <groupId>org.mongodb</groupId>
10 <artifactId>mongodb-driver-sync</artifactId>
11 <version>5.1.0</version>
12 </dependency>
13 <dependency>
14 <groupId>io.papermc.paper</groupId>
15 <artifactId>paper-api</artifactId>
16 <version>1.20.1-R0.1-SNAPSHOT</version>
17 <scope>provided</scope>
18 </dependency>
19 </dependencies>
Once the dependencies are loaded, we can start by creating the plugin and the properties file.
In Minecraft plugin development, particularly with the Bukkit or Spigot APIs (often used with the PaperMC server), the onEnable and onDisable functions are essential methods in a plugin's lifecycle. These methods are part of the MongoDBPlugin.java class that your plugin class typically extends.
The onEnable() method is called when the plugin is first enabled. This is where you put any initialization code for your plugin. It is typically used to register commands, set up configuration files, initialize variables and other resources, etc.
For this tutorial, we will set up the MongoDB connection in this function.
1 @Override
2 public void onEnable() {
3 getLogger().info("MongoDBTest plugin has been enabled");
4 Properties properties = new Properties();
5 String uri ;
6 try (InputStream input = getClass().getClassLoader().getResourceAsStream("application.properties")) {
7 if (input == null) {
8 getLogger().severe("Sorry, unable to find application.properties");
9 return;
10 }
11 properties.load(input);
12 uri = properties.getProperty("mongo.uri");
13 MongoClient mongoClient = MongoClients.create(uri);
14 database = mongoClient.getDatabase("test");
15 getLogger().info("Connected to the database successfully");
16 // Example: Listing collections in the database
17 for (String name : database.listCollectionNames()) {
18 getLogger().info("Collection: " + name);
19 }
20 } catch (Exception ex) {
21 ex.printStackTrace();
22 }
23 }
The onDisable() method is called when the plugin is disabled. This is where you put any cleanup code to ensure that your plugin shuts down gracefully. It is typically used to save data to files or databases, unregister event listeners, and close database connections.
1@Override
2 public void onDisable() {
3 getLogger().info("MongoDBTest plugin has been disabled");
4 }
5 public MongoDatabase getDatabase() {
6 return database;
7 }
Get your connection string from the Atlas cluster and copy it into the application.properties file as below.
1mongo.uri=<Place Your connection URI here>
Finally, the plugin.yml file is another crucial component of the Minecraft client application. This file contains essential metadata and configuration information about your plugin, allowing the server to properly load, enable, and manage your plugin.
Finally, when all the code is in place, run the below command to create the jar file.
1mvn clean package
Once the jar is created, copy this jar file into the plugins folder for the PaperMC server file. Below is how the folder should look:
1aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % pwd
2/Users/aasawari.sahasrabuddhe/minecraftServer/plugins
3aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % ls
4MinecraftMongoDB-1.0-SNAPSHOT.jar bStats
5aasawari.sahasrabuddhe@M-C02DV42LML85 plugins %

Running the Java application with the Minecraft Server

To run the Java application with the Minecraft server, copy the below command and run the jar file. This command will enable the server and make the connection to the database.
The server will start, and the below image gives you an overview that shows that the connection with MongoDB is successful.
As a result of the above command, you should see the following message:
1[MongoDBConnection] Connected to the database successfully
Here it is shown in the terminal screenshot:
Screenshot demonstrating that connection to MongoDB is successful
Fig: Screenshot demonstrating that connection to MongoDB is successful

Further steps

Now that you've successfully established a connection to your MongoDB database from your Paper plugin, you can start extending the functionality of your plugin. Try interacting with the MongoDB database and managing the player information. You can perform the CRUD operations by managing the players who enter the game, modify the status once they leave the game, and so on.

Conclusion

Integrating MongoDB Atlas with a Minecraft mod opens up a world of possibilities for enhancing your game's functionality. By following the steps outlined in this article, you have learned how to set up a MongoDB Atlas cluster, connect it to your Minecraft mod, and perform basic database operations. With the steps mentioned in the above section, you can extend your application and start managing the players who enter the game through your application.
Remember, this is just the beginning. Experiment with different features and explore the full capabilities of MongoDB Atlas to take your Minecraft modding to the next level. If you have any further questions, please feel free to share your reviews and feedback on our MongoDB Community forum. You can also visit the MongoDB Developer Center for more interesting articles.
Top Comments in Forums
There are no comments on this article yet.
Start the Conversation

Facebook Icontwitter iconlinkedin icon
Rate this quickstart
star-empty
star-empty
star-empty
star-empty
star-empty
Related
News & Announcements

MongoDB Academia - Introduction to Modern Databases


Sep 23, 2022 | 4 min read
Tutorial

Revolutionizing AI Interaction: Integrating Mistral AI and MongoDB for a Custom LLM GenAI Application


Feb 13, 2024 | 11 min read
Article

The Six Principles for Building Robust Yet Flexible Shared Data Applications


Sep 23, 2022 | 3 min read
Article

Wordle Solving Using MongoDB Query API Operators


Feb 01, 2023 | 4 min read
Table of Contents