diff --git a/README.md b/README.md index 68f3d7a..89f0ca9 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,11 @@ LovelyDrop is a minecraft plugin that adds a drop system to a server. You can cu - **NONE** - item will not do any action - **CLOSE_MENU** - menu will be closed - **SWITCH_DROP** - drop assigned to this menu item will be turned on/off + - Set click types when to perform the specified action + - **ALL** - action will be performed on any type of the click + - **LEFT** - action will be performed on left click + - **RIGHT** - action will be performed on right click + - Whole list of the click types is [here](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/inventory/ClickType.html)! - The configurable drop section - Adding as many items as you want - Set **display name and lore** of the dropped item (optional) diff --git a/build.gradle b/build.gradle index 6dae82c..3a45af5 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { allprojects { group "io.github.zrdzn.minecraft" - version "1.1.1" + version "1.2.0" } subprojects { 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 85ef42c..a6c9049 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 @@ -17,8 +17,10 @@ import io.github.zrdzn.minecraft.lovelydrop.item.Item; import org.bukkit.Material; +import org.bukkit.event.inventory.ClickType; import java.util.List; +import java.util.Map; import java.util.Map.Entry; public class MenuItem { @@ -26,11 +28,11 @@ public class MenuItem { private final Material type; private final String displayName; private final List lore; - private final MenuAction action; + private final Map action; private final Entry slot; private final Item dropItem; - public MenuItem(Material type, String displayName, List lore, MenuAction action, + public MenuItem(Material type, String displayName, List lore, Map action, Entry slot, Item dropItem) { this.type = type; this.displayName = displayName; @@ -52,7 +54,7 @@ public List getLore() { return this.lore; } - public MenuAction getAction() { + 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 71791d3..67a2a69 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 @@ -20,10 +20,13 @@ import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.event.inventory.ClickType; 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; @@ -56,7 +59,34 @@ public MenuItem parse(ConfigurationSection section) throws InvalidConfigurationE List itemLore = LovelyDropPlugin.color(section.getStringList("meta.lore")); - MenuAction action = MenuAction.valueOf(section.getString("action")); + ConfigurationSection actionSection = section.getConfigurationSection("click-action"); + if (actionSection == null) { + throw new InvalidConfigurationException("Section 'click-action' does not exist."); + } + + Map actions = new HashMap<>(); + + // Parsing action section. + for (String clickType : actionSection.getKeys(false)) { + String actionRaw = actionSection.getString(clickType); + + MenuAction action; + try { + action = MenuAction.valueOf(actionRaw); + } catch (IllegalArgumentException exception) { + action = MenuAction.NONE; + } + + try { + actions.put(ClickType.valueOf(clickType), action); + } catch (IllegalArgumentException exception) { + actions.put(ClickType.UNKNOWN, action); + } + } + + if (actions.size() == 0) { + throw new InvalidConfigurationException("Action map is empty, check your configuration in action section."); + } int slotRow = section.getInt("slot.row"); int slotColumn = section.getInt("slot.column"); @@ -67,7 +97,7 @@ public MenuItem parse(ConfigurationSection section) throws InvalidConfigurationE Entry slot = new AbstractMap.SimpleEntry<>(slotRow, slotColumn); - return new MenuItem(type, itemName, itemLore, action, slot, this.itemCache.getDrop(section.getName()).orElse(null)); + return new MenuItem(type, itemName, itemLore, 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 76230ce..c8d0f40 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 @@ -23,10 +23,12 @@ import io.github.zrdzn.minecraft.lovelydrop.user.User; import io.github.zrdzn.minecraft.lovelydrop.user.UserCache; import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.Optional; import java.util.logging.Logger; @@ -64,6 +66,9 @@ public boolean open(Player player) { int row = slot.getKey(); int column = slot.getValue(); + // Action map cannot be empty because of the exception thrown in the parsing method. + Map actions = item.getAction(); + // Set item that does not have drop item assigned to it. Item dropItem = item.getDropItem(); if (dropItem == null) { @@ -71,7 +76,17 @@ public boolean open(Player player) { .setName(item.getDisplayName()) .setLore(item.getLore()) .asGuiItem(event -> { - if (item.getAction() == MenuAction.CLOSE_MENU) { + if (!actions.containsKey(ClickType.UNKNOWN)) { + ClickType click = event.getClick(); + + if (actions.containsKey(click) && actions.get(click) == MenuAction.CLOSE_MENU) { + menu.close(player, true); + } + + return; + } + + if (actions.get(ClickType.UNKNOWN) == MenuAction.CLOSE_MENU) { menu.close(player, true); } })); @@ -117,13 +132,33 @@ public boolean open(Player player) { // Perform specific actions when player clicks the item. menuItem.setAction(event -> { - if (item.getAction() == MenuAction.CLOSE_MENU) { - menu.close(player, true); - } else if (item.getAction() == MenuAction.SWITCH_DROP) { - if (user.hasDisabledDrop(dropItem)) { - user.enableDrop(dropItem); - } else { - user.disableDrop(dropItem); + if (!actions.containsKey(ClickType.UNKNOWN)) { + ClickType click = event.getClick(); + + if (actions.containsKey(click)) { + MenuAction action = actions.get(click); + if (action == MenuAction.CLOSE_MENU) { + menu.close(player, true); + return; + } else if (action == MenuAction.SWITCH_DROP) { + if (user.hasDisabledDrop(dropItem)) { + user.enableDrop(dropItem); + } else { + user.disableDrop(dropItem); + } + } + } + } else { + MenuAction action = actions.get(ClickType.UNKNOWN); + if (action == MenuAction.CLOSE_MENU) { + menu.close(player, true); + return; + } else if (action == MenuAction.SWITCH_DROP) { + if (user.hasDisabledDrop(dropItem)) { + user.enableDrop(dropItem); + } else { + user.disableDrop(dropItem); + } } } diff --git a/plugin/src/main/resources/config.yml b/plugin/src/main/resources/config.yml index a2bd5c8..03d7a0b 100644 --- a/plugin/src/main/resources/config.yml +++ b/plugin/src/main/resources/config.yml @@ -45,11 +45,18 @@ menu: # CLOSE_MENU - it will close the menu; # SWITCH_DROP - it will switch drop on/off (id of the item here must equal to the id in the 'drops' section # to make this work, otherwise it will not recognise the drop that it should switch on/off). + # + # click-action section contains key, value fields where the key is the specified field in the + # https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/inventory/ClickType.html. If you want to perform an action + # on any click, you can just put ALL as shown below. Value is the action described above. + # # Whole structure explanation: # # close: - id of the menu item; # type: barrier - type of the menu item; - # action: CLOSE_MENU - action that should be performed after clicking on the menu item; + # click-action: + # ALL: CLOSE_MENU - action that should be performed after clicking on the menu item with + # any combination; # # slot: # row: 4 - row in the menu where the menu item should be @@ -69,7 +76,9 @@ menu: items: close: type: barrier - action: CLOSE_MENU + + click-action: + ALL: CLOSE_MENU slot: row: 4 @@ -81,7 +90,10 @@ menu: - "&7Click to close the menu." gold: type: gold_ingot - action: SWITCH_DROP + + click-action: + LEFT: SWITCH_DROP + RIGHT: CLOSE_MENU slot: row: 2 @@ -98,7 +110,10 @@ menu: - "&aClick to switch the drop." diamond: type: diamond - action: SWITCH_DROP + + click-action: + LEFT: SWITCH_DROP + RIGHT: CLOSE_MENU slot: row: 2 @@ -115,7 +130,10 @@ menu: - "&aClick to switch the drop." tnt: type: tnt - action: SWITCH_DROP + + click-action: + LEFT: SWITCH_DROP + RIGHT: CLOSE_MENU slot: row: 2 @@ -132,7 +150,10 @@ menu: - "&aClick to switch the drop." emerald: type: emerald - action: SWITCH_DROP + + click-action: + LEFT: SWITCH_DROP + RIGHT: CLOSE_MENU slot: row: 2 @@ -149,7 +170,10 @@ menu: - "&aClick to switch the drop." obsidian: type: obsidian - action: SWITCH_DROP + + click-action: + LEFT: SWITCH_DROP + RIGHT: CLOSE_MENU slot: row: 2 diff --git a/plugin/src/main/resources/plugin.yml b/plugin/src/main/resources/plugin.yml index c0735b4..2c9187d 100644 --- a/plugin/src/main/resources/plugin.yml +++ b/plugin/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: LovelyDrop -version: 1.1.1 +version: 1.2.0 main: io.github.zrdzn.minecraft.lovelydrop.LovelyDropPlugin api-version: 1.13 author: zrdzn