Create your own Mechanic
also known as "How to superpower Oraxen?"
How does the Mechanic system work?
What's a mechanic?
A mechanic is a custom item property. There can be different variations of the same property on different items. For example, the durability property allows you to define a custom durability for an item, but not all items that have a durability mechanic have the same durability.
The durability mechanic works by storing the item's durability in its metadatas and uses the vanilla durability bar to display its custom durability and not its vanilla durability
So if a Mechanic is different for every Item, should I rewrite a different Durability everytime?
No, it would be far too long. Instead, Oraxen offers a MechanicFactory system. Basically you will have to create a Mechanics class that is configurable, and a MechanicsFactory class that will configure all the different versions of your Mechanic class. It will also manage the common code of all these versions.
Okay, but I want to modify the items which implement my Mechanic to store data in it, is it possible?
Of course, for this purpose Oraxen allows you to associate a list of ItemModifier to each mechanic. An ItemModifier is Function<ItemBuilder, ItemBuilder>
which is basically a small piece of code that contains changes to be made to an item when it is generated by the server via its configuration. For example for durability mechanics I use an itemModifier that stores the durability chosen by the user in the item's metadatas.
Let's create our first mechanic
For this tutorial I will take as an example the durability mechanic (because it is very simple to understand) but you can follow this tutorial to create the one you want.
First step: create our mechanic class
Start by creating a class inheriting from Mechanic, if you use intelliJ you should get something like this:
The Mechanic constructor takes three arguments:
I want each variation of my mechanics to have a different durability, so I will read the configuration of the mechanics and store the value of the field value.
How the Mechanic configuration section will look like:
So now we have a DurabilityMechanic class that is able to adapt to any item and that will call our DurabilityModifier class to tell to Oraxen which modifications to do before creating them (here we just add a data in the item which contains the wanted new durability).
Second step: create our mechanic factory class
As before, use your ide features to automatically create a class that extends MechanicFactory:
We rewrite the parse method to create a new Mechanic (via the DurabilityMechanic class created previously). We also want to tell to Oraxen that this mechanic is successfully implemented and can be loaded using addToImplemented
method. So our class now looks like that:
Third step: add our features (events)
In my case I need to use only one event to play with durability and I will create a DurabilityMechanicsManager class that implements Listener to have a clean and tidy code but I could have done it directly in DurabilityMechanicFactory. I tell to Bukkit which class manages the events when the factory is built:
In order to calculate the durability to display on the item according to the real durability managed by the plugin I use some simple maths:
So this is my DurabilityMechanicsManager class:
Last step: register our mechanic
To finish we need to register our MechanicFactory and reload the items to apply the new mechanic to them.
It is recommended to register it in an EventListener for OraxenNativeMechanicsRegisteredEvent
, due to /oraxen reload all
clearing this registry.
To do this we need to add these lines in the onEnable method of our plugin:
Conclusion
To properly create a new mechanic it is recommended to separate its code into three parts:
A factory which extends MechanicFactory
A mechanic which extends Mechanic
Your own features in a <YourMechanicName>MechanicsManager class (optional too)
While it is possible to modify items using your mechanic thanks to an ItemModifier:
You can also modify the texture pack in a similar way:
Finally register your mechanic!
To summarize the tutorial, here is the complete source code of the durability mechanics.
Last updated