diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/api/ClickHandler.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/api/ClickHandler.java index 03398bb..53472de 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/api/ClickHandler.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/api/ClickHandler.java @@ -4,6 +4,11 @@ import org.bukkit.entity.Player; public interface ClickHandler { - public void onClick(Player player); + /** + * + * @param player - the player that clicked on the icon. + * @return true if the menu should be closed, false otherwise. + */ + public boolean onClick(Player player); } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/api/Icon.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/api/Icon.java index cf8c2f2..2463e8d 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/api/Icon.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/api/Icon.java @@ -32,8 +32,8 @@ public class Icon { private ClickHandler clickHandler; public Icon() { - closeOnClick = true; enchantments = new HashMap(); + closeOnClick = true; } public void setMaterial(Material material) { @@ -204,10 +204,11 @@ public class Icon { return itemStack; } - public void onClick(Player whoClicked) { + public boolean onClick(Player whoClicked) { if (clickHandler != null) { - clickHandler.onClick(whoClicked); + return clickHandler.onClick(whoClicked); } + return closeOnClick; } } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/CommandsClickHandler.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/CommandsClickHandler.java index c572541..98b4d43 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/CommandsClickHandler.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/CommandsClickHandler.java @@ -6,22 +6,36 @@ import org.bukkit.entity.Player; import com.gmail.filoghost.chestcommands.api.ClickHandler; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.OpenIconCommand; public class CommandsClickHandler implements ClickHandler { private List commands; + private boolean closeOnClick; - public CommandsClickHandler(List commands) { + public CommandsClickHandler(List commands, boolean closeOnClick) { this.commands = commands; + this.closeOnClick = closeOnClick; + + if (commands != null && commands.size() > 0) { + for (IconCommand command : commands) { + if (command instanceof OpenIconCommand) { + // Fix GUI closing if KEEP-OPEN is not set, and a command should open another GUI. + this.closeOnClick = false; + } + } + } } @Override - public void onClick(Player player) { + public boolean onClick(Player player) { if (commands != null && commands.size() > 0) { for (IconCommand command : commands) { command.execute(player); } - } + } + + return closeOnClick; } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/ExtendedIcon.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/ExtendedIcon.java index 7a5736a..57e24c5 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/ExtendedIcon.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/ExtendedIcon.java @@ -55,7 +55,7 @@ public class ExtendedIcon extends Icon { @SuppressWarnings("deprecation") @Override - public void onClick(Player player) { + public boolean onClick(Player player) { // Check all the requirements. @@ -65,18 +65,18 @@ public class ExtendedIcon extends Icon { } else { player.sendMessage(ChatColor.RED + "You don't have permission."); } - return; + return isCloseOnClick(); } if (price > 0) { if (!EconomyBridge.hasValidEconomy()) { player.sendMessage(ChatColor.RED + "This command has a price, but Vault with a compatible economy plugin was not found. For security, the command has been blocked. Please inform the staff."); - return; + return isCloseOnClick(); } if (!player.hasPermission(Permissions.BYPASS_ECONOMY) && !EconomyBridge.hasMoney(player, price)) { player.sendMessage(ChestCommands.getLang().no_money.replace("{money}", EconomyBridge.formatMoney(price))); - return; + return isCloseOnClick(); } } @@ -89,7 +89,7 @@ public class ExtendedIcon extends Icon { .replace("{amount}", Integer.toString(requiredItem.getAmount())) .replace("{datavalue}", requiredItem.hasRestrictiveDataValue() ? Short.toString(requiredItem.getDataValue()) : ChestCommands.getLang().any) ); - return; + return isCloseOnClick(); } } @@ -98,7 +98,7 @@ public class ExtendedIcon extends Icon { if (price > 0) { if (!player.hasPermission(Permissions.BYPASS_ECONOMY) && !EconomyBridge.takeMoney(player, price)) { player.sendMessage(ChatColor.RED + "Error: the transaction couldn't be executed. Please inform the staff."); - return; + return isCloseOnClick(); } } @@ -106,7 +106,7 @@ public class ExtendedIcon extends Icon { requiredItem.takeItem(player); } - super.onClick(player); + return super.onClick(player); } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/BroadcastCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/BroadcastIconCommand.java similarity index 76% rename from ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/BroadcastCommand.java rename to ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/BroadcastIconCommand.java index bcad50f..a761659 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/BroadcastCommand.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/BroadcastIconCommand.java @@ -6,9 +6,9 @@ import org.bukkit.entity.Player; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; import com.gmail.filoghost.chestcommands.util.Utils; -public class BroadcastCommand extends IconCommand { +public class BroadcastIconCommand extends IconCommand { - public BroadcastCommand(String command) { + public BroadcastIconCommand(String command) { super(Utils.addColors(command)); } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ConsoleCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ConsoleIconCommand.java similarity index 74% rename from ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ConsoleCommand.java rename to ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ConsoleIconCommand.java index d5bbb75..1cb0ffc 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ConsoleCommand.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ConsoleIconCommand.java @@ -5,9 +5,9 @@ import org.bukkit.entity.Player; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; -public class ConsoleCommand extends IconCommand { +public class ConsoleIconCommand extends IconCommand { - public ConsoleCommand(String command) { + public ConsoleIconCommand(String command) { super(command); } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveIconCommand.java similarity index 86% rename from ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveCommand.java rename to ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveIconCommand.java index e557a61..d9aa889 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveCommand.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveIconCommand.java @@ -8,12 +8,12 @@ import com.gmail.filoghost.chestcommands.exception.FormatException; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; import com.gmail.filoghost.chestcommands.util.ItemStackReader; -public class GiveCommand extends IconCommand { +public class GiveIconCommand extends IconCommand { ItemStack itemToGive; String errorMessage; - public GiveCommand(String command) { + public GiveIconCommand(String command) { super(command); try { diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveMoneyCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveMoneyIconCommand.java similarity index 86% rename from ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveMoneyCommand.java rename to ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveMoneyIconCommand.java index e407479..411b468 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveMoneyCommand.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/GiveMoneyIconCommand.java @@ -7,12 +7,12 @@ import com.gmail.filoghost.chestcommands.bridge.EconomyBridge; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; import com.gmail.filoghost.chestcommands.util.Utils; -public class GiveMoneyCommand extends IconCommand { +public class GiveMoneyIconCommand extends IconCommand { double moneyToGive; String errorMessage; - public GiveMoneyCommand(String command) { + public GiveMoneyIconCommand(String command) { super(command); if (!Utils.isValidPositiveDouble(command)) { diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpIconCommand.java similarity index 79% rename from ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpCommand.java rename to ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpIconCommand.java index fef1a30..1a67b54 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpCommand.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpIconCommand.java @@ -4,9 +4,9 @@ import org.bukkit.entity.Player; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; -public class OpCommand extends IconCommand { +public class OpIconCommand extends IconCommand { - public OpCommand(String command) { + public OpIconCommand(String command) { super(command); } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpenCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpenIconCommand.java similarity index 83% rename from ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpenCommand.java rename to ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpenIconCommand.java index 565e66a..ce68d4a 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpenCommand.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/OpenIconCommand.java @@ -7,9 +7,9 @@ import com.gmail.filoghost.chestcommands.ChestCommands; import com.gmail.filoghost.chestcommands.api.IconMenu; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; -public class OpenCommand extends IconCommand { +public class OpenIconCommand extends IconCommand { - public OpenCommand(String command) { + public OpenIconCommand(String command) { super(command); } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/PlayerCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/PlayerIconCommand.java similarity index 71% rename from ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/PlayerCommand.java rename to ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/PlayerIconCommand.java index 4b5d8f2..8c022b2 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/PlayerCommand.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/PlayerIconCommand.java @@ -4,9 +4,9 @@ import org.bukkit.entity.Player; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; -public class PlayerCommand extends IconCommand { +public class PlayerIconCommand extends IconCommand { - public PlayerCommand(String command) { + public PlayerIconCommand(String command) { super(command); } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ServerCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ServerIconCommand.java similarity index 75% rename from ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ServerCommand.java rename to ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ServerIconCommand.java index 3c41401..28dacda 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ServerCommand.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/ServerIconCommand.java @@ -5,9 +5,9 @@ import org.bukkit.entity.Player; import com.gmail.filoghost.chestcommands.bridge.bungee.BungeeCordUtils; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; -public class ServerCommand extends IconCommand { +public class ServerIconCommand extends IconCommand { - public ServerCommand(String command) { + public ServerIconCommand(String command) { super(command); } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/SoundCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/SoundIconCommand.java similarity index 87% rename from ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/SoundCommand.java rename to ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/SoundIconCommand.java index 23c5eae..693e944 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/SoundCommand.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/SoundIconCommand.java @@ -7,14 +7,14 @@ import org.bukkit.entity.Player; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; import com.gmail.filoghost.chestcommands.util.Utils; -public class SoundCommand extends IconCommand { +public class SoundIconCommand extends IconCommand { Sound sound; float pitch; float volume; String errorMessage; - public SoundCommand(String command) { + public SoundIconCommand(String command) { super(command); pitch = 1.0f; diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/TellCommand.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/TellIconCommand.java similarity index 76% rename from ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/TellCommand.java rename to ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/TellIconCommand.java index e7f03a4..503878a 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/TellCommand.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/internal/icon/command/TellIconCommand.java @@ -5,9 +5,9 @@ import org.bukkit.entity.Player; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; import com.gmail.filoghost.chestcommands.util.Utils; -public class TellCommand extends IconCommand { +public class TellIconCommand extends IconCommand { - public TellCommand(String command) { + public TellIconCommand(String command) { super(Utils.addColors(command)); } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/CommandSerializer.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/CommandSerializer.java index 344cef3..933a94c 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/CommandSerializer.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/CommandSerializer.java @@ -9,16 +9,16 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import com.gmail.filoghost.chestcommands.internal.icon.IconCommand; -import com.gmail.filoghost.chestcommands.internal.icon.command.BroadcastCommand; -import com.gmail.filoghost.chestcommands.internal.icon.command.ConsoleCommand; -import com.gmail.filoghost.chestcommands.internal.icon.command.GiveCommand; -import com.gmail.filoghost.chestcommands.internal.icon.command.GiveMoneyCommand; -import com.gmail.filoghost.chestcommands.internal.icon.command.OpCommand; -import com.gmail.filoghost.chestcommands.internal.icon.command.OpenCommand; -import com.gmail.filoghost.chestcommands.internal.icon.command.PlayerCommand; -import com.gmail.filoghost.chestcommands.internal.icon.command.ServerCommand; -import com.gmail.filoghost.chestcommands.internal.icon.command.SoundCommand; -import com.gmail.filoghost.chestcommands.internal.icon.command.TellCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.BroadcastIconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.ConsoleIconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.GiveIconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.GiveMoneyIconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.OpIconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.OpenIconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.PlayerIconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.ServerIconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.SoundIconCommand; +import com.gmail.filoghost.chestcommands.internal.icon.command.TellIconCommand; import com.gmail.filoghost.chestcommands.util.ErrorLogger; import com.google.common.collect.Lists; @@ -26,15 +26,15 @@ public class CommandSerializer { private static Map> commandTypesMap = new HashMap>(); static { - commandTypesMap.put(commandPattern("console:"), ConsoleCommand.class); - commandTypesMap.put(commandPattern("op:"), OpCommand.class); - commandTypesMap.put(commandPattern("open:"), OpenCommand.class); - commandTypesMap.put(commandPattern("server:?"), ServerCommand.class); // The colon is optional. - commandTypesMap.put(commandPattern("tell:"), TellCommand.class); - commandTypesMap.put(commandPattern("broadcast:"), BroadcastCommand.class); - commandTypesMap.put(commandPattern("give:"), GiveCommand.class); - commandTypesMap.put(commandPattern("give-?money:"), GiveMoneyCommand.class); - commandTypesMap.put(commandPattern("sound:"), SoundCommand.class); + commandTypesMap.put(commandPattern("console:"), ConsoleIconCommand.class); + commandTypesMap.put(commandPattern("op:"), OpIconCommand.class); + commandTypesMap.put(commandPattern("open:"), OpenIconCommand.class); + commandTypesMap.put(commandPattern("server:?"), ServerIconCommand.class); // The colon is optional. + commandTypesMap.put(commandPattern("tell:"), TellIconCommand.class); + commandTypesMap.put(commandPattern("broadcast:"), BroadcastIconCommand.class); + commandTypesMap.put(commandPattern("give:"), GiveIconCommand.class); + commandTypesMap.put(commandPattern("give-?money:"), GiveMoneyIconCommand.class); + commandTypesMap.put(commandPattern("sound:"), SoundIconCommand.class); } private static Pattern commandPattern(String regex) { @@ -92,7 +92,7 @@ public class CommandSerializer { } } - return new PlayerCommand(input); // Normal command, no match found. + return new PlayerIconCommand(input); // Normal command, no match found. } } diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/IconSerializer.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/IconSerializer.java index 5af13a6..770d0ed 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/IconSerializer.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/serializer/IconSerializer.java @@ -98,13 +98,15 @@ public class IconSerializer { } } - icon.setCloseOnClick(!section.getBoolean(Nodes.KEEP_OPEN)); // Inverted icon.setPermission(section.getString(Nodes.PERMISSION)); icon.setPermissionMessage(Utils.addColors(section.getString(Nodes.PERMISSION_MESSAGE))); + boolean closeOnClick = !section.getBoolean(Nodes.KEEP_OPEN); + icon.setCloseOnClick(closeOnClick); + if (section.isSet(Nodes.COMMAND)) { - List commands = CommandSerializer.readCommands(section.getString(Nodes.COMMAND)); - icon.setClickHandler(new CommandsClickHandler(commands)); + List commands = CommandSerializer.readCommands(section.getString(Nodes.COMMAND)); + icon.setClickHandler(new CommandsClickHandler(commands, closeOnClick)); } double price = section.getDouble(Nodes.PRICE);