Skip to content

Commit

Permalink
GH-9 Add enchantments to drop items
Browse files Browse the repository at this point in the history
  • Loading branch information
zrdzn committed Apr 26, 2022
1 parent 333003b commit b94c67b
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,6 +104,12 @@ public void onSourceBreak(BlockBreakEvent event) {
droppedItemMeta.setLore(item.getLore());
droppedItem.setItemMeta(droppedItemMeta);

// Add additional enchantments.
Map<Enchantment, Integer> enchantments = item.getEnchantments();
if (enchantments.size() > 0) {
droppedItem.addUnsafeEnchantments(enchantments);
}

World world = player.getWorld();

Location location = block.getLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,9 +32,10 @@ public class Item {
private final int experience;
private final String displayName;
private final List<String> lore;
private final Map<Enchantment, Integer> enchantments;

public Item(String id, Material type, Material source, double chance, Entry<Integer, Integer> amount, int experience,
String displayName, List<String> lore) {
String displayName, List<String> lore, Map<Enchantment, Integer> enchantments) {
this.id = id;
this.type = type;
this.source = source;
Expand All @@ -41,6 +44,7 @@ public Item(String id, Material type, Material source, double chance, Entry<Inte
this.experience = experience;
this.displayName = displayName;
this.lore = lore;
this.enchantments = enchantments;
}

public String getId() {
Expand Down Expand Up @@ -75,4 +79,8 @@ public List<String> getLore() {
return this.lore;
}

public Map<Enchantment, Integer> getEnchantments() {
return this.enchantments;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -106,7 +110,26 @@ public Item parse(ConfigurationSection section) throws InvalidConfigurationExcep

List<String> lore = LovelyDropPlugin.color(section.getStringList("meta.lore"));

return new Item(section.getName(), type, sourceType, chance, amounts, experience, displayName, lore);
Map<Enchantment, Integer> 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<Item> parseMany(ConfigurationSection section) throws InvalidConfigurationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ public class MenuItem {
private final Material type;
private final String displayName;
private final List<String> lore;
private final boolean showEnchantments;
private final Map<ClickType, MenuAction> action;
private final Entry<Integer, Integer> slot;
private final Item dropItem;

public MenuItem(Material type, String displayName, List<String> lore, Map<ClickType, MenuAction> action,
Entry<Integer, Integer> slot, Item dropItem) {
public MenuItem(Material type, String displayName, List<String> lore, boolean showEnchantments,
Map<ClickType, MenuAction> action, Entry<Integer, Integer> slot, Item dropItem) {
this.type = type;
this.displayName = displayName;
this.lore = lore;
this.showEnchantments = showEnchantments;
this.action = action;
this.slot = slot;
this.dropItem = dropItem;
Expand All @@ -54,6 +56,10 @@ public List<String> getLore() {
return this.lore;
}

public boolean isShowEnchantments() {
return this.showEnchantments;
}

public Map<ClickType, MenuAction> getAction() {
return this.action;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public MenuItem parse(ConfigurationSection section) throws InvalidConfigurationE

List<String> 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.");
Expand Down Expand Up @@ -97,7 +99,8 @@ public MenuItem parse(ConfigurationSection section) throws InvalidConfigurationE

Entry<Integer, Integer> 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<MenuItem> parseMany(ConfigurationSection section) throws InvalidConfigurationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,22 @@ public boolean open(Player player) {
Entry<String, String> dropSwitch = this.menu.getDropSwitch();
Entry<String, String> 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
.replace("{SWITCH}", !user.hasDisabledDrop(dropItem) ? dropSwitch.getKey() : dropSwitch.getValue())
.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 -> {
Expand Down
71 changes: 61 additions & 10 deletions plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit b94c67b

Please sign in to comment.