Want a better Minecraft server? Read about SpigotMC here!
Separate names with a comma.
Discussion in 'Spigot Plugin Development' started by Lovis, Aug 4, 2018.
How do I save an enchantment in the config and then load?
If you are talking about saving an ItemStack which has an enchantment then you can save its name and level and then take it back from the config and implement it on the ItemStack with ItemMeta.
Edit: Other option is saving the whole object and get it with getConfig()#getItemStack()
Like the post above said, the only two important details to an Enchantment is the type of enchant and the level (all the types are listed on the linked page). If you want to store just an enchantment separate from any item, then just store the enchantment object's getName(), and the level. The level needs to come from elsewhere as the Enchantment object itself doesn't store it. Either use a fixed value or get it from an enchanted item. You can get an enchant directly from an ItemStack (as apposed to the above post, it seems ItemStack exposes the enchantment methods directly). To get the level from an enchanted item stack, you can use getEnchantmentLevel(Enchant)
And also as mentioned above, if you want to save an entire ItemStack, including it's enchantments and all it's associated data (lore, name, etc), then you can simply set a path in the config to the ItemStack.
Then to convert it back into an ItemStack later, simply use getConfig().getItemStack("item.path"); You can provide an ItemStack as a second argument here to give it a default if it doesn't exist in the config.
As an additional tip, you can set an ItemStack directly in the config because it extends or implements ConfigurationSerializable -- and you can do the same with any of the other classes listed on that page's 'All Known Subinterfaces' or 'All Known Implementing Classes'. I'm unsure though if the config object has a 'get' method for each of those classes, but at the very least, you should be able to set them.