diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/ChestCommands.java b/Plugin/src/main/java/me/filoghost/chestcommands/ChestCommands.java index 53fa5e6..8aa8f83 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/ChestCommands.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/ChestCommands.java @@ -45,6 +45,7 @@ import org.bstats.bukkit.MetricsLite; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; import java.io.IOException; import java.nio.file.Files; @@ -56,11 +57,11 @@ public class ChestCommands extends BaseJavaPlugin { public static final String CHAT_PREFIX = ChatColor.DARK_GREEN + "[" + ChatColor.GREEN + "ChestCommands" + ChatColor.DARK_GREEN + "] " + ChatColor.GREEN; - private static ChestCommands instance; + private static Plugin pluginInstance; private static Path dataFolderPath; - private ConfigManager configManager; - private MenuManager menuManager; + private static ConfigManager configManager; + private static MenuManager menuManager; private static Settings settings; private static Lang lang; private static CustomPlaceholders placeholders; @@ -78,29 +79,29 @@ public class ChestCommands extends BaseJavaPlugin { } } - if (instance != null || System.getProperty("ChestCommandsLoaded") != null) { + if (pluginInstance != null || System.getProperty("ChestCommandsLoaded") != null) { Log.warning("Please do not use /reload or plugin reloaders. Use the command \"/cc reload\" instead."); return; } System.setProperty("ChestCommandsLoaded", "true"); - instance = this; + pluginInstance = this; dataFolderPath = getDataFolder().toPath(); Log.setLogger(getLogger()); - BackendAPI.setImplementation(new DefaultBackendAPI()); - configManager = new ConfigManager(getDataFolderPath()); menuManager = new MenuManager(); settings = new Settings(); lang = new Lang(); placeholders = new CustomPlaceholders(); + BackendAPI.setImplementation(new DefaultBackendAPI()); + VaultEconomyHook.INSTANCE.setup(); BarAPIHook.INSTANCE.setup(); PlaceholderAPIHook.INSTANCE.setup(); BungeeCordHook.INSTANCE.setup(); - + if (VaultEconomyHook.INSTANCE.isEnabled()) { Log.info("Hooked Vault"); } else { @@ -137,7 +138,7 @@ public class ChestCommands extends BaseJavaPlugin { CommandFramework.register(this, new CommandHandler(menuManager, "chestcommands")); ErrorCollector errorCollector = load(); - + if (errorCollector.hasErrors()) { errorCollector.logToConsole(); Bukkit.getScheduler().runTaskLater(this, () -> { @@ -150,16 +151,12 @@ public class ChestCommands extends BaseJavaPlugin { Bukkit.getScheduler().runTaskTimer(this, new TickingTask(), 1L, 1L); } - public static Path getDataFolderPath() { - return dataFolderPath; - } - @Override public void onDisable() { closeAllMenus(); } - public ErrorCollector load() { + public static ErrorCollector load() { ErrorCollector errorCollector = new PrintableErrorCollector(); menuManager.clear(); boolean isFreshInstall = !Files.isDirectory(configManager.getRootDataFolder()); @@ -210,11 +207,15 @@ public class ChestCommands extends BaseJavaPlugin { } - public static ChestCommands getInstance() { - return instance; + public static Plugin getPluginInstance() { + return pluginInstance; } - public MenuManager getMenuManager() { + public static Path getDataFolderPath() { + return dataFolderPath; + } + + public static MenuManager getMenuManager() { return menuManager; } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/DefaultBackendAPI.java b/Plugin/src/main/java/me/filoghost/chestcommands/DefaultBackendAPI.java index 619c7be..e57b92f 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/DefaultBackendAPI.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/DefaultBackendAPI.java @@ -31,12 +31,12 @@ public class DefaultBackendAPI extends BackendAPI { @Override public boolean isPluginMenu(String yamlFile) { - return ChestCommands.getInstance().getMenuManager().getMenuByFileName(yamlFile) != null; + return ChestCommands.getMenuManager().getMenuByFileName(yamlFile) != null; } @Override public boolean openPluginMenu(Player player, String yamlFile) { - InternalIconMenu menu = ChestCommands.getInstance().getMenuManager().getMenuByFileName(yamlFile); + InternalIconMenu menu = ChestCommands.getMenuManager().getMenuByFileName(yamlFile); if (menu != null) { menu.open(player); diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/OpenMenuAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/OpenMenuAction.java index 150d5c7..40df9c9 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/OpenMenuAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/OpenMenuAction.java @@ -31,14 +31,14 @@ public class OpenMenuAction extends Action { @Override protected void execute0(final Player player) { - final InternalIconMenu menu = ChestCommands.getInstance().getMenuManager().getMenuByFileName(targetMenu.getValue(player)); + final InternalIconMenu menu = ChestCommands.getMenuManager().getMenuByFileName(targetMenu.getValue(player)); if (menu != null) { /* * Delay the task, since this action is executed in ClickInventoryEvent * and opening another inventory in the same moment is not a good idea. */ - Bukkit.getScheduler().runTask(ChestCommands.getInstance(), () -> { + Bukkit.getScheduler().runTask(ChestCommands.getPluginInstance(), () -> { menu.openCheckingPermission(player); }); diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java b/Plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java index 0a7833a..1a6e8a2 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java @@ -43,7 +43,7 @@ public class CommandHandler extends CommandFramework { if (args.length == 0) { // This info is accessible to anyone. Please don't remove it, remember that Chest Commands is developed for free sender.sendMessage(ChestCommands.CHAT_PREFIX); - sender.sendMessage(ChatColor.GREEN + "Version: " + ChatColor.GRAY + ChestCommands.getInstance().getDescription().getVersion()); + sender.sendMessage(ChatColor.GREEN + "Version: " + ChatColor.GRAY + ChestCommands.getPluginInstance().getDescription().getVersion()); sender.sendMessage(ChatColor.GREEN + "Developer: " + ChatColor.GRAY + "filoghost"); sender.sendMessage(ChatColor.GREEN + "Commands: " + ChatColor.GRAY + "/" + label + " help"); return; @@ -83,7 +83,7 @@ public class CommandHandler extends CommandFramework { ChestCommands.closeAllMenus(); - ErrorCollector errorCollector = ChestCommands.getInstance().load(); + ErrorCollector errorCollector = ChestCommands.load(); if (!errorCollector.hasErrors()) { sender.sendMessage(ChestCommands.CHAT_PREFIX + "Plugin reloaded."); diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/hook/BungeeCordHook.java b/Plugin/src/main/java/me/filoghost/chestcommands/hook/BungeeCordHook.java index 241011b..79223de 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/hook/BungeeCordHook.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/hook/BungeeCordHook.java @@ -29,8 +29,8 @@ public enum BungeeCordHook implements PluginHook { @Override public void setup() { - if (!Bukkit.getMessenger().isOutgoingChannelRegistered(ChestCommands.getInstance(), "BungeeCord")) { - Bukkit.getMessenger().registerOutgoingPluginChannel(ChestCommands.getInstance(), "BungeeCord"); + if (!Bukkit.getMessenger().isOutgoingChannelRegistered(ChestCommands.getPluginInstance(), "BungeeCord")) { + Bukkit.getMessenger().registerOutgoingPluginChannel(ChestCommands.getPluginInstance(), "BungeeCord"); } } @@ -57,7 +57,7 @@ public enum BungeeCordHook implements PluginHook { throw new AssertionError(); } - player.sendPluginMessage(ChestCommands.getInstance(), "BungeeCord", byteArrayOutputStream.toByteArray()); + player.sendPluginMessage(ChestCommands.getPluginInstance(), "BungeeCord", byteArrayOutputStream.toByteArray()); } } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java b/Plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java index d0d0abb..bfd1fc8 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java @@ -89,7 +89,7 @@ public class InventoryListener implements Listener { } // Only handle the click AFTER the event has finished - Bukkit.getScheduler().runTask(ChestCommands.getInstance(), () -> { + Bukkit.getScheduler().runTask(ChestCommands.getPluginInstance(), () -> { ClickResult result = slotClickHandler.onClick(); if (result == ClickResult.CLOSE) { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/listener/SignListener.java b/Plugin/src/main/java/me/filoghost/chestcommands/listener/SignListener.java index ab04229..bc871f1 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/listener/SignListener.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/listener/SignListener.java @@ -77,12 +77,14 @@ public class SignListener implements Listener { @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onCreateMenuSign(SignChangeEvent event) { - if (isCreatingMenuSign(event.getLine(HEADER_LINE)) && canCreateMenuSign(event.getPlayer())) { + Player player = event.getPlayer(); + + if (isCreatingMenuSign(event.getLine(HEADER_LINE)) && canCreateMenuSign(player)) { String menuFileName = event.getLine(FILENAME_LINE).trim(); if (menuFileName.isEmpty()) { event.setCancelled(true); - event.getPlayer().sendMessage(ChatColor.RED + "You must write a menu name in the second line."); + player.sendMessage(ChatColor.RED + "You must write a menu name in the second line."); return; } @@ -91,12 +93,12 @@ public class SignListener implements Listener { InternalIconMenu iconMenu = menuManager.getMenuByFileName(menuFileName); if (iconMenu == null) { event.setCancelled(true); - event.getPlayer().sendMessage(ChatColor.RED + "Menu \"" + menuFileName + "\" was not found."); + player.sendMessage(ChatColor.RED + "Menu \"" + menuFileName + "\" was not found."); return; } event.setLine(HEADER_LINE, VALID_SIGN_COLOR + event.getLine(HEADER_LINE)); - event.getPlayer().sendMessage(ChatColor.GREEN + "Successfully created a sign for the menu " + menuFileName + "."); + player.sendMessage(ChatColor.GREEN + "Successfully created a sign for the menu " + menuFileName + "."); } }