-
Notifications
You must be signed in to change notification settings - Fork 0
10 advanced usage
This section covers technical tips and advanced practices for maximizing performance and flexibility with KLibrary.
Bukkit requires most API calls (e.g., setting blocks, altering inventories, changing health) to happen on the main thread. However, database fetches or heavy requirement calculations should happen asynchronously.
If you write a requirement that queries a database, do not evaluate it directly in an event listener.
- Fetch data asynchronously using
Bukkit.getScheduler().runTaskAsynchronously. - Cache the result.
- Validate the cached result via the
RequirementResult.evaluate()sync loop.
While InventoryProvider#update() ticks on the main thread to allow safe GUI item mutation, you can perform logic fetches on another thread and apply the state update via thread-safe variables before the next tick.
KLibrary uses ConfigNode heavily. If you use a format completely foreign to Bukkit's YamlConfiguration (e.g., TOML, JSON, or HOCON via Spongepowered Configurate), you only need to write a new implementation of the ConfigNode interface wrapping your format.
All deserialization code across Actions, Requirements, and Inventories will instantly support your new file format.
-
Cache Action Parsings: Compiling MiniMessage strings into Components is CPU heavy. If your action sends a static text string, parse the
Componentonce inside the Action's constructor, rather than parsing it every timeexecute(player)is called. -
Registry Singleton:
ActionFactoryandRequirementFactoryshould be singletons per plugin instance. Do not instantiate them repeatedly. -
Tick Limits: Be mindful of your
InventoryProvider#update()execution times. If you have 50 players opening dynamic inventories, heavy logic insideupdate()will cause server lag. Limit complex redraw operations.