4.3 KiB
Content Creation Guide
This project provides a framework for adding new items and features (components) using the HcuItems plugin.
Event handling is uniformly managed by the EventListener, which delegates to appropriate classes based on information in the item's PersistentDataContainer (PDC).
Architecture Overview
All events (right-click, damage, movement, etc.) are received by the EventListener.
The EventListener delegates processing through the following steps:
- Retrieves the item involved in the event.
- Checks the item's PDC.
- If an
AbstractItemID (hcu_items:id) exists → Retrieves the item definition fromItemRegistryand executes. - If a component key (
hcu_items:component_id) exists → Retrieves the component definition fromComponentRegistryand executes.
- If an
1. Creating an AbstractItem (Custom Item)
Use this when creating an item with completely unique behavior.
Steps
- Create a new class that inherits from the
AbstractItemclass. - Specify a unique ID in the constructor.
- Implement the
buildItemmethod to define the item's appearance (Material, Name, Lore, etc.). - Override necessary event handlers (
onInteract,onPlayerMove, etc.). - Register by calling
ItemRegistry.register(YourItem())inApp.kt'sonEnable.
class MyCustomItem : AbstractItem("my_custom_item") {
override fun buildItem(tier: Tier): ItemStack {
return ItemStack(Material.DIAMOND_SWORD).apply {
// Meta configuration
}
}
override fun onInteract(event: PlayerInteractEvent) {
event.player.sendMessage("Used custom item!")
}
}
2. Creating a CustomComponent (Adding Features to Existing Items)
Use this when adding common features (e.g., glider, passive effects) to existing items or items that meet specific conditions.
Steps
- Create a class that inherits from
AbstractComponentand implementsCustomComponent(orEquippableComponent). - Specify a unique component ID in the constructor.
- Implement the
applymethod with logic to apply the component to the item (key registration to PDC is automatic, but describe if additional metadata is needed). - Override necessary event handlers.
- Register by calling
ComponentRegistry.register(YourComponent(this))inApp.kt'sonEnable.
class MyComponent(plugin: App) : AbstractComponent(plugin, "my_component") {
override fun onInteract(event: PlayerInteractEvent) {
event.player.sendMessage("Component interaction!")
}
}
Event Mechanism
AbstractComponent generates a NamespacedKey during initialization and writes this key to the item's PDC during apply.
When the EventListener confirms this key exists in the item's PDC, it calls your component's event handler.
Notes
AbstractItemandCustomComponentcan coexist. A single item can be anAbstractItemand have multipleCustomComponents.- Within event handlers, appropriately control
event.isCancelledas needed.
3. Configuration Management
The project uses a centralized Config object to manage configuration values from config.yml. This allows for easy adjustment of values (like cooldowns) without code changes.
Accessing Configuration
To access configuration values, simply access the public properties of the Config object:
import net.hareworks.hcu.items.config.Config
// Example usage
val cooldown = Cooldown(Config.blinkCooldown)
Adding New Config Values
- Add Property to Config Object: Add a new property to
net.hareworks.hcu.items.config.Config.kt. - Update Load Method: Read the value from
plugin.configin theloadmethod. - Update Defaults: Add the default value in the
saveDefaultsmethod.
object Config {
// 1. Add Property
var newFeatureEnabled: Boolean = true
fun load(plugin: JavaPlugin) {
// ... existing code ...
// 2. Load Value
newFeatureEnabled = config.getBoolean("components.new_feature.enabled", true)
// ... existing code ...
}
private fun saveDefaults(plugin: JavaPlugin, config: FileConfiguration) {
// ... existing code ...
// 3. Set Default
config.addDefault("components.new_feature.enabled", true)
// ... existing code ...
}
}