Skip to content

Commit

Permalink
GH-7 Add ClickType field
Browse files Browse the repository at this point in the history
  • Loading branch information
zrdzn committed Apr 24, 2022
1 parent 7794147 commit 5598a28
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 22 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {

allprojects {
group "io.github.zrdzn.minecraft"
version "1.1.1"
version "1.2.0"
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@

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 {

private final Material type;
private final String displayName;
private final List<String> lore;
private final MenuAction action;
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, MenuAction action,
public MenuItem(Material type, String displayName, List<String> lore, Map<ClickType, MenuAction> action,
Entry<Integer, Integer> slot, Item dropItem) {
this.type = type;
this.displayName = displayName;
Expand All @@ -52,7 +54,7 @@ public List<String> getLore() {
return this.lore;
}

public MenuAction getAction() {
public Map<ClickType, MenuAction> getAction() {
return this.action;
}

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

Expand Down Expand Up @@ -56,7 +59,34 @@ public MenuItem parse(ConfigurationSection section) throws InvalidConfigurationE

List<String> 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<ClickType, MenuAction> 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");
Expand All @@ -67,7 +97,7 @@ public MenuItem parse(ConfigurationSection section) throws InvalidConfigurationE

Entry<Integer, Integer> 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<MenuItem> parseMany(ConfigurationSection section) throws InvalidConfigurationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,14 +66,27 @@ 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<ClickType, MenuAction> actions = item.getAction();

// Set item that does not have drop item assigned to it.
Item dropItem = item.getDropItem();
if (dropItem == null) {
menu.setItem(row, column, ItemBuilder.from(item.getType())
.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);
}
}));
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
38 changes: 31 additions & 7 deletions plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -69,7 +76,9 @@ menu:
items:
close:
type: barrier
action: CLOSE_MENU

click-action:
ALL: CLOSE_MENU

slot:
row: 4
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 5598a28

Please sign in to comment.