Fix "open" command not working if KEEP-OPEN is not set.

API - ClickHandler can now choose if the GUI should be closed or not.
Refactored some classes.
This commit is contained in:
filoghost 2014-11-01 11:20:16 +01:00
parent 78af0f7169
commit fef2b40250
16 changed files with 79 additions and 57 deletions

View File

@ -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);
}

View File

@ -32,8 +32,8 @@ public class Icon {
private ClickHandler clickHandler;
public Icon() {
closeOnClick = true;
enchantments = new HashMap<Enchantment, Integer>();
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;
}
}

View File

@ -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<IconCommand> commands;
private boolean closeOnClick;
public CommandsClickHandler(List<IconCommand> commands) {
public CommandsClickHandler(List<IconCommand> 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;
}

View File

@ -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);
}

View File

@ -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));
}

View File

@ -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);
}

View File

@ -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 {

View File

@ -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)) {

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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));
}

View File

@ -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<Pattern, Class<? extends IconCommand>> commandTypesMap = new HashMap<Pattern, Class<? extends IconCommand>>();
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.
}
}

View File

@ -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<IconCommand> commands = CommandSerializer.readCommands(section.getString(Nodes.COMMAND));
icon.setClickHandler(new CommandsClickHandler(commands));
List<IconCommand> commands = CommandSerializer.readCommands(section.getString(Nodes.COMMAND));
icon.setClickHandler(new CommandsClickHandler(commands, closeOnClick));
}
double price = section.getDouble(Nodes.PRICE);