mirror of
https://github.com/filoghost/ChestCommands.git
synced 2024-11-29 13:36:04 +01:00
Refactoring
This commit is contained in:
parent
27c05ece6a
commit
93724b5096
@ -89,17 +89,16 @@ public class CommandHandler extends CommandFramework {
|
||||
|
||||
Player target = null;
|
||||
|
||||
if (!(sender instanceof Player)) {
|
||||
CommandValidate.minLength(args, 3, "You must specify a player from the console.");
|
||||
target = Bukkit.getPlayerExact(args[2]);
|
||||
} else {
|
||||
if (sender instanceof Player) {
|
||||
if (args.length > 2) {
|
||||
CommandValidate.isTrue(sender.hasPermission(Permissions.COMMAND_BASE + "open.others"), "You don't have permission to open menus for others.");
|
||||
target = Bukkit.getPlayerExact(args[2]);
|
||||
} else {
|
||||
target = (Player) sender;
|
||||
}
|
||||
|
||||
} else {
|
||||
CommandValidate.minLength(args, 3, "You must specify a player from the console.");
|
||||
target = Bukkit.getPlayerExact(args[2]);
|
||||
}
|
||||
|
||||
CommandValidate.notNull(target, "That player is not online.");
|
||||
|
@ -18,6 +18,8 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import me.filoghost.chestcommands.ChestCommands;
|
||||
import me.filoghost.chestcommands.MenuManager;
|
||||
import me.filoghost.chestcommands.action.Action;
|
||||
import me.filoghost.chestcommands.action.OpenMenuAction;
|
||||
import me.filoghost.chestcommands.api.impl.ConfigurableIconImpl;
|
||||
import me.filoghost.chestcommands.bridge.EconomyBridge;
|
||||
import me.filoghost.chestcommands.util.MaterialsHelper;
|
||||
@ -37,6 +39,8 @@ public class AdvancedIcon extends ConfigurableIconImpl {
|
||||
private double moneyPrice;
|
||||
private int expLevelsPrice;
|
||||
private List<RequiredItem> requiredItems;
|
||||
private List<Action> clickActions;
|
||||
private boolean hasOpenMenuAction;
|
||||
|
||||
private boolean canClickIcon(Player player) {
|
||||
if (permission == null) {
|
||||
@ -126,6 +130,24 @@ public class AdvancedIcon extends ConfigurableIconImpl {
|
||||
this.requiredItems = requiredItems;
|
||||
}
|
||||
|
||||
public List<Action> getClickActions() {
|
||||
return clickActions;
|
||||
}
|
||||
|
||||
public void setClickActions(List<Action> clickActions) {
|
||||
this.clickActions = clickActions;
|
||||
|
||||
hasOpenMenuAction = false;
|
||||
if (clickActions != null) {
|
||||
for (Action action : clickActions) {
|
||||
if (action instanceof OpenMenuAction) {
|
||||
// Fix GUI closing if KEEP-OPEN is not set, and a command should open another GUI
|
||||
hasOpenMenuAction = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onClick(Player player) {
|
||||
|
||||
@ -162,7 +184,7 @@ public class AdvancedIcon extends ConfigurableIconImpl {
|
||||
if (requiredItems != null) {
|
||||
boolean notHasItem = false;
|
||||
for (RequiredItem item : requiredItems) {
|
||||
if (!item.hasItem(player)) {
|
||||
if (!item.isItemContainedIn(player.getInventory())) {
|
||||
notHasItem = true;
|
||||
player.sendMessage(ChestCommands.getLang().no_required_item
|
||||
.replace("{material}", MaterialsHelper.formatMaterial(item.getMaterial()))
|
||||
@ -194,7 +216,7 @@ public class AdvancedIcon extends ConfigurableIconImpl {
|
||||
|
||||
if (requiredItems != null) {
|
||||
for (RequiredItem item : requiredItems) {
|
||||
item.takeItem(player);
|
||||
item.takeItemFrom(player.getInventory());
|
||||
}
|
||||
}
|
||||
|
||||
@ -202,6 +224,16 @@ public class AdvancedIcon extends ConfigurableIconImpl {
|
||||
MenuManager.refreshOpenMenu(player);
|
||||
}
|
||||
|
||||
if (clickActions != null) {
|
||||
for (Action action : clickActions) {
|
||||
action.execute(player);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasOpenMenuAction) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return super.onClick(player);
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
package me.filoghost.chestcommands.internal;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.filoghost.chestcommands.util.Preconditions;
|
||||
@ -62,10 +62,10 @@ public class RequiredItem {
|
||||
return data == this.dataValue;
|
||||
}
|
||||
|
||||
public boolean hasItem(Player player) {
|
||||
public boolean isItemContainedIn(Inventory inventory) {
|
||||
int amountFound = 0;
|
||||
|
||||
for (ItemStack item : player.getInventory().getContents()) {
|
||||
for (ItemStack item : inventory.getContents()) {
|
||||
if (item != null && item.getType() == material && isValidDataValue(item.getDurability())) {
|
||||
amountFound += item.getAmount();
|
||||
}
|
||||
@ -74,14 +74,14 @@ public class RequiredItem {
|
||||
return amountFound >= amount;
|
||||
}
|
||||
|
||||
public boolean takeItem(Player player) {
|
||||
public boolean takeItemFrom(Inventory inventory) {
|
||||
if (amount <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int itemsToTake = amount; //start from amount and decrease
|
||||
|
||||
ItemStack[] contents = player.getInventory().getContents();
|
||||
ItemStack[] contents = inventory.getContents();
|
||||
ItemStack current = null;
|
||||
|
||||
|
||||
@ -95,7 +95,7 @@ public class RequiredItem {
|
||||
return true;
|
||||
} else {
|
||||
itemsToTake -= current.getAmount();
|
||||
player.getInventory().setItem(i, new ItemStack(Material.AIR));
|
||||
inventory.setItem(i, new ItemStack(Material.AIR));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,59 +0,0 @@
|
||||
/*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package me.filoghost.chestcommands.internal;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.filoghost.chestcommands.action.Action;
|
||||
import me.filoghost.chestcommands.action.OpenMenuAction;
|
||||
import me.filoghost.chestcommands.api.ClickHandler;
|
||||
import me.filoghost.chestcommands.api.ClickResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RunActionsClickHandler implements ClickHandler {
|
||||
|
||||
private List<Action> actions;
|
||||
private boolean forceClose;
|
||||
|
||||
public RunActionsClickHandler(List<Action> actions) {
|
||||
this.actions = actions;
|
||||
|
||||
if (actions != null && actions.size() > 0) {
|
||||
for (Action action : actions) {
|
||||
if (action instanceof OpenMenuAction) {
|
||||
// Fix GUI closing if KEEP-OPEN is not set, and a command should open another GUI
|
||||
this.forceClose = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClickResult onClick(Player player) {
|
||||
if (actions != null && actions.size() > 0) {
|
||||
for (Action action : actions) {
|
||||
action.execute(player);
|
||||
}
|
||||
}
|
||||
|
||||
if (forceClose) {
|
||||
return ClickResult.CLOSE;
|
||||
}
|
||||
|
||||
return ClickResult.DEFAULT;
|
||||
}
|
||||
|
||||
}
|
@ -26,7 +26,6 @@ import me.filoghost.chestcommands.ChestCommands;
|
||||
import me.filoghost.chestcommands.action.Action;
|
||||
import me.filoghost.chestcommands.config.AsciiPlaceholders;
|
||||
import me.filoghost.chestcommands.config.ConfigUtil;
|
||||
import me.filoghost.chestcommands.internal.RunActionsClickHandler;
|
||||
import me.filoghost.chestcommands.internal.AdvancedIcon;
|
||||
import me.filoghost.chestcommands.internal.RequiredItem;
|
||||
import me.filoghost.chestcommands.parser.EnchantmentParser.EnchantmentDetails;
|
||||
@ -196,7 +195,7 @@ public class IconParser {
|
||||
}
|
||||
|
||||
if (!actions.isEmpty()) {
|
||||
icon.setClickHandler(new RunActionsClickHandler(actions));
|
||||
icon.setClickActions(actions);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user