The networkdataapi-example-plugin module demonstrates how to create a plugin that leverages NetworkDataAPI's shared MongoDB connection to create and manage its own database collections.
This example shows the CORRECT way to use NetworkDataAPI - as a database connection layer, not an automatic data manager.
- Uses NetworkDataAPI's existing MongoDB connection pool
- No need to create a separate database connection in your plugin
- Automatically benefits from connection pooling, caching, and retry logic
- Creates its own dedicated MongoDB database (
example_plugin) - Complete data isolation from other plugins
- No conflicts with other plugin data
- Each plugin can have its own database!
- Creates a sample collection (
example_collection) - Stores documents with custom fields:
name,value,timestamp,updated - Demonstrates proper collection initialization and indexing
- Create: Insert new documents
- Read: Query documents by name, value, or get all
- Update: Modify existing documents
- Delete: Remove documents
- Creates indexes on frequently queried fields
- Uses compound indexes for complex queries
- Demonstrates MongoDB best practices
- All operations are logged with detailed information
- Easy to debug and understand what's happening
- Helps developers learn MongoDB operations
networkdataapi-example-plugin/
├── src/main/java/com/astroid/stijnjakobs/networkdataapi/example/
│ ├── ExamplePlugin.java # Main plugin class
│ ├── ExampleDataManager.java # MongoDB operations handler
│ └── ExampleCommand.java # Command handler for testing
├── src/main/resources/
│ └── plugin.yml # Plugin configuration
├── pom.xml # Maven build configuration
└── README.md # Plugin documentation
The main plugin class that:
- Checks for NetworkDataAPI availability
- Gets the API instance from APIRegistry
- Creates an isolated database using
api.getDatabase("example_plugin") - Initializes the data manager
- Registers commands
Key Code:
// Get API instance
NetworkDataAPIProvider api = APIRegistry.getAPI();
// Get dedicated database for this plugin
MongoDatabase database = api.getDatabase("example_plugin");Manages all MongoDB operations:
- Collection initialization
- Index creation for performance
- Insert, query, update, and delete operations
- Statistics retrieval
- Comprehensive operation logging
Key Features:
- Creates indexes on
nameandvaluefields - Demonstrates MongoDB filter operations
- Shows update operations with multiple fields
- Includes error handling and logging
In-game command handler that provides:
/example insert <name> <value>- Insert a document/example query <name>- Query by name/example queryall- Query all documents/example queryvalue <min>- Query by value/example update <name> <value>- Update a document/example delete <name>- Delete a document/example stats- Show collection statistics
- Maven 3.6+
- Java 17+
- Network access to Maven repositories (Spigot/Paper API)
cd networkdataapi-example-plugin
mvn clean packageThe compiled JAR will be located at:
networkdataapi-example-plugin/target/NetworkDataAPI-Example-1.0-SNAPSHOT.jar
- Install NetworkDataAPI first (prerequisite)
- Place
NetworkDataAPI-Example-1.0-SNAPSHOT.jarin yourplugins/folder - Start your server
- Use
/example helpto see available commands
/example insert apple 100
/example insert banana 200
/example insert cherry 50
/example insert orange 150
# Query by name
/example query apple
# Query all documents
/example queryall
# Query documents with value > 100
/example queryvalue 100
/example update apple 250
/example delete cherry
/example stats
import api.com.cynive.networkdataapi.core.APIRegistry;
import api.com.cynive.networkdataapi.core.NetworkDataAPIProvider;
// In your plugin's onEnable()
if (!APIRegistry.isAvailable()) {
getLogger().severe("NetworkDataAPI not found!");
getServer().getPluginManager().disablePlugin(this);
return;
}
NetworkDataAPIProvider api = APIRegistry.getAPI();// Get a dedicated database for your plugin
MongoDatabase database = api.getDatabase("your_plugin_name");MongoCollection<Document> collection = database.getCollection("your_collection");import com.mongodb.client.model.Indexes;
// Single field index
collection.createIndex(Indexes.ascending("fieldName"));
// Compound index
collection.createIndex(Indexes.ascending("field1", "field2"));import org.bson.Document;
Document doc = new Document()
.append("name", "example")
.append("value", 100)
.append("timestamp", System.currentTimeMillis());
collection.insertOne(doc);import com.mongodb.client.model.Filters;
// Query by exact match
Bson filter = Filters.eq("name", "example");
List<Document> results = new ArrayList<>();
collection.find(filter).into(results);
// Query with comparison
Bson filter = Filters.gt("value", 50);
collection.find(filter).into(results);import com.mongodb.client.model.Updates;
Bson filter = Filters.eq("name", "example");
Bson update = Updates.combine(
Updates.set("value", 200),
Updates.set("updated", true),
Updates.set("lastModified", System.currentTimeMillis())
);
collection.updateOne(filter, update);Bson filter = Filters.eq("name", "example");
collection.deleteOne(filter);Your plugin doesn't need its own MongoDB connection. Just use NetworkDataAPI's shared connection.
NetworkDataAPI handles:
- Connection pooling
- Automatic reconnection
- Error retry logic
- Resource cleanup
Each plugin can have its own database, preventing conflicts.
Use all MongoDB operations without restrictions.
Just one line of code to get started: api.getDatabase("your_plugin_name")
All plugins share the same connection pool, reducing overhead.
This pattern is perfect for:
- Cosmetics Plugins: Store owned cosmetics, equipped items
- Economy Plugins: Store player balances, transactions
- Guilds/Clans Plugins: Store guild data, members, ranks
- Stats Plugins: Store player statistics, achievements
- Punishment Systems: Store bans, mutes, warnings
- Custom Game Modes: Store game data, player progress
- Study the Code: Review each class to understand the implementation
- Test It: Run the commands in-game and watch the server logs
- Customize It: Adapt the code for your own plugin's needs
- Build Your Plugin: Use this as a template for your own database operations
- Read the main API_DOCUMENTATION.md
- Review the example plugin's README.md
- Check the code comments for detailed explanations
- Open an issue on GitHub for questions
This example plugin is a complete, working reference for building plugins that use NetworkDataAPI!