Tools
Tools: Getting Started With Minecraft and MongoDB
2026-01-16
0 views
admin
Prerequisites ## Starting the Minecraft Server ## Connecting the Java application with MongoDB ## Running the Java application with the Minecraft Server ## Further steps ## Conclusion This tutorial was written by Aasawari Sahasrabuddhe. 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. Let's get started on this exciting integration journey! Once the jar file for the PaperMC is downloaded, copy the jar into a new folder and start the server using the below command: 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. Add your dependencies for PaperMC and spigot in your pom.xml. Below is an example to download the 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. 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. Get your connection string from the Atlas cluster and copy it into the application.properties file as below. 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. 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: 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: 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. 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. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK:
java -jar paper-1.20.6-147.jar Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
java -jar paper-1.20.6-147.jar CODE_BLOCK:
java -jar paper-1.20.6-147.jar CODE_BLOCK:
<dependencies> <dependency> <groupId>org.spigotmc</groupId> <artifactId>spigot-api</artifactId> <version>1.20.4-R0.1-SNAPSHOT</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>5.1.0</version> </dependency> <dependency> <groupId>io.papermc.paper</groupId> <artifactId>paper-api</artifactId> <version>1.20.1-R0.1-SNAPSHOT</version> <scope>provided</scope> </dependency> </dependencies> Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
<dependencies> <dependency> <groupId>org.spigotmc</groupId> <artifactId>spigot-api</artifactId> <version>1.20.4-R0.1-SNAPSHOT</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>5.1.0</version> </dependency> <dependency> <groupId>io.papermc.paper</groupId> <artifactId>paper-api</artifactId> <version>1.20.1-R0.1-SNAPSHOT</version> <scope>provided</scope> </dependency> </dependencies> CODE_BLOCK:
<dependencies> <dependency> <groupId>org.spigotmc</groupId> <artifactId>spigot-api</artifactId> <version>1.20.4-R0.1-SNAPSHOT</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>5.1.0</version> </dependency> <dependency> <groupId>io.papermc.paper</groupId> <artifactId>paper-api</artifactId> <version>1.20.1-R0.1-SNAPSHOT</version> <scope>provided</scope> </dependency> </dependencies> CODE_BLOCK:
@Override public void onEnable() { getLogger().info("MongoDBTest plugin has been enabled"); Properties properties = new Properties(); String uri ; try (InputStream input = getClass().getClassLoader().getResourceAsStream("application.properties")) { if (input == null) { getLogger().severe("Sorry, unable to find application.properties"); return; } properties.load(input); uri = properties.getProperty("mongo.uri"); MongoClient mongoClient = MongoClients.create(uri); database = mongoClient.getDatabase("test"); getLogger().info("Connected to the database successfully"); // Example: Listing collections in the database for (String name : database.listCollectionNames()) { getLogger().info("Collection: " + name); } } catch (Exception ex) { ex.printStackTrace(); } } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
@Override public void onEnable() { getLogger().info("MongoDBTest plugin has been enabled"); Properties properties = new Properties(); String uri ; try (InputStream input = getClass().getClassLoader().getResourceAsStream("application.properties")) { if (input == null) { getLogger().severe("Sorry, unable to find application.properties"); return; } properties.load(input); uri = properties.getProperty("mongo.uri"); MongoClient mongoClient = MongoClients.create(uri); database = mongoClient.getDatabase("test"); getLogger().info("Connected to the database successfully"); // Example: Listing collections in the database for (String name : database.listCollectionNames()) { getLogger().info("Collection: " + name); } } catch (Exception ex) { ex.printStackTrace(); } } CODE_BLOCK:
@Override public void onEnable() { getLogger().info("MongoDBTest plugin has been enabled"); Properties properties = new Properties(); String uri ; try (InputStream input = getClass().getClassLoader().getResourceAsStream("application.properties")) { if (input == null) { getLogger().severe("Sorry, unable to find application.properties"); return; } properties.load(input); uri = properties.getProperty("mongo.uri"); MongoClient mongoClient = MongoClients.create(uri); database = mongoClient.getDatabase("test"); getLogger().info("Connected to the database successfully"); // Example: Listing collections in the database for (String name : database.listCollectionNames()) { getLogger().info("Collection: " + name); } } catch (Exception ex) { ex.printStackTrace(); } } CODE_BLOCK:
@Override public void onDisable() { getLogger().info("MongoDBTest plugin has been disabled"); } public MongoDatabase getDatabase() { return database; } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
@Override public void onDisable() { getLogger().info("MongoDBTest plugin has been disabled"); } public MongoDatabase getDatabase() { return database; } CODE_BLOCK:
@Override public void onDisable() { getLogger().info("MongoDBTest plugin has been disabled"); } public MongoDatabase getDatabase() { return database; } CODE_BLOCK:
mongo.uri=<Place Your connection URI here> Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
mongo.uri=<Place Your connection URI here> CODE_BLOCK:
mongo.uri=<Place Your connection URI here> CODE_BLOCK:
mvn clean package Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
mvn clean package CODE_BLOCK:
mvn clean package CODE_BLOCK:
aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % pwd
/Users/aasawari.sahasrabuddhe/minecraftServer/plugins
aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % ls
MinecraftMongoDB-1.0-SNAPSHOT.jar bStats
aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % pwd
/Users/aasawari.sahasrabuddhe/minecraftServer/plugins
aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % ls
MinecraftMongoDB-1.0-SNAPSHOT.jar bStats
aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % CODE_BLOCK:
aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % pwd
/Users/aasawari.sahasrabuddhe/minecraftServer/plugins
aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % ls
MinecraftMongoDB-1.0-SNAPSHOT.jar bStats
aasawari.sahasrabuddhe@M-C02DV42LML85 plugins % CODE_BLOCK:
[MongoDBConnection] Connected to the database successfully Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
[MongoDBConnection] Connected to the database successfully CODE_BLOCK:
[MongoDBConnection] Connected to the database successfully - Create a free Atlas cluster: Sign up and create your free Atlas cluster to obtain your MongoDB connection string.
- Download the Minecraft server: Use PaperMC to download the Minecraft server and enhance Minecraft’s ecosystem.
- Download the latest JDK: We will be using Java Version 22 for this tutorial.
- Update your MongoDB Java Driver version: For this tutorial, we will be using version 5.1.0.
how-totutorialguidedev.toaimlserverssldatabasegitgithub