API Best Practices
Guidelines for effective integration with Vexor Core plugins.
Performance
Use Async Operations
// ❌ BAD - Blocks main thread
public void loadData() {
DatabaseData data = database.query(); // Slow!
applyData(data);
}
// ✅ GOOD - Async operation
public void loadData() {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
DatabaseData data = database.query();
Bukkit.getScheduler().runTask(plugin, () -> {
applyData(data); // Back on main thread
});
});
}
Cache Plugin Instances
// ❌ BAD - Repeated lookups
public void doSomething() {
PivotPoly pp = (PivotPoly) Bukkit.getPluginManager().getPlugin("PivotPoly");
pp.getDoorManager().getDoors(); // Called many times!
}
// ✅ GOOD - Cache instance
private final PivotPoly pivotPoly;
public MyPlugin() {
this.pivotPoly = (PivotPoly) Bukkit.getPluginManager().getPlugin("PivotPoly");
}
Safety
Always Null Check
// ❌ BAD - Assumes plugin exists
PivotPoly pp = (PivotPoly) Bukkit.getPluginManager().getPlugin("PivotPoly");
DoorManager manager = pp.getDoorManager(); // NullPointerException!
// ✅ GOOD - Null safe
PivotPoly pp = (PivotPoly) Bukkit.getPluginManager().getPlugin("PivotPoly");
if (pp != null) {
DoorManager manager = pp.getDoorManager();
// Safe to use
}
Validate Input
// ✅ GOOD - Validate everything
public void createDoor(Player player, String name) {
if (player == null) throw new IllegalArgumentException("Player cannot be null");
if (name == null || name.isEmpty()) {
player.sendMessage("§cName cannot be empty!");
return;
}
if (name.length() > 32) {
player.sendMessage("§cName too long!");
return;
}
// Proceed with validated input
}
Compatibility
Use Soft Dependencies
# plugin.yml
softdepend: [PivotPoly, CoreTeams]
Check Versions
private boolean isCompatibleVersion(Plugin plugin, String minVersion) {
String version = plugin.getDescription().getVersion();
// Compare versions
return version.compareTo(minVersion) >= 0;
}
Error Handling
Graceful Degradation
@Override
public void onEnable() {
try {
setupPivotPoly();
} catch (Exception e) {
getLogger().warning("PivotPoly integration failed - feature disabled");
getLogger().warning(e.getMessage());
}
// Plugin still works without integration
}
Informative Logging
// ❌ BAD
getLogger().warning("Error!");
// ✅ GOOD
getLogger().warning("Failed to create door '" + doorName + "': " + e.getMessage());
getLogger().info("Door '" + doorName + "' created successfully at " + location);
Documentation
Document Your API
/**
* Creates a new animated door.
*
* @param player The player creating the door
* @param name Unique name for the door (max 32 chars)
* @param selection Selection data (pos1, pos2, pivot)
* @return true if door was created successfully
* @throws IllegalArgumentException if name is invalid
* @throws IllegalStateException if selection is incomplete
*/
public boolean createDoor(Player player, String name, Selection selection) {
// Implementation
}
Follow these practices for robust integration!