-
Notifications
You must be signed in to change notification settings - Fork 0
Creating Properties
Sky edited this page Feb 9, 2022
·
3 revisions
Property defines values that an item can have.
They could edit the item in different ways:
- Item Meta (name, lore, flags)
- Item NBT (custom model data, custom NBT tags)
- etc ...
In addition, a property can be either Simple (one value, like the name) or Multiple (two or more values, like lores).
All of this information combined make multiples class useful, even if you can implement your own custom property using ItemProperty
:
Format: 'class name' ('example property class')
Multiple MultipleItemProperty
-
MultipleMetaProperty
(lore) -
MultipleNBTProperty
(commands)
Simple ItemProperty
-
SimpleMetaProperty
(name) -
SimpleNBTProperty
(can't enchant / can't craft) -
SimpleStateProperty
(true / false property, like ItemCreatorTag) -
SimpleMetaStateProperty
(true / false property, like unbreakable) -
SimpleNBTStateProperty
(true / false property)
Once your class is ready to use, don't forget to register your property inside ItemCreator. Use the following method to do so:
api.registerProperty(new YourPropertyClass());
Let's make a concrete example about a simple property:
public class RarityProperty extends SimpleMetaProperty<String> {
@Override
public String getId() {
// The unique ID used in NBT, lang file and more
return "rarity";
}
@Override
public boolean allowClearing() {
// Either this property can cleared, using drop key (aka if the value can be null)
return true;
}
@Override
public String getDefaultValue() {
// The default value of this property. if 'allowClearing()' is false, this CANNOT return null
return null;
}
@Override
public void onEditorClick(InventoryClickEvent e, CustomItem item) {
// Executed when the admin click on the property in the item GUI
final Player player = (Player) e.getWhoClicked();
player.closeInventory();
player.sendMessage(LangLoader.get().format("messages.rarity"));
// Simle utils class by Sky to catch player's message
new ChatWaiter(player, ev -> {
final String rawValue = ev.getMessage();
// Here we save the changes:
// First, we change the property of the provided item instance:
item.setPropertyValue(this, convert(rawValue, player));
// Then we save the value in the configuration file of the item
save(item, rawValue, player);
player.sendMessage(LangLoader.get().format("messages.success"));
new EditorGUI(item, true, player).open(player);
}, true, true);
}
@Override
public XMaterial getMaterial() {
// The material shown in the properties list
return XMaterial.ENDER_EYE;
}
@Override
public @Nullable String convert(String input, Player player) {
// Convert a raw value of string (user input or from config) to the actual value we want.
return Utils.colored(input);
}
@Override
public ItemMeta convert(ItemMeta original, String value) {
// Apply changes of this property on the provided value, here the item's meta
final List<String> lores =
original.getLore() == null ? new ArrayList<>() : original.getLore();
if (!lores.isEmpty() && !lores.get(lores.size() - 1).equals(""))
lores.add("");
lores.add(Utils.colored(value));
original.setLore(lores);
return original;
}
}