|
| 1 | +# Quick Start Guide - Example Plugin |
| 2 | + |
| 3 | +## 🚀 5-Minute Quick Start |
| 4 | + |
| 5 | +### Prerequisites |
| 6 | +- NetworkDataAPI installed and configured |
| 7 | +- Paper/Spigot server (1.20+) |
| 8 | +- MongoDB running and accessible |
| 9 | + |
| 10 | +### Step 1: Get the Example Plugin |
| 11 | +The example plugin is included in this repository at `networkdataapi-example-plugin/` |
| 12 | + |
| 13 | +### Step 2: Build the Plugin |
| 14 | +```bash |
| 15 | +cd networkdataapi-example-plugin |
| 16 | +mvn clean package |
| 17 | +``` |
| 18 | + |
| 19 | +### Step 3: Install |
| 20 | +```bash |
| 21 | +# Copy to your server |
| 22 | +cp target/NetworkDataAPI-Example-1.0-SNAPSHOT.jar /path/to/server/plugins/ |
| 23 | + |
| 24 | +# Restart your server |
| 25 | +``` |
| 26 | + |
| 27 | +### Step 4: Test It! |
| 28 | + |
| 29 | +#### Insert Some Data |
| 30 | +``` |
| 31 | +/example insert apple 100 |
| 32 | +/example insert banana 200 |
| 33 | +/example insert cherry 50 |
| 34 | +/example insert orange 150 |
| 35 | +``` |
| 36 | + |
| 37 | +#### Query the Data |
| 38 | +``` |
| 39 | +/example queryall # See all documents |
| 40 | +/example query apple # Find by name |
| 41 | +/example queryvalue 100 # Find all with value > 100 |
| 42 | +``` |
| 43 | + |
| 44 | +#### Update Data |
| 45 | +``` |
| 46 | +/example update apple 250 # Change apple's value to 250 |
| 47 | +/example query apple # Verify the update |
| 48 | +``` |
| 49 | + |
| 50 | +#### Delete Data |
| 51 | +``` |
| 52 | +/example delete cherry # Remove cherry |
| 53 | +/example queryall # Verify it's gone |
| 54 | +``` |
| 55 | + |
| 56 | +#### Check Statistics |
| 57 | +``` |
| 58 | +/example stats # See collection stats |
| 59 | +``` |
| 60 | + |
| 61 | +### Step 5: Check the Logs |
| 62 | +Look at your server console to see detailed logging for each operation: |
| 63 | + |
| 64 | +``` |
| 65 | +[ExamplePlugin] ======================================== |
| 66 | +[ExamplePlugin] INSERT OPERATION |
| 67 | +[ExamplePlugin] ======================================== |
| 68 | +[ExamplePlugin] Creating document: {"name":"apple","value":100,"timestamp":1699123456789,"updated":false} |
| 69 | +[ExamplePlugin] Insert successful! |
| 70 | +[ExamplePlugin] Inserted ID: BsonObjectId{value=...} |
| 71 | +[ExamplePlugin] Document inserted into collection 'example_collection' |
| 72 | +[ExamplePlugin] ======================================== |
| 73 | +``` |
| 74 | + |
| 75 | +## 🎓 What You Just Learned |
| 76 | + |
| 77 | +### Database Concepts |
| 78 | +- ✅ Created an isolated MongoDB database (`example_plugin`) |
| 79 | +- ✅ Used a custom collection (`example_collection`) |
| 80 | +- ✅ Performed CRUD operations (Create, Read, Update, Delete) |
| 81 | + |
| 82 | +### MongoDB Operations |
| 83 | +- ✅ **Insert**: Added documents with `insertOne()` |
| 84 | +- ✅ **Query**: Found documents with `find()` and filters |
| 85 | +- ✅ **Update**: Modified documents with `updateOne()` |
| 86 | +- ✅ **Delete**: Removed documents with `deleteOne()` |
| 87 | + |
| 88 | +### NetworkDataAPI Integration |
| 89 | +- ✅ Leveraged shared MongoDB connection |
| 90 | +- ✅ No separate database connection needed |
| 91 | +- ✅ Automatic connection management |
| 92 | +- ✅ Used the public API (`APIRegistry.getAPI()`) |
| 93 | + |
| 94 | +## 📖 Next Steps |
| 95 | + |
| 96 | +### 1. Study the Code |
| 97 | +Start with these files in order: |
| 98 | +1. **ExamplePlugin.java** - Main plugin class, shows API integration |
| 99 | +2. **ExampleDataManager.java** - All MongoDB operations |
| 100 | +3. **ExampleCommand.java** - Command handling |
| 101 | + |
| 102 | +### 2. Customize for Your Plugin |
| 103 | +```java |
| 104 | +// Instead of "example_plugin", use your plugin name |
| 105 | +MongoDatabase database = api.getDatabase("yourplugin_name"); |
| 106 | + |
| 107 | +// Instead of "example_collection", use your collection name |
| 108 | +MongoCollection<Document> collection = database.getCollection("your_collection"); |
| 109 | + |
| 110 | +// Add your own fields to documents |
| 111 | +Document doc = new Document() |
| 112 | + .append("yourField1", value1) |
| 113 | + .append("yourField2", value2) |
| 114 | + .append("timestamp", System.currentTimeMillis()); |
| 115 | +``` |
| 116 | + |
| 117 | +### 3. Add More Collections |
| 118 | +```java |
| 119 | +// You can have multiple collections |
| 120 | +MongoCollection<Document> users = database.getCollection("users"); |
| 121 | +MongoCollection<Document> items = database.getCollection("items"); |
| 122 | +MongoCollection<Document> transactions = database.getCollection("transactions"); |
| 123 | +``` |
| 124 | + |
| 125 | +### 4. Add Indexes for Performance |
| 126 | +```java |
| 127 | +// Create indexes on frequently queried fields |
| 128 | +collection.createIndex(Indexes.ascending("playerId")); |
| 129 | +collection.createIndex(Indexes.descending("timestamp")); |
| 130 | +collection.createIndex(Indexes.ascending("category", "type")); // Compound index |
| 131 | +``` |
| 132 | + |
| 133 | +### 5. Build Your Features |
| 134 | +Use the example as a template for: |
| 135 | +- **Cosmetics System**: Store owned cosmetics, equipped items |
| 136 | +- **Economy Plugin**: Store balances, transactions |
| 137 | +- **Guild System**: Store guilds, members, ranks |
| 138 | +- **Stats Tracker**: Store player statistics |
| 139 | +- **Punishment System**: Store bans, mutes, warnings |
| 140 | + |
| 141 | +## 🔍 Debugging Tips |
| 142 | + |
| 143 | +### Enable Detailed Logging |
| 144 | +All operations in the example plugin already log detailed information. Just watch your server console! |
| 145 | + |
| 146 | +### Test Each Operation |
| 147 | +Use the in-game commands to test: |
| 148 | +1. Insert a document |
| 149 | +2. Query it back |
| 150 | +3. Update it |
| 151 | +4. Query again to verify |
| 152 | +5. Delete it |
| 153 | +6. Query to confirm deletion |
| 154 | + |
| 155 | +### Common Issues |
| 156 | + |
| 157 | +**Problem**: "NetworkDataAPI not found" |
| 158 | +**Solution**: Ensure NetworkDataAPI is installed and loaded first |
| 159 | + |
| 160 | +**Problem**: No documents found |
| 161 | +**Solution**: Insert some data first with `/example insert` |
| 162 | + |
| 163 | +**Problem**: Update doesn't work |
| 164 | +**Solution**: Document must exist first - verify with `/example query` |
| 165 | + |
| 166 | +## 📊 Understanding the Database Structure |
| 167 | + |
| 168 | +### Database: `example_plugin` |
| 169 | +- Created automatically when you first use it |
| 170 | +- Completely isolated from other plugins |
| 171 | +- Can be backed up independently |
| 172 | + |
| 173 | +### Collection: `example_collection` |
| 174 | +- Created automatically on first use |
| 175 | +- Stores documents with this structure: |
| 176 | +```json |
| 177 | +{ |
| 178 | + "_id": ObjectId("..."), // Auto-generated MongoDB ID |
| 179 | + "name": "apple", // Your name field |
| 180 | + "value": 100, // Your value field |
| 181 | + "timestamp": 1699123456789, // Creation timestamp |
| 182 | + "updated": false, // Update flag |
| 183 | + "lastModified": 1699123999999 // Last modification time (if updated) |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +### Indexes Created |
| 188 | +- `name` (ascending) - Fast name lookups |
| 189 | +- `value` (ascending) - Fast value queries |
| 190 | +- `name, value` (compound) - Fast combined queries |
| 191 | + |
| 192 | +## 💡 Pro Tips |
| 193 | + |
| 194 | +### 1. Use Asynchronous Operations |
| 195 | +For production plugins, wrap MongoDB operations in async tasks: |
| 196 | +```java |
| 197 | +Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { |
| 198 | + // MongoDB operations here |
| 199 | + Document doc = collection.find(Filters.eq("name", "example")).first(); |
| 200 | + |
| 201 | + // Switch back to main thread for Bukkit API calls |
| 202 | + Bukkit.getScheduler().runTask(plugin, () -> { |
| 203 | + player.sendMessage("Found: " + doc.toJson()); |
| 204 | + }); |
| 205 | +}); |
| 206 | +``` |
| 207 | + |
| 208 | +### 2. Use Batch Operations |
| 209 | +For multiple inserts, use `insertMany()`: |
| 210 | +```java |
| 211 | +List<Document> documents = Arrays.asList( |
| 212 | + new Document("name", "item1").append("value", 10), |
| 213 | + new Document("name", "item2").append("value", 20), |
| 214 | + new Document("name", "item3").append("value", 30) |
| 215 | +); |
| 216 | +collection.insertMany(documents); |
| 217 | +``` |
| 218 | + |
| 219 | +### 3. Use Projections |
| 220 | +Only fetch the fields you need: |
| 221 | +```java |
| 222 | +Bson projection = Projections.fields( |
| 223 | + Projections.include("name", "value"), |
| 224 | + Projections.excludeId() |
| 225 | +); |
| 226 | +Document doc = collection.find(filter).projection(projection).first(); |
| 227 | +``` |
| 228 | + |
| 229 | +### 4. Use Aggregation |
| 230 | +For complex queries: |
| 231 | +```java |
| 232 | +List<Bson> pipeline = Arrays.asList( |
| 233 | + Aggregates.match(Filters.gt("value", 50)), |
| 234 | + Aggregates.group("$name", Accumulators.avg("avgValue", "$value")), |
| 235 | + Aggregates.sort(Sorts.descending("avgValue")) |
| 236 | +); |
| 237 | +List<Document> results = collection.aggregate(pipeline).into(new ArrayList<>()); |
| 238 | +``` |
| 239 | + |
| 240 | +## 🎯 You're Ready! |
| 241 | + |
| 242 | +You now know how to: |
| 243 | +- ✅ Use NetworkDataAPI's shared connection |
| 244 | +- ✅ Create your own database and collections |
| 245 | +- ✅ Perform all CRUD operations |
| 246 | +- ✅ Create indexes for performance |
| 247 | +- ✅ Handle errors properly |
| 248 | +- ✅ Log operations for debugging |
| 249 | + |
| 250 | +**Start building your plugin!** Use this example as a reference and customize it for your needs. |
| 251 | + |
| 252 | +## 📚 Additional Resources |
| 253 | + |
| 254 | +- [Example Plugin README](networkdataapi-example-plugin/README.md) |
| 255 | +- [Example Plugin Guide](EXAMPLE_PLUGIN_GUIDE.md) |
| 256 | +- [API Documentation](API_DOCUMENTATION.md) |
| 257 | +- [MongoDB Java Driver Docs](https://mongodb.github.io/mongo-java-driver/) |
| 258 | + |
| 259 | +--- |
| 260 | + |
| 261 | +**Happy coding! 🚀** |
0 commit comments