|
| 1 | +# NetworkDataAPI - Implementation Notes |
| 2 | + |
| 3 | +**Date:** November 14, 2025 |
| 4 | +**Version:** 1.0-SNAPSHOT |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## ✅ Core Principles |
| 9 | + |
| 10 | +NetworkDataAPI is designed with ONE primary purpose: |
| 11 | + |
| 12 | +**Provide a shared MongoDB connection pool for all plugins on a Minecraft server.** |
| 13 | + |
| 14 | +### What NetworkDataAPI IS: |
| 15 | +- ✅ A **shared MongoDB connection pool** manager |
| 16 | +- ✅ A **connection layer** between plugins and MongoDB |
| 17 | +- ✅ A **high-level API** for common database operations |
| 18 | +- ✅ An **automatic reconnection** handler |
| 19 | +- ✅ A **caching layer** to reduce database load |
| 20 | + |
| 21 | +### What NetworkDataAPI IS NOT: |
| 22 | +- ❌ An automatic player data manager |
| 23 | +- ❌ A player tracking system |
| 24 | +- ❌ A statistics/economy/cosmetics plugin |
| 25 | +- ❌ A system that creates default data |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 🎯 Design Philosophy |
| 30 | + |
| 31 | +### 1. **No Automatic Data Creation** |
| 32 | + |
| 33 | +NetworkDataAPI does NOT create any player data automatically. There are no default documents, no automatic player tracking, and no pre-defined data structures. |
| 34 | + |
| 35 | +**Why?** |
| 36 | +- Each plugin should control its own data |
| 37 | +- Prevents unwanted data in the database |
| 38 | +- Avoids conflicts between plugins |
| 39 | +- Gives developers complete flexibility |
| 40 | + |
| 41 | +### 2. **Action-Based Data Creation** |
| 42 | + |
| 43 | +Data should only be created when it's actually needed: |
| 44 | + |
| 45 | +```java |
| 46 | +// ❌ WRONG: Create empty data on player join |
| 47 | +@EventHandler |
| 48 | +public void onPlayerJoin(PlayerJoinEvent event) { |
| 49 | + Document data = new Document("uuid", uuid).append("coins", 0); |
| 50 | + collection.insertOne(data); // Why create empty data? |
| 51 | +} |
| 52 | + |
| 53 | +// ✅ CORRECT: Create data when relevant |
| 54 | +public void claimCosmetic(Player player, String cosmeticId) { |
| 55 | + Document data = collection.find(Filters.eq("uuid", uuid)).first(); |
| 56 | + if (data == null) { |
| 57 | + // First cosmetic - NOW create the document |
| 58 | + data = new Document("uuid", uuid).append("cosmetics", List.of(cosmeticId)); |
| 59 | + collection.insertOne(data); |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### 3. **Shared Connection Pool** |
| 65 | + |
| 66 | +All plugins on a server share ONE connection pool: |
| 67 | + |
| 68 | +**Without NetworkDataAPI:** |
| 69 | +- Cosmetics Plugin: 10 connections |
| 70 | +- Economy Plugin: 10 connections |
| 71 | +- Stats Plugin: 10 connections |
| 72 | +- **Total: 30 connections per server!** |
| 73 | + |
| 74 | +**With NetworkDataAPI:** |
| 75 | +- NetworkDataAPI: 1 shared pool (max 100 connections) |
| 76 | +- All plugins use this shared pool |
| 77 | +- **Total: Max 100 connections, shared efficiently!** |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## 📚 API Usage Patterns |
| 82 | + |
| 83 | +### Option 1: Dedicated Database per Plugin (RECOMMENDED) |
| 84 | + |
| 85 | +Each plugin gets its own MongoDB database: |
| 86 | + |
| 87 | +```java |
| 88 | +NetworkDataAPIProvider api = APIRegistry.getAPI(); |
| 89 | + |
| 90 | +// Cosmetics plugin |
| 91 | +MongoDatabase cosmeticsDB = api.getDatabase("cosmetics"); |
| 92 | +MongoCollection<Document> items = cosmeticsDB.getCollection("player_cosmetics"); |
| 93 | + |
| 94 | +// Economy plugin |
| 95 | +MongoDatabase economyDB = api.getDatabase("economy"); |
| 96 | +MongoCollection<Document> balances = economyDB.getCollection("balances"); |
| 97 | +``` |
| 98 | + |
| 99 | +**Benefits:** |
| 100 | +- Complete data isolation |
| 101 | +- No conflicts possible |
| 102 | +- Easier backups per plugin |
| 103 | +- Clear separation of concerns |
| 104 | + |
| 105 | +### Option 2: Shared Database with Own Collections |
| 106 | + |
| 107 | +All plugins use the default database but with separate collections: |
| 108 | + |
| 109 | +```java |
| 110 | +NetworkDataAPIProvider api = APIRegistry.getAPI(); |
| 111 | +MongoDatabase db = api.getDatabase(); // Default from config |
| 112 | + |
| 113 | +MongoCollection<Document> cosmetics = db.getCollection("cosmetics"); |
| 114 | +MongoCollection<Document> economy = db.getCollection("economy"); |
| 115 | +``` |
| 116 | + |
| 117 | +**Use when:** |
| 118 | +- Smaller plugins |
| 119 | +- Need cross-plugin queries |
| 120 | +- Simpler setup |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## 🔧 Implementation Details |
| 125 | + |
| 126 | +### Removed Features (v1.0) |
| 127 | + |
| 128 | +The following features were **removed** to keep NetworkDataAPI focused on being a connection layer: |
| 129 | + |
| 130 | +1. **Automatic PlayerConnectionListener registration** - Removed |
| 131 | +2. **Default player data creation** - Removed |
| 132 | +3. **Automatic join/quit tracking** - Removed |
| 133 | +4. **Game-specific default fields** (coins, level, experience) - Removed |
| 134 | + |
| 135 | +### What Remains |
| 136 | + |
| 137 | +1. **PlayerDataService** - Optional service for shared player data |
| 138 | +2. **Direct MongoDB access** - Full MongoDB API available |
| 139 | +3. **Connection pooling** - Shared pool for all plugins |
| 140 | +4. **Caching** - Optional caching layer |
| 141 | +5. **REST API** - Optional HTTP endpoints |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## 📖 Documentation Structure |
| 146 | + |
| 147 | +### For End Users: |
| 148 | +- **README.md** - Quick overview and installation |
| 149 | +- **Installation guide** - Step-by-step setup |
| 150 | + |
| 151 | +### For Plugin Developers: |
| 152 | +- **API_DOCUMENTATION.md** - Complete API reference |
| 153 | +- **EXAMPLE_PLUGIN_GUIDE.md** - Working example walkthrough |
| 154 | +- **Example Plugin** - Full working code in `networkdataapi-example-plugin/` |
| 155 | + |
| 156 | +### For Contributors: |
| 157 | +- **CONTRIBUTING.md** - Contribution guidelines |
| 158 | +- **Code structure** - Well-documented source code |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## 🎓 Best Practices |
| 163 | + |
| 164 | +### For Plugin Developers Using NetworkDataAPI: |
| 165 | + |
| 166 | +1. **Always use async operations** |
| 167 | + ```java |
| 168 | + CompletableFuture.supplyAsync(() -> { |
| 169 | + return collection.find(filter).first(); |
| 170 | + }).thenAccept(data -> { |
| 171 | + // Process result |
| 172 | + }); |
| 173 | + ``` |
| 174 | + |
| 175 | +2. **Create indexes for frequently queried fields** |
| 176 | + ```java |
| 177 | + collection.createIndex(Indexes.ascending("uuid")); |
| 178 | + collection.createIndex(Indexes.descending("coins")); |
| 179 | + ``` |
| 180 | + |
| 181 | +3. **Use dedicated databases for large plugins** |
| 182 | + ```java |
| 183 | + MongoDatabase myDB = api.getDatabase("my_plugin"); |
| 184 | + ``` |
| 185 | + |
| 186 | +4. **Handle errors gracefully** |
| 187 | + ```java |
| 188 | + .exceptionally(throwable -> { |
| 189 | + logger.error("Database error", throwable); |
| 190 | + return null; |
| 191 | + }); |
| 192 | + ``` |
| 193 | + |
| 194 | +5. **Create data only when needed** (action-based) |
| 195 | + - Not on player join |
| 196 | + - Only when player performs relevant action |
| 197 | + |
| 198 | +--- |
| 199 | + |
| 200 | +## 🚀 Real-World Example |
| 201 | + |
| 202 | +### Cosmetics Plugin Architecture |
| 203 | + |
| 204 | +```java |
| 205 | +public class CosmeticsPlugin extends JavaPlugin { |
| 206 | + private MongoDatabase database; |
| 207 | + private MongoCollection<Document> playerCosmetics; |
| 208 | + |
| 209 | + @Override |
| 210 | + public void onEnable() { |
| 211 | + NetworkDataAPIProvider api = APIRegistry.getAPI(); |
| 212 | + |
| 213 | + // Get dedicated database |
| 214 | + database = api.getDatabase("cosmetics"); |
| 215 | + playerCosmetics = database.getCollection("player_cosmetics"); |
| 216 | + |
| 217 | + // Create indexes |
| 218 | + playerCosmetics.createIndex(Indexes.ascending("uuid")); |
| 219 | + } |
| 220 | + |
| 221 | + // Data created when player claims cosmetic |
| 222 | + public void claimCosmetic(Player player, String cosmeticId) { |
| 223 | + UUID uuid = player.getUniqueId(); |
| 224 | + |
| 225 | + Document data = playerCosmetics.find( |
| 226 | + Filters.eq("uuid", uuid.toString()) |
| 227 | + ).first(); |
| 228 | + |
| 229 | + if (data == null) { |
| 230 | + // First cosmetic - create document NOW |
| 231 | + data = new Document() |
| 232 | + .append("uuid", uuid.toString()) |
| 233 | + .append("claimed", List.of(cosmeticId)) |
| 234 | + .append("equipped", cosmeticId) |
| 235 | + .append("firstClaim", System.currentTimeMillis()); |
| 236 | + |
| 237 | + playerCosmetics.insertOne(data); |
| 238 | + } else { |
| 239 | + // Add to existing |
| 240 | + playerCosmetics.updateOne( |
| 241 | + Filters.eq("uuid", uuid.toString()), |
| 242 | + Updates.addToSet("claimed", cosmeticId) |
| 243 | + ); |
| 244 | + } |
| 245 | + } |
| 246 | +} |
| 247 | +``` |
| 248 | + |
| 249 | +--- |
| 250 | + |
| 251 | +## 📊 Performance Considerations |
| 252 | + |
| 253 | +### Connection Pool Sizing |
| 254 | + |
| 255 | +Default configuration provides: |
| 256 | +- **Min pool size:** 10 connections |
| 257 | +- **Max pool size:** 100 connections |
| 258 | +- Shared across ALL plugins |
| 259 | + |
| 260 | +**This means:** |
| 261 | +- With 5 plugins, each effectively has access to 100 connections |
| 262 | +- Much better than 5 × 10 = 50 separate connections |
| 263 | +- Automatic load balancing across plugins |
| 264 | + |
| 265 | +### Caching Strategy |
| 266 | + |
| 267 | +NetworkDataAPI includes built-in caching: |
| 268 | +- **Default:** 10,000 entries max |
| 269 | +- **Expiry:** 5 minutes after write, 10 minutes after access |
| 270 | +- **Reduces database load by 80%+** |
| 271 | + |
| 272 | +--- |
| 273 | + |
| 274 | +## ✨ Summary |
| 275 | + |
| 276 | +NetworkDataAPI is a **pure connection layer** that: |
| 277 | +1. Provides shared MongoDB connection pool |
| 278 | +2. Offers high-level API for convenience |
| 279 | +3. Handles automatic reconnection |
| 280 | +4. Provides optional caching |
| 281 | +5. Lets plugins control their own data |
| 282 | + |
| 283 | +**It does NOT:** |
| 284 | +1. Track players automatically |
| 285 | +2. Create default data |
| 286 | +3. Make decisions about data structure |
| 287 | +4. Impose any data schema |
| 288 | + |
| 289 | +**Result:** Maximum flexibility + Minimum resource usage |
| 290 | + |
| 291 | +--- |
| 292 | + |
| 293 | +*For more information, see the complete API documentation in `API_DOCUMENTATION.md`* |
| 294 | + |
0 commit comments