|
| 1 | +# Example Plugin Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `networkdataapi-example-plugin` module demonstrates how to create a minimal plugin that leverages the NetworkDataAPI shared MongoDB connection to create and manage its own database collections. |
| 6 | + |
| 7 | +## What It Demonstrates |
| 8 | + |
| 9 | +This example plugin showcases the following capabilities: |
| 10 | + |
| 11 | +### 1. **Shared MongoDB Connection** |
| 12 | + - Uses NetworkDataAPI's existing MongoDB connection |
| 13 | + - No need to create a separate database connection |
| 14 | + - Automatically benefits from connection pooling and retry logic |
| 15 | + |
| 16 | +### 2. **Isolated Database** |
| 17 | + - Creates its own dedicated MongoDB database (`example_plugin`) |
| 18 | + - Complete data isolation from other plugins |
| 19 | + - No conflicts with other plugin data |
| 20 | + |
| 21 | +### 3. **Custom Collections** |
| 22 | + - Creates a sample collection (`example_collection`) |
| 23 | + - Stores documents with fields: `name`, `value`, `timestamp`, `updated` |
| 24 | + - Demonstrates proper collection initialization |
| 25 | + |
| 26 | +### 4. **CRUD Operations** |
| 27 | + - **Create**: Insert new documents |
| 28 | + - **Read**: Query documents by name, value, or get all |
| 29 | + - **Update**: Modify existing documents |
| 30 | + - **Delete**: Remove documents |
| 31 | + |
| 32 | +### 5. **Performance Optimization** |
| 33 | + - Creates indexes on frequently queried fields |
| 34 | + - Uses compound indexes for complex queries |
| 35 | + - Demonstrates MongoDB best practices |
| 36 | + |
| 37 | +### 6. **Comprehensive Logging** |
| 38 | + - All operations are logged with detailed information |
| 39 | + - Easy to debug and understand what's happening |
| 40 | + - Helps developers learn MongoDB operations |
| 41 | + |
| 42 | +## Directory Structure |
| 43 | + |
| 44 | +``` |
| 45 | +networkdataapi-example-plugin/ |
| 46 | +├── src/main/java/com/astroid/stijnjakobs/networkdataapi/example/ |
| 47 | +│ ├── ExamplePlugin.java # Main plugin class |
| 48 | +│ ├── ExampleDataManager.java # MongoDB operations handler |
| 49 | +│ └── ExampleCommand.java # Command handler for testing |
| 50 | +├── src/main/resources/ |
| 51 | +│ └── plugin.yml # Plugin configuration |
| 52 | +├── pom.xml # Maven build configuration |
| 53 | +└── README.md # Plugin documentation |
| 54 | +``` |
| 55 | + |
| 56 | +## Key Classes |
| 57 | + |
| 58 | +### ExamplePlugin.java |
| 59 | +The main plugin class that: |
| 60 | +- Checks for NetworkDataAPI availability |
| 61 | +- Gets the API instance from APIRegistry |
| 62 | +- Creates an isolated database using `api.getDatabase("example_plugin")` |
| 63 | +- Initializes the data manager |
| 64 | +- Registers commands |
| 65 | + |
| 66 | +**Key Code:** |
| 67 | +```java |
| 68 | +// Get API instance |
| 69 | +NetworkDataAPIProvider api = APIRegistry.getAPI(); |
| 70 | + |
| 71 | +// Get dedicated database for this plugin |
| 72 | +MongoDatabase database = api.getDatabase("example_plugin"); |
| 73 | +``` |
| 74 | + |
| 75 | +### ExampleDataManager.java |
| 76 | +Manages all MongoDB operations: |
| 77 | +- Collection initialization |
| 78 | +- Index creation for performance |
| 79 | +- Insert, query, update, and delete operations |
| 80 | +- Statistics retrieval |
| 81 | +- Comprehensive operation logging |
| 82 | + |
| 83 | +**Key Features:** |
| 84 | +- Creates indexes on `name` and `value` fields |
| 85 | +- Demonstrates MongoDB filter operations |
| 86 | +- Shows update operations with multiple fields |
| 87 | +- Includes error handling and logging |
| 88 | + |
| 89 | +### ExampleCommand.java |
| 90 | +In-game command handler that provides: |
| 91 | +- `/example insert <name> <value>` - Insert a document |
| 92 | +- `/example query <name>` - Query by name |
| 93 | +- `/example queryall` - Query all documents |
| 94 | +- `/example queryvalue <min>` - Query by value |
| 95 | +- `/example update <name> <value>` - Update a document |
| 96 | +- `/example delete <name>` - Delete a document |
| 97 | +- `/example stats` - Show collection statistics |
| 98 | + |
| 99 | +## Building the Example Plugin |
| 100 | + |
| 101 | +### Prerequisites |
| 102 | +- Maven 3.6+ |
| 103 | +- Java 17+ |
| 104 | +- Network access to Maven repositories (Spigot/Paper API) |
| 105 | + |
| 106 | +### Build Command |
| 107 | +```bash |
| 108 | +cd networkdataapi-example-plugin |
| 109 | +mvn clean package |
| 110 | +``` |
| 111 | + |
| 112 | +The compiled JAR will be located at: |
| 113 | +``` |
| 114 | +networkdataapi-example-plugin/target/NetworkDataAPI-Example-1.0-SNAPSHOT.jar |
| 115 | +``` |
| 116 | + |
| 117 | +## Installation |
| 118 | + |
| 119 | +1. **Install NetworkDataAPI** first (prerequisite) |
| 120 | +2. **Place** `NetworkDataAPI-Example-1.0-SNAPSHOT.jar` in your `plugins/` folder |
| 121 | +3. **Start** your server |
| 122 | +4. **Use** `/example help` to see available commands |
| 123 | + |
| 124 | +## Usage Examples |
| 125 | + |
| 126 | +### Insert Sample Data |
| 127 | +``` |
| 128 | +/example insert apple 100 |
| 129 | +/example insert banana 200 |
| 130 | +/example insert cherry 50 |
| 131 | +/example insert orange 150 |
| 132 | +``` |
| 133 | + |
| 134 | +### Query Data |
| 135 | +``` |
| 136 | +# Query by name |
| 137 | +/example query apple |
| 138 | +
|
| 139 | +# Query all documents |
| 140 | +/example queryall |
| 141 | +
|
| 142 | +# Query documents with value > 100 |
| 143 | +/example queryvalue 100 |
| 144 | +``` |
| 145 | + |
| 146 | +### Update Data |
| 147 | +``` |
| 148 | +/example update apple 250 |
| 149 | +``` |
| 150 | + |
| 151 | +### Delete Data |
| 152 | +``` |
| 153 | +/example delete cherry |
| 154 | +``` |
| 155 | + |
| 156 | +### View Statistics |
| 157 | +``` |
| 158 | +/example stats |
| 159 | +``` |
| 160 | + |
| 161 | +## Learning Points |
| 162 | + |
| 163 | +### 1. How to Get the NetworkDataAPI Instance |
| 164 | +```java |
| 165 | +import com.astroid.stijnjakobs.networkdataapi.core.api.APIRegistry; |
| 166 | +import com.astroid.stijnjakobs.networkdataapi.core.api.NetworkDataAPIProvider; |
| 167 | + |
| 168 | +// In your plugin's onEnable() |
| 169 | +if (!APIRegistry.isAvailable()) { |
| 170 | + getLogger().severe("NetworkDataAPI not found!"); |
| 171 | + getServer().getPluginManager().disablePlugin(this); |
| 172 | + return; |
| 173 | +} |
| 174 | + |
| 175 | +NetworkDataAPIProvider api = APIRegistry.getAPI(); |
| 176 | +``` |
| 177 | + |
| 178 | +### 2. How to Get Your Own Database |
| 179 | +```java |
| 180 | +// Get a dedicated database for your plugin |
| 181 | +MongoDatabase database = api.getDatabase("your_plugin_name"); |
| 182 | +``` |
| 183 | + |
| 184 | +### 3. How to Create Collections |
| 185 | +```java |
| 186 | +MongoCollection<Document> collection = database.getCollection("your_collection"); |
| 187 | +``` |
| 188 | + |
| 189 | +### 4. How to Create Indexes |
| 190 | +```java |
| 191 | +import com.mongodb.client.model.Indexes; |
| 192 | + |
| 193 | +// Single field index |
| 194 | +collection.createIndex(Indexes.ascending("fieldName")); |
| 195 | + |
| 196 | +// Compound index |
| 197 | +collection.createIndex(Indexes.ascending("field1", "field2")); |
| 198 | +``` |
| 199 | + |
| 200 | +### 5. How to Insert Documents |
| 201 | +```java |
| 202 | +import org.bson.Document; |
| 203 | + |
| 204 | +Document doc = new Document() |
| 205 | + .append("name", "example") |
| 206 | + .append("value", 100) |
| 207 | + .append("timestamp", System.currentTimeMillis()); |
| 208 | + |
| 209 | +collection.insertOne(doc); |
| 210 | +``` |
| 211 | + |
| 212 | +### 6. How to Query Documents |
| 213 | +```java |
| 214 | +import com.mongodb.client.model.Filters; |
| 215 | + |
| 216 | +// Query by exact match |
| 217 | +Bson filter = Filters.eq("name", "example"); |
| 218 | +List<Document> results = new ArrayList<>(); |
| 219 | +collection.find(filter).into(results); |
| 220 | + |
| 221 | +// Query with comparison |
| 222 | +Bson filter = Filters.gt("value", 50); |
| 223 | +collection.find(filter).into(results); |
| 224 | +``` |
| 225 | + |
| 226 | +### 7. How to Update Documents |
| 227 | +```java |
| 228 | +import com.mongodb.client.model.Updates; |
| 229 | + |
| 230 | +Bson filter = Filters.eq("name", "example"); |
| 231 | +Bson update = Updates.combine( |
| 232 | + Updates.set("value", 200), |
| 233 | + Updates.set("updated", true), |
| 234 | + Updates.set("lastModified", System.currentTimeMillis()) |
| 235 | +); |
| 236 | + |
| 237 | +collection.updateOne(filter, update); |
| 238 | +``` |
| 239 | + |
| 240 | +### 8. How to Delete Documents |
| 241 | +```java |
| 242 | +Bson filter = Filters.eq("name", "example"); |
| 243 | +collection.deleteOne(filter); |
| 244 | +``` |
| 245 | + |
| 246 | +## Benefits of This Approach |
| 247 | + |
| 248 | +### ✅ No Separate Database Connection |
| 249 | +Your plugin doesn't need its own MongoDB connection. Just use NetworkDataAPI's shared connection. |
| 250 | + |
| 251 | +### ✅ Automatic Connection Management |
| 252 | +NetworkDataAPI handles: |
| 253 | +- Connection pooling |
| 254 | +- Automatic reconnection |
| 255 | +- Error retry logic |
| 256 | +- Resource cleanup |
| 257 | + |
| 258 | +### ✅ Complete Data Isolation |
| 259 | +Each plugin can have its own database, preventing conflicts. |
| 260 | + |
| 261 | +### ✅ Full MongoDB API Access |
| 262 | +Use all MongoDB operations without restrictions. |
| 263 | + |
| 264 | +### ✅ Easy Setup |
| 265 | +Just one line of code to get started: `api.getDatabase("your_plugin_name")` |
| 266 | + |
| 267 | +### ✅ Better Resource Usage |
| 268 | +All plugins share the same connection pool, reducing overhead. |
| 269 | + |
| 270 | +## Use Cases |
| 271 | + |
| 272 | +This pattern is perfect for: |
| 273 | +- **Cosmetics Plugins**: Store owned cosmetics, equipped items |
| 274 | +- **Economy Plugins**: Store player balances, transactions |
| 275 | +- **Guilds/Clans Plugins**: Store guild data, members, ranks |
| 276 | +- **Stats Plugins**: Store player statistics, achievements |
| 277 | +- **Punishment Systems**: Store bans, mutes, warnings |
| 278 | +- **Custom Game Modes**: Store game data, player progress |
| 279 | + |
| 280 | +## Next Steps |
| 281 | + |
| 282 | +1. **Study the Code**: Review each class to understand the implementation |
| 283 | +2. **Test It**: Run the commands in-game and watch the server logs |
| 284 | +3. **Customize It**: Adapt the code for your own plugin's needs |
| 285 | +4. **Build Your Plugin**: Use this as a template for your own database operations |
| 286 | + |
| 287 | +## Support |
| 288 | + |
| 289 | +- Read the main [API_DOCUMENTATION.md](../API_DOCUMENTATION.md) |
| 290 | +- Review the example plugin's [README.md](networkdataapi-example-plugin/README.md) |
| 291 | +- Check the code comments for detailed explanations |
| 292 | +- Open an issue on GitHub for questions |
| 293 | + |
| 294 | +--- |
| 295 | + |
| 296 | +**This example plugin is a complete, working reference for building plugins that use NetworkDataAPI!** |
0 commit comments