From b94c67b7c2d2c2cf3cdee20de70f3c8cdf9ee07a Mon Sep 17 00:00:00 2001 From: zrdzn Date: Tue, 26 Apr 2022 21:19:14 +0200 Subject: [PATCH] GH-9 Add enchantments to drop items --- README.md | 1 + .../lovelydrop/drop/DropListener.java | 7 ++ .../zrdzn/minecraft/lovelydrop/item/Item.java | 10 ++- .../minecraft/lovelydrop/item/ItemParser.java | 25 ++++++- .../minecraft/lovelydrop/menu/MenuItem.java | 10 ++- .../lovelydrop/menu/MenuItemParser.java | 5 +- .../lovelydrop/menu/MenuService.java | 12 +++- plugin/src/main/resources/config.yml | 71 ++++++++++++++++--- 8 files changed, 123 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 6f47248..2617524 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ LovelyDrop is a minecraft plugin that adds a drop system to a server. You can cu - Set **chance** of the drop - Set **experience** that should be given to the player - Set **amount** of possible items of the drop + - Set **enchantments** of the drop ## Configuration The whole configuration of the plugin is explained in this [default configuration file](https://github.com/zrdzn/LovelyDrop/blob/master/plugin/src/main/resources/config.yml). diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropListener.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropListener.java index 1c925f7..1537a7a 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropListener.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/drop/DropListener.java @@ -23,6 +23,7 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -103,6 +104,12 @@ public void onSourceBreak(BlockBreakEvent event) { droppedItemMeta.setLore(item.getLore()); droppedItem.setItemMeta(droppedItemMeta); + // Add additional enchantments. + Map enchantments = item.getEnchantments(); + if (enchantments.size() > 0) { + droppedItem.addUnsafeEnchantments(enchantments); + } + World world = player.getWorld(); Location location = block.getLocation(); diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/item/Item.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/item/Item.java index 3ad190f..d1eda70 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/item/Item.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/item/Item.java @@ -16,8 +16,10 @@ package io.github.zrdzn.minecraft.lovelydrop.item; import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; import java.util.List; +import java.util.Map; import java.util.Map.Entry; public class Item { @@ -30,9 +32,10 @@ public class Item { private final int experience; private final String displayName; private final List lore; + private final Map enchantments; public Item(String id, Material type, Material source, double chance, Entry amount, int experience, - String displayName, List lore) { + String displayName, List lore, Map enchantments) { this.id = id; this.type = type; this.source = source; @@ -41,6 +44,7 @@ public Item(String id, Material type, Material source, double chance, Entry getLore() { return this.lore; } + public Map getEnchantments() { + return this.enchantments; + } + } diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/item/ItemParser.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/item/ItemParser.java index 115cb15..6c2c038 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/item/ItemParser.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/item/ItemParser.java @@ -17,12 +17,16 @@ import io.github.zrdzn.minecraft.lovelydrop.LovelyDropPlugin; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.enchantments.Enchantment; import java.util.AbstractMap; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.logging.Logger; @@ -106,7 +110,26 @@ public Item parse(ConfigurationSection section) throws InvalidConfigurationExcep List lore = LovelyDropPlugin.color(section.getStringList("meta.lore")); - return new Item(section.getName(), type, sourceType, chance, amounts, experience, displayName, lore); + Map enchantments = new HashMap<>(); + for (String enchantmentRaw : section.getStringList("meta.enchantments")) { + String[] enchantmentRawArray = enchantmentRaw.split(":"); + + Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(enchantmentRawArray[0])); + if (enchantment == null) { + throw new InvalidConfigurationException("Key in 'enchantments' is an invalid enchantment."); + } + + int level; + try { + level = Integer.parseUnsignedInt(enchantmentRawArray[1]); + } catch (NumberFormatException exception) { + throw new InvalidConfigurationException("Key in 'enchantments' is an invalid enchantment level."); + } + + enchantments.put(enchantment, level); + } + + return new Item(section.getName(), type, sourceType, chance, amounts, experience, displayName, lore, enchantments); } public List parseMany(ConfigurationSection section) throws InvalidConfigurationException { diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuItem.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuItem.java index a6c9049..6a25586 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuItem.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuItem.java @@ -28,15 +28,17 @@ public class MenuItem { private final Material type; private final String displayName; private final List lore; + private final boolean showEnchantments; private final Map action; private final Entry slot; private final Item dropItem; - public MenuItem(Material type, String displayName, List lore, Map action, - Entry slot, Item dropItem) { + public MenuItem(Material type, String displayName, List lore, boolean showEnchantments, + Map action, Entry slot, Item dropItem) { this.type = type; this.displayName = displayName; this.lore = lore; + this.showEnchantments = showEnchantments; this.action = action; this.slot = slot; this.dropItem = dropItem; @@ -54,6 +56,10 @@ public List getLore() { return this.lore; } + public boolean isShowEnchantments() { + return this.showEnchantments; + } + public Map getAction() { return this.action; } diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuItemParser.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuItemParser.java index 67a2a69..a326b32 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuItemParser.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuItemParser.java @@ -59,6 +59,8 @@ public MenuItem parse(ConfigurationSection section) throws InvalidConfigurationE List itemLore = LovelyDropPlugin.color(section.getStringList("meta.lore")); + boolean showEnchantments = section.getBoolean("meta.show-enchantments", true); + ConfigurationSection actionSection = section.getConfigurationSection("click-action"); if (actionSection == null) { throw new InvalidConfigurationException("Section 'click-action' does not exist."); @@ -97,7 +99,8 @@ public MenuItem parse(ConfigurationSection section) throws InvalidConfigurationE Entry slot = new AbstractMap.SimpleEntry<>(slotRow, slotColumn); - return new MenuItem(type, itemName, itemLore, actions, slot, this.itemCache.getDrop(section.getName()).orElse(null)); + return new MenuItem(type, itemName, itemLore, showEnchantments, actions, slot, + this.itemCache.getDrop(section.getName()).orElse(null)); } public List parseMany(ConfigurationSection section) throws InvalidConfigurationException { diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuService.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuService.java index 0d29c9f..b84e6e4 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuService.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuService.java @@ -123,7 +123,7 @@ public boolean open(Player player) { Entry dropSwitch = this.menu.getDropSwitch(); Entry inventoryDropSwitch = this.menu.getInventoryDropSwitch(); - GuiItem menuItem = ItemBuilder.from(item.getType()) + ItemBuilder menuItemBuilder = ItemBuilder.from(item.getType()) .setName(item.getDisplayName()) .setLore(lore.stream() .map(line -> line @@ -131,8 +131,14 @@ public boolean open(Player player) { .replace("{SWITCH_INVENTORY}", user.hasSwitchedInventoryDrop(dropItem.getId()) ? inventoryDropSwitch.getKey() : inventoryDropSwitch.getValue())) - .collect(Collectors.toList())) - .asGuiItem(); + .collect(Collectors.toList())); + + // Add enchantments if they should be shown. + if (item.isShowEnchantments()) { + dropItem.getEnchantments().forEach((enchantment, level) -> menuItemBuilder.enchant(enchantment, level, true)); + } + + GuiItem menuItem = menuItemBuilder.asGuiItem(); // Perform specific actions when player clicks the item. menuItem.setAction(event -> { diff --git a/plugin/src/main/resources/config.yml b/plugin/src/main/resources/config.yml index 22db28d..2e91bfd 100644 --- a/plugin/src/main/resources/config.yml +++ b/plugin/src/main/resources/config.yml @@ -76,6 +76,8 @@ menu: # displayname: "&c&lClose" - display name of the menu item; # lore: - lore of the menu item; # - "&7Click to close the menu." + # show-enchantments: true - whether to show the dropped item enchantments (only applicable if + # it is assigned to a drop item) # # Available placeholders: # {CHANCE} - a chance of the item to drop; @@ -99,6 +101,31 @@ menu: displayname: "&c&lClose" lore: - "&7Click to close the menu." + diamond-sword: + type: diamond_sword + + click-action: + LEFT: SWITCH_DROP + RIGHT: SWITCH_DROP_TO_INVENTORY + + slot: + row: 1 + column: 5 + + meta: + displayname: "&a&lEpic Sword" + lore: + - " &6Chance: &e{CHANCE}%" + - " &6Amount: {AMOUNT}" + - " &6Experience: &e{EXPERIENCE}" + - " &6Status: {SWITCH}" + - " &6To inventory: {SWITCH_INVENTORY}" + - "" + - "&8-------------------------------------" + - " &aLeft click to switch the drop." + - " &eRight click to switch the inventory drop." + - "&8-------------------------------------" + show-enchantments: true gold: type: gold_ingot @@ -123,6 +150,7 @@ menu: - " &aLeft click to switch the drop." - " &eRight click to switch the inventory drop." - "&8-------------------------------------" + show-enchantments: true diamond: type: diamond @@ -147,6 +175,7 @@ menu: - " &aLeft click to switch the drop." - " &eRight click to switch the inventory drop." - "&8-------------------------------------" + show-enchantments: true tnt: type: tnt @@ -171,6 +200,7 @@ menu: - " &aLeft click to switch the drop." - " &eRight click to switch the inventory drop." - "&8-------------------------------------" + show-enchantments: true emerald: type: emerald @@ -195,6 +225,7 @@ menu: - " &aLeft click to switch the drop." - " &eRight click to switch the inventory drop." - "&8-------------------------------------" + show-enchantments: true obsidian: type: obsidian @@ -219,26 +250,46 @@ menu: - " &aLeft click to switch the drop." - " &eRight click to switch the inventory drop." - "&8-------------------------------------" + show-enchantments: true # All drops that will be available on the server. # Structure explanation below: # -# gold: - id of the drop. IMPORTANT: it is used in menu (id in menu.items must equal to this id -# if you want to use SWITCH_DROP action there); -# type: gold_ingot - type of the drop; -# source: stone - source block type from which 'type' should be dropped; -# chance: 60 - percentage chance of the drop; -# amount: 1-5 - minimum and maximum amount of the drop (if '-' is not found, it will use only first number); -# experience: 20 - experience given to the player when he has successfull drop; +# gold: - id of the drop. IMPORTANT: it is used in menu (id in menu.items must equal to this id +# if you want to use SWITCH_DROP action there); +# type: diamond_sword - type of the drop; +# source: stone - source block type from which 'type' should be dropped; +# chance: 20 - percentage chance of the drop; +# amount: 1-2 - minimum and maximum amount of the drop (if '-' is not found, it will use only first number); +# experience: 175 - experience given to the player when he has successful drop; # meta: -# displayname: "&6Gold" - display name of the drop; -# lore: - lore of the drop -# - "&7Dropped gold" +# displayname: "&a&lEpic Sword" - display name of the drop; +# lore: - lore of the drop; +# - "&6Legendary diamond sword." +# enchantments: - enchantments of the drop, word before ':' is enchantment name, and after it is the level, +# they MUST BE lowercase +# - "sharpness:5" +# - "unbreaking:3" +# - "looting:3" # # NOTE: 'meta' section is optional, you can remove it to have original/vanilla items. drops: + diamond-sword: + type: diamond_sword + source: stone + chance: 20 + amount: 1-2 + experience: 175 + meta: + displayname: "&a&lEpic Sword" + lore: + - "&6Legendary diamond sword." + enchantments: + - "sharpness:5" + - "unbreaking:3" + - "looting:3" gold: type: gold_ingot source: stone