diff --git a/pom.xml b/pom.xml index 596e13e..7492d79 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ ch.hutch79 F-Command - 2.5.1 + 2.5.2 jar F-Command diff --git a/src/main/java/ch/hutch79/FCommand.java b/src/main/java/ch/hutch79/FCommand.java index 296062c..36c6dfe 100644 --- a/src/main/java/ch/hutch79/FCommand.java +++ b/src/main/java/ch/hutch79/FCommand.java @@ -2,7 +2,7 @@ import ch.hutch79.command.Command; import ch.hutch79.command.CommandTab; -import ch.hutch79.utility.EventListener; +import ch.hutch79.events.EventHandler; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginDescriptionFile; @@ -14,7 +14,7 @@ public final class FCommand extends JavaPlugin { PluginDescriptionFile pdf = this.getDescription(); private static FCommand instance; - private static EventListener eventListener; + private static EventHandler eventHandler; private boolean isPlaceholderApiInstalled = false; private static boolean debug; @@ -22,14 +22,14 @@ public final class FCommand extends JavaPlugin { public void onEnable() { instance = this; - eventListener = new EventListener(); + eventHandler = new EventHandler(); // getConfig().options().copyDefaults(); saveDefaultConfig(); reloadConfig(); - eventListener.EventListenerInit(); - Bukkit.getPluginManager().registerEvents(eventListener, this); + eventHandler.EventListenerInit(); + Bukkit.getPluginManager().registerEvents(eventHandler, this); Objects.requireNonNull(getCommand("fcommand")).setExecutor(new Command()); Objects.requireNonNull(getCommand("fcommand")).setTabCompleter(new CommandTab()); @@ -95,8 +95,8 @@ public static FCommand getInstance() { return instance; } - public static EventListener getListener() { - return eventListener; + public static EventHandler getListener() { + return eventHandler; } public static boolean getDebug() { diff --git a/src/main/java/ch/hutch79/command/Command.java b/src/main/java/ch/hutch79/command/Command.java index 009aeab..2e2d7b8 100644 --- a/src/main/java/ch/hutch79/command/Command.java +++ b/src/main/java/ch/hutch79/command/Command.java @@ -3,7 +3,6 @@ import ch.hutch79.FCommand; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; public class Command implements CommandExecutor { diff --git a/src/main/java/ch/hutch79/commandExecutor/EventCommandExecutor.java b/src/main/java/ch/hutch79/commandExecutor/EventCommandExecutor.java new file mode 100644 index 0000000..582f5d3 --- /dev/null +++ b/src/main/java/ch/hutch79/commandExecutor/EventCommandExecutor.java @@ -0,0 +1,98 @@ +package ch.hutch79.commandExecutor; + +import ch.hutch79.FCommand; +import ch.hutch79.utility.ConfigManager; +import ch.hutch79.utility.Debugger; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class EventCommandExecutor { + private final ConfigManager configManager; + private final List commandOptions; + public EventCommandExecutor(List commandOptions) { + this.commandOptions = commandOptions; + configManager = new ConfigManager(this.commandOptions); + } + + public boolean commandExecutor(Player player, Event event) { + String eventKey = ""; + + if (Objects.equals(event.getEventName(), "PlayerSwapHandItemsEvent")) { + eventKey = "f"; + } else if (Objects.equals(event.getEventName(), "PlayerDropItemEvent")) { + eventKey = "q"; + } + + for (int count = 0; count < commandOptions.size(); count++) { + + + Debugger.debug("Event while count: §e" + (count)); + Debugger.debug("Event while current command: §e" + commandOptions.get(count)); + + + if (!configManager.getInfo(count, "key").equalsIgnoreCase(eventKey)) { // Correct Key was pressed? + Debugger.debug("return key - §e" + commandOptions.get(count)); + continue; + } + + if (!configManager.getInfo(count, "permission").equalsIgnoreCase("none")) { // Permission is set? + if (!player.hasPermission(configManager.getInfo(count, "permission"))) { // Correct Permission? + Debugger.debug("return permission - §e" + commandOptions.get(count)); + continue; + } + } + + if (configManager.getInfo(count, "requireShift").equalsIgnoreCase("true")) { + if (!player.isSneaking()) { + Debugger.debug("return sneaking 1 - §e" + commandOptions.get(count)); + continue; + } + } else if (configManager.getInfo(count, "requireShift").equalsIgnoreCase("false")) { + if (player.isSneaking()) { + Debugger.debug("return sneaking 2 - §e" + commandOptions.get(count)); + continue; + } + } + + List commandsList; + String commandString = configManager.getInfo(count, "command"); + + if (commandString.charAt(0) == '[' && commandString.charAt(commandString.length() - 1) == ']') { // Check if first and last character are `[` and `]` + commandsList = configManager.getCfg().getStringList("command." + commandOptions.get(count) + "." + "command"); + } else { + commandsList = new ArrayList<>(1); + commandsList.add(commandString); + } + + if (configManager.getInfo(count, "executeAsServer").equalsIgnoreCase("true")) { + for (String i : commandsList) { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), FCommand.getInstance().replacePlaceholders(player, i)); + } + + Debugger.debug("Executed by Server - §e" + commandOptions.get(count)); + } else { + for (String i : commandsList) { + player.performCommand(FCommand.getInstance().replacePlaceholders(player, i)); + } + Debugger.debug("Executed by Player - §e" + commandOptions.get(count)); + } + + + if (configManager.getInfo(count, "cancel").equalsIgnoreCase("true")) { + + if (eventKey.equalsIgnoreCase("f")) { + return true; + } else if (eventKey.equalsIgnoreCase("q")) { + return true; + } + Debugger.debug("event canceled - §e" + commandOptions.get(count)); + } + } + return false; + } +} \ No newline at end of file diff --git a/src/main/java/ch/hutch79/events/EventHandler.java b/src/main/java/ch/hutch79/events/EventHandler.java new file mode 100644 index 0000000..98b05e7 --- /dev/null +++ b/src/main/java/ch/hutch79/events/EventHandler.java @@ -0,0 +1,65 @@ +package ch.hutch79.events; + +import ch.hutch79.FCommand; +import ch.hutch79.commandExecutor.EventCommandExecutor; +import ch.hutch79.utility.Debugger; +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerSwapHandItemsEvent; + +import java.io.File; +import java.util.*; + +public class EventHandler implements Listener { + + private final FCommand mainInstance = FCommand.getInstance(); + private EventCommandExecutor eventCommandExecutor; + + FileConfiguration cfg; + + public void EventListenerInit() { + mainInstance.reloadConfig(); + Set commandOptions2 = Objects.requireNonNull(FCommand.getInstance().getConfig().getConfigurationSection("command")).getKeys(false); + List commandOptions = new ArrayList<>(commandOptions2.size()); + commandOptions.addAll(commandOptions2); + FCommand.setDebug(mainInstance.getConfig().getBoolean("debug")); + Debugger.debug("commandOptions list: §e" + commandOptions); + + cfg = YamlConfiguration.loadConfiguration(new File("plugins" + File.separator + "F-Command", "config.yml")); + + Bukkit.getConsoleSender().sendMessage("§dF-Command §8> §7Loaded Commands: " + commandOptions); + + eventCommandExecutor = new EventCommandExecutor(commandOptions); + } + + @org.bukkit.event.EventHandler + private void onSwapHandItemsEvent(PlayerSwapHandItemsEvent e) { + Debugger.debug("PlayerSwapHandItemsEvent detected"); + if (eventCommandExecutor.commandExecutor(e.getPlayer(), e)) + e.setCancelled(true); + } + private Boolean ignoreEvent = false; + @org.bukkit.event.EventHandler + private void inventoryClickEvent(InventoryClickEvent e) { + if (e.getSlotType() != InventoryType.SlotType.valueOf("OUTSIDE")) { + Debugger.debug("Event ignored, not OUTSIDE"); + ignoreEvent = true; + } + } + + @org.bukkit.event.EventHandler + private void dropItemEvent(PlayerDropItemEvent e) { + Debugger.debug("PlayerDropItemEvent detected: " + e.getPlayer()); + if (!ignoreEvent) { + Debugger.debug("It was Q"); + if (eventCommandExecutor.commandExecutor(e.getPlayer(), e)) + e.setCancelled(true); + } + ignoreEvent = false; + } +} diff --git a/src/main/java/ch/hutch79/utility/ConfigManager.java b/src/main/java/ch/hutch79/utility/ConfigManager.java new file mode 100644 index 0000000..1e04d68 --- /dev/null +++ b/src/main/java/ch/hutch79/utility/ConfigManager.java @@ -0,0 +1,55 @@ +package ch.hutch79.utility; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.util.List; + +public class ConfigManager { + private final List commandOptions; + private FileConfiguration cfg; + + public ConfigManager(List commandOptions) { + this.commandOptions = commandOptions; + } + + public String getInfo(int count, String value) { + + cfg = YamlConfiguration.loadConfiguration(new File("plugins" + File.separator + "F-Command", "config.yml")); + String result = cfg.getString("command." + commandOptions.get(count) + "." + value); + if (result == null) { + Debugger.debug("The Value " + value + " for the Command " + commandOptions.get(count) + " is not set!"); + + if (value.equals("key")) { + return "f"; + } + + if (value.equals("permission")) { + return "none"; + } + + if (value.equals("requireShift")) { + return "true"; + } + + if (value.equals("cancel")) { + return "false"; + } + + if (value.equals("executeAsServer")) { + return "false"; + } + + if (value.equals("command")) { + return "say hi, im a default command. Please edit the config.yml to set your own command."; + } + } + + return result; + } + + public FileConfiguration getCfg () { + return cfg; + } +} \ No newline at end of file diff --git a/src/main/java/ch/hutch79/utility/EventListener.java b/src/main/java/ch/hutch79/utility/EventListener.java deleted file mode 100644 index 1145f8a..0000000 --- a/src/main/java/ch/hutch79/utility/EventListener.java +++ /dev/null @@ -1,174 +0,0 @@ -package ch.hutch79.utility; - -import ch.hutch79.FCommand; -import org.bukkit.Bukkit; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryType; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerSwapHandItemsEvent; - -import java.io.File; -import java.util.*; - -public class EventListener implements Listener { - - private final FCommand mainInstance = FCommand.getInstance(); - private List commandOptions; - private PlayerSwapHandItemsEvent playerSwapHandItemsEvent; - private PlayerDropItemEvent playerDropItemEvent; - FileConfiguration cfg; - - public void EventListenerInit() { - mainInstance.reloadConfig(); - Set commandOptions2 = Objects.requireNonNull(FCommand.getInstance().getConfig().getConfigurationSection("command")).getKeys(false); - commandOptions = new ArrayList<>(commandOptions2.size()); - commandOptions.addAll(commandOptions2); - FCommand.setDebug(mainInstance.getConfig().getBoolean("debug")); - Debugger.debug("commandOptions list: §e" + commandOptions); - - cfg = YamlConfiguration.loadConfiguration(new File("plugins" + File.separator + "F-Command", "config.yml")); - - Bukkit.getConsoleSender().sendMessage("§dF-Command §8> §7Loaded Commands: " + commandOptions); - } - - private String getInfo(int count, String value){ - - String result = cfg.getString("command." + commandOptions.get(count) + "." + value); - if(result == null) { - Debugger.debug("The Value " + value + " for the Command " + commandOptions.get(count) + " is not set!"); - - if (value.equals("key")) { - return "f"; - } - - if (value.equals("permission")) { - return "none"; - } - - if (value.equals("requireShift")) { - return "true"; - } - - if (value.equals("cancel")) { - return "false"; - } - - if (value.equals("executeAsServer")) { - return "false"; - } - - if (value.equals("command")) { - return "say hi, im a default command. Please edit the config.yml to set your own command."; - } - } - - return result; - } - - private void commandExecuter (Player player, String eventKey) { - - if (Objects.equals(eventKey, "PlayerSwapHandItemsEvent")) { - eventKey = "f"; - } else if (Objects.equals(eventKey, "PlayerDropItemEvent")) { - eventKey = "q"; - } - - for (int count = 0; count < commandOptions.size(); count++) { - - - Debugger.debug("Event while count: §e" + (count)); - Debugger.debug("Event while current command: §e" + commandOptions.get(count)); - - - if (!getInfo(count, "key").equalsIgnoreCase(eventKey)) { // Correct Key was pressed? - Debugger.debug("return key - §e" + commandOptions.get(count)); - continue; - } - - if (!getInfo(count, "permission").equalsIgnoreCase("none")) { // Permission in set? - if (!player.hasPermission(getInfo(count, "permission"))) { // Correct Permission? - Debugger.debug("return permission - §e" + commandOptions.get(count)); - continue; - } - } - - if (getInfo(count, "requireShift").equalsIgnoreCase("true")) { - if (!player.isSneaking()) { - Debugger.debug("return sneaking 1 - §e" + commandOptions.get(count)); - continue; - } - } else if (getInfo(count, "requireShift").equalsIgnoreCase("false")) { - if (player.isSneaking()) { - Debugger.debug("return sneaking 2 - §e" + commandOptions.get(count)); - continue; - } - } - - - if (getInfo(count, "cancel").equalsIgnoreCase("true")) { - - if (eventKey.equalsIgnoreCase("f")) { - playerSwapHandItemsEvent.setCancelled(true); - } else if (eventKey.equalsIgnoreCase("q")) { - playerDropItemEvent.setCancelled(true); - } - Debugger.debug("event canceled - §e" + commandOptions.get(count)); - } - - List commandsList; - String commandString = getInfo(count, "command"); - - if (commandString.charAt(0) == '[' && commandString.charAt(commandString.length() - 1) == ']') { // Check if first and last character are `[` and `]` - commandsList = cfg.getStringList("command." + commandOptions.get(count) + "." + "command"); - } - else { - commandsList = new ArrayList<>(1); - commandsList.add(commandString); - } - - if (getInfo(count, "executeAsServer").equalsIgnoreCase("true")) { - for (String i: commandsList) { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), mainInstance.replacePlaceholders(player,i)); - } - - Debugger.debug("Executed by Server - §e" + commandOptions.get(count)); - } else { - for (String i: commandsList) { - player.performCommand(mainInstance.replacePlaceholders(player,i)); - } - Debugger.debug("Executed by Player - §e" + commandOptions.get(count)); - } - } - } - - @EventHandler - private void onSwapHandItemsEvent(PlayerSwapHandItemsEvent e) { - Debugger.debug("PlayerSwapHandItemsEvent detected"); - playerSwapHandItemsEvent = e; - commandExecuter(e.getPlayer(), e.getEventName()); - } - private Boolean ignoreEvent = false; - @EventHandler - private void inventoryClickEvent(InventoryClickEvent e) { - if (e.getSlotType() != InventoryType.SlotType.valueOf("OUTSIDE")) { - Debugger.debug("Event ignored, not OUTSIDE"); - ignoreEvent = true; - } - } - - @EventHandler - private void dropItemEvent(PlayerDropItemEvent e) { - Debugger.debug("PlayerDropItemEvent detected: " + e.getPlayer()); - if (!ignoreEvent) { - Debugger.debug("It was Q"); - playerDropItemEvent = e; - commandExecuter(e.getPlayer(), e.getEventName()); - } - ignoreEvent = false; - } -}