Skip to content

Commit

Permalink
Add enchantments using ItemMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed May 10, 2023
1 parent 7a1b7c7 commit 7f87f0b
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions api/src/main/java/xyz/srnyx/annoyingapi/file/AnnoyingFile.java
Expand Up @@ -261,18 +261,7 @@ public ItemStack getItemStack(@NotNull String path, @Nullable ItemStack def) {
// Material, amount, and durability
final ItemStack item = new ItemStack(material, section.getInt("amount", 1), (short) section.getInt("damage", 0));

// Enchantments
final ConfigurationSection enchantmentsSection = section.getConfigurationSection("enchantments");
if (enchantmentsSection != null) for (final String enchantmentKey : enchantmentsSection.getKeys(false)) {
final Enchantment enchantment = Enchantment.getByName(enchantmentKey);
if (enchantment == null) {
log(Level.WARNING, path, "&cInvalid enchantment: &4" + enchantmentKey);
continue;
}
item.addUnsafeEnchantment(enchantment, enchantmentsSection.getInt(enchantmentKey));
}

// Name, lore, unbreakable, flags, attribute modifiers, and custom model data
// Name, lore, unbreakable, enchantments, flags, attribute modifiers, and custom model data
final ItemMeta meta = item.getItemMeta();
if (meta != null) {
// Name
Expand All @@ -287,6 +276,17 @@ public ItemStack getItemStack(@NotNull String path, @Nullable ItemStack def) {
// Unbreakable
meta.setUnbreakable(section.getBoolean("unbreakable", false));

// Enchantments
final ConfigurationSection enchantmentsSection = section.getConfigurationSection("enchantments");
if (enchantmentsSection != null) for (final String enchantmentKey : enchantmentsSection.getKeys(false)) {
final Enchantment enchantment = Enchantment.getByName(enchantmentKey);
if (enchantment == null) {
log(Level.WARNING, path, "&cInvalid enchantment: &4" + enchantmentKey);
continue;
}
meta.addEnchant(enchantment, enchantmentsSection.getInt(enchantmentKey), true);
}

// Flags
section.getStringList("flags").stream()
.map(string -> {
Expand Down

0 comments on commit 7f87f0b

Please sign in to comment.