Refactoring
This commit is contained in:
parent
2dfecfb3a2
commit
9989c9abb0
|
@ -35,7 +35,6 @@ import me.filoghost.updatechecker.UpdateChecker;
|
|||
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;
|
||||
|
@ -52,7 +51,6 @@ public class ChestCommands extends BaseJavaPlugin {
|
|||
private static Path dataFolderPath;
|
||||
|
||||
private static ConfigManager configManager;
|
||||
private static MenuManager menuManager;
|
||||
private static CustomPlaceholders placeholders;
|
||||
|
||||
private static ErrorCollector lastLoadErrors;
|
||||
|
@ -79,7 +77,6 @@ public class ChestCommands extends BaseJavaPlugin {
|
|||
dataFolderPath = getDataFolder().toPath();
|
||||
Log.setLogger(getLogger());
|
||||
configManager = new ConfigManager(getDataFolderPath());
|
||||
menuManager = new MenuManager();
|
||||
placeholders = new CustomPlaceholders();
|
||||
|
||||
BackendAPI.setImplementation(new DefaultBackendAPI());
|
||||
|
@ -117,12 +114,12 @@ public class ChestCommands extends BaseJavaPlugin {
|
|||
int pluginID = 3658;
|
||||
new MetricsLite(this, pluginID);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(new CommandListener(menuManager), this);
|
||||
Bukkit.getPluginManager().registerEvents(new InventoryListener(menuManager), this);
|
||||
Bukkit.getPluginManager().registerEvents(new CommandListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new InventoryListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new JoinListener(), this);
|
||||
Bukkit.getPluginManager().registerEvents(new SignListener(menuManager), this);
|
||||
Bukkit.getPluginManager().registerEvents(new SignListener(), this);
|
||||
|
||||
new CommandHandler(menuManager, "chestcommands").register(this);
|
||||
new CommandHandler("chestcommands").register(this);
|
||||
|
||||
ErrorCollector errorCollector = load();
|
||||
|
||||
|
@ -140,12 +137,12 @@ public class ChestCommands extends BaseJavaPlugin {
|
|||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
closeAllMenus();
|
||||
MenuManager.closeAllOpenMenuViews();
|
||||
}
|
||||
|
||||
public static ErrorCollector load() {
|
||||
ErrorCollector errorCollector = new PrintableErrorCollector();
|
||||
menuManager.clear();
|
||||
MenuManager.reset();
|
||||
boolean isFreshInstall = !Files.isDirectory(configManager.getRootDataFolder());
|
||||
try {
|
||||
Files.createDirectories(configManager.getRootDataFolder());
|
||||
|
@ -179,21 +176,13 @@ public class ChestCommands extends BaseJavaPlugin {
|
|||
|
||||
List<LoadedMenu> loadedMenus = configManager.tryLoadMenus(errorCollector);
|
||||
for (LoadedMenu loadedMenu : loadedMenus) {
|
||||
menuManager.registerMenu(loadedMenu, errorCollector);
|
||||
MenuManager.registerMenu(loadedMenu, errorCollector);
|
||||
}
|
||||
|
||||
ChestCommands.lastLoadErrors = errorCollector;
|
||||
return errorCollector;
|
||||
}
|
||||
|
||||
public static void closeAllMenus() {
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (MenuManager.getOpenMenuView(player) != null) {
|
||||
player.closeInventory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Plugin getPluginInstance() {
|
||||
return pluginInstance;
|
||||
|
@ -203,10 +192,6 @@ public class ChestCommands extends BaseJavaPlugin {
|
|||
return dataFolderPath;
|
||||
}
|
||||
|
||||
public static MenuManager getMenuManager() {
|
||||
return menuManager;
|
||||
}
|
||||
|
||||
public static boolean hasNewVersion() {
|
||||
return newVersion != null;
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import me.filoghost.chestcommands.icon.APIConfigurableIcon;
|
|||
import me.filoghost.chestcommands.icon.APIStaticIcon;
|
||||
import me.filoghost.chestcommands.menu.APIMenu;
|
||||
import me.filoghost.chestcommands.menu.InternalMenu;
|
||||
import me.filoghost.chestcommands.menu.MenuManager;
|
||||
import me.filoghost.chestcommands.placeholder.PlaceholderManager;
|
||||
import me.filoghost.fcommons.Preconditions;
|
||||
import org.bukkit.Material;
|
||||
|
@ -28,7 +29,7 @@ public class DefaultBackendAPI extends BackendAPI {
|
|||
public boolean pluginMenuExists(@NotNull String menuFileName) {
|
||||
Preconditions.notNull(menuFileName, "menuFileName");
|
||||
|
||||
return ChestCommands.getMenuManager().getMenuByFileName(menuFileName) != null;
|
||||
return MenuManager.getMenuByFileName(menuFileName) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -36,7 +37,7 @@ public class DefaultBackendAPI extends BackendAPI {
|
|||
Preconditions.notNull(player, "player");
|
||||
Preconditions.notNull(menuFileName, "menuFileName");
|
||||
|
||||
InternalMenu menu = ChestCommands.getMenuManager().getMenuByFileName(menuFileName);
|
||||
InternalMenu menu = MenuManager.getMenuByFileName(menuFileName);
|
||||
|
||||
if (menu != null) {
|
||||
menu.open(player);
|
||||
|
|
|
@ -8,6 +8,7 @@ package me.filoghost.chestcommands.action;
|
|||
import me.filoghost.chestcommands.ChestCommands;
|
||||
import me.filoghost.chestcommands.logging.Errors;
|
||||
import me.filoghost.chestcommands.menu.InternalMenu;
|
||||
import me.filoghost.chestcommands.menu.MenuManager;
|
||||
import me.filoghost.chestcommands.placeholder.PlaceholderString;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -23,7 +24,7 @@ public class OpenMenuAction implements Action {
|
|||
@Override
|
||||
public void execute(final Player player) {
|
||||
String menuName = targetMenu.getValue(player);
|
||||
final InternalMenu menu = ChestCommands.getMenuManager().getMenuByFileName(menuName);
|
||||
final InternalMenu menu = MenuManager.getMenuByFileName(menuName);
|
||||
|
||||
if (menu != null) {
|
||||
/*
|
||||
|
|
|
@ -30,11 +30,8 @@ import org.bukkit.entity.Player;
|
|||
|
||||
public class CommandHandler extends MultiCommandManager {
|
||||
|
||||
private final MenuManager menuManager;
|
||||
|
||||
public CommandHandler(MenuManager menuManager, String label) {
|
||||
public CommandHandler(String label) {
|
||||
super(label);
|
||||
this.menuManager = menuManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -74,7 +71,7 @@ public class CommandHandler extends MultiCommandManager {
|
|||
@Permission(Permissions.COMMAND_PREFIX + "reload")
|
||||
@DisplayPriority(100)
|
||||
public void reload(CommandSender sender) {
|
||||
ChestCommands.closeAllMenus();
|
||||
MenuManager.closeAllOpenMenuViews();
|
||||
|
||||
ErrorCollector errorCollector = ChestCommands.load();
|
||||
|
||||
|
@ -114,7 +111,7 @@ public class CommandHandler extends MultiCommandManager {
|
|||
@DisplayPriority(2)
|
||||
public void list(CommandSender sender) {
|
||||
sender.sendMessage(ChestCommands.CHAT_PREFIX + "Loaded menus:");
|
||||
for (String file : menuManager.getMenuFileNames()) {
|
||||
for (String file : MenuManager.getMenuFileNames()) {
|
||||
sender.sendMessage(ChatColor.GRAY + "- " + ChatColor.WHITE + file);
|
||||
}
|
||||
}
|
||||
|
@ -145,7 +142,7 @@ public class CommandHandler extends MultiCommandManager {
|
|||
CommandValidate.notNull(target, "That player is not online.");
|
||||
|
||||
String menuName = Utils.addYamlExtension(args[0]);
|
||||
InternalMenu menu = menuManager.getMenuByFileName(menuName);
|
||||
InternalMenu menu = MenuManager.getMenuByFileName(menuName);
|
||||
CommandValidate.notNull(menu, "The menu \"" + menuName + "\" was not found.");
|
||||
|
||||
if (!sender.hasPermission(menu.getOpenPermission())) {
|
||||
|
|
|
@ -5,22 +5,25 @@
|
|||
*/
|
||||
package me.filoghost.chestcommands.hook;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import me.filoghost.chestcommands.ChestCommands;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public enum BungeeCordHook implements PluginHook {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
public static final String BUNGEE_CORD_CHANNEL = "BungeeCord";
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
if (!Bukkit.getMessenger().isOutgoingChannelRegistered(ChestCommands.getPluginInstance(), "BungeeCord")) {
|
||||
Bukkit.getMessenger().registerOutgoingPluginChannel(ChestCommands.getPluginInstance(), "BungeeCord");
|
||||
if (!Bukkit.getMessenger().isOutgoingChannelRegistered(ChestCommands.getPluginInstance(), BUNGEE_CORD_CHANNEL)) {
|
||||
Bukkit.getMessenger().registerOutgoingPluginChannel(ChestCommands.getPluginInstance(), BUNGEE_CORD_CHANNEL);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,7 +50,7 @@ public enum BungeeCordHook implements PluginHook {
|
|||
throw new AssertionError();
|
||||
}
|
||||
|
||||
player.sendPluginMessage(ChestCommands.getPluginInstance(), "BungeeCord", byteArrayOutputStream.toByteArray());
|
||||
player.sendPluginMessage(ChestCommands.getPluginInstance(), BUNGEE_CORD_CHANNEL, byteArrayOutputStream.toByteArray());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -13,12 +13,6 @@ import org.bukkit.event.Listener;
|
|||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
|
||||
public class CommandListener implements Listener {
|
||||
|
||||
private final MenuManager menuManager;
|
||||
|
||||
public CommandListener(MenuManager menuManager) {
|
||||
this.menuManager = menuManager;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onCommand(PlayerCommandPreprocessEvent event) {
|
||||
|
@ -27,7 +21,7 @@ public class CommandListener implements Listener {
|
|||
return;
|
||||
}
|
||||
|
||||
InternalMenu menu = menuManager.getMenuByOpenCommand(command);
|
||||
InternalMenu menu = MenuManager.getMenuByOpenCommand(command);
|
||||
if (menu == null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -5,8 +5,6 @@
|
|||
*/
|
||||
package me.filoghost.chestcommands.listener;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
import me.filoghost.chestcommands.ChestCommands;
|
||||
import me.filoghost.chestcommands.api.ClickResult;
|
||||
import me.filoghost.chestcommands.api.Icon;
|
||||
|
@ -23,21 +21,18 @@ import org.bukkit.event.inventory.InventoryClickEvent;
|
|||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
public class InventoryListener implements Listener {
|
||||
|
||||
private final MenuManager menuManager;
|
||||
private final Map<Player, Long> antiClickSpam;
|
||||
|
||||
public InventoryListener(MenuManager menuManager) {
|
||||
this.menuManager = menuManager;
|
||||
this.antiClickSpam = new WeakHashMap<>();
|
||||
}
|
||||
private final Map<Player, Long> antiClickSpam = new WeakHashMap<>();
|
||||
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
|
||||
public void onInteract(PlayerInteractEvent event) {
|
||||
if (event.hasItem() && event.getAction() != Action.PHYSICAL) {
|
||||
menuManager.openMenuByItem(event.getPlayer(), event.getItem(), event.getAction());
|
||||
MenuManager.openMenuByItem(event.getPlayer(), event.getItem(), event.getAction());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,12 +30,7 @@ public class SignListener implements Listener {
|
|||
|
||||
private static final ChatColor VALID_SIGN_COLOR = ChatColor.DARK_BLUE;
|
||||
private static final String VALID_SIGN_HEADER = VALID_SIGN_COLOR + SIGN_CREATION_TRIGGER;
|
||||
|
||||
private final MenuManager menuManager;
|
||||
|
||||
public SignListener(MenuManager menuManager) {
|
||||
this.menuManager = menuManager;
|
||||
}
|
||||
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void onSignClick(PlayerInteractEvent event) {
|
||||
|
@ -56,7 +51,7 @@ public class SignListener implements Listener {
|
|||
}
|
||||
|
||||
String menuFileName = Utils.addYamlExtension(sign.getLine(FILENAME_LINE).trim());
|
||||
InternalMenu menu = menuManager.getMenuByFileName(menuFileName);
|
||||
InternalMenu menu = MenuManager.getMenuByFileName(menuFileName);
|
||||
|
||||
if (menu == null) {
|
||||
event.getPlayer().sendMessage(Lang.menu_not_found);
|
||||
|
@ -81,7 +76,7 @@ public class SignListener implements Listener {
|
|||
|
||||
menuFileName = Utils.addYamlExtension(menuFileName);
|
||||
|
||||
InternalMenu menu = menuManager.getMenuByFileName(menuFileName);
|
||||
InternalMenu menu = MenuManager.getMenuByFileName(menuFileName);
|
||||
if (menu == null) {
|
||||
event.setCancelled(true);
|
||||
player.sendMessage(ChatColor.RED + "Menu \"" + menuFileName + "\" was not found.");
|
||||
|
|
|
@ -5,10 +5,6 @@
|
|||
*/
|
||||
package me.filoghost.chestcommands.menu;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import me.filoghost.chestcommands.inventory.DefaultMenuView;
|
||||
import me.filoghost.chestcommands.inventory.MenuInventoryHolder;
|
||||
import me.filoghost.chestcommands.logging.Errors;
|
||||
|
@ -16,35 +12,35 @@ import me.filoghost.chestcommands.parsing.menu.LoadedMenu;
|
|||
import me.filoghost.chestcommands.parsing.menu.MenuOpenItem;
|
||||
import me.filoghost.fcommons.collection.CaseInsensitiveMap;
|
||||
import me.filoghost.fcommons.logging.ErrorCollector;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryView;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MenuManager {
|
||||
|
||||
private static Map<String, InternalMenu> menusByFile;
|
||||
private static Map<String, InternalMenu> menusByOpenCommand;
|
||||
private static Map<MenuOpenItem, InternalMenu> menusByOpenItem;
|
||||
private static final Map<String, InternalMenu> menusByFile = new CaseInsensitiveMap<>();
|
||||
private static final Map<String, InternalMenu> menusByOpenCommand = new CaseInsensitiveMap<>();
|
||||
private static final Map<MenuOpenItem, InternalMenu> menusByOpenItem = new HashMap<>();
|
||||
|
||||
public MenuManager() {
|
||||
menusByFile = new CaseInsensitiveMap<>();
|
||||
menusByOpenCommand = new CaseInsensitiveMap<>();
|
||||
menusByOpenItem = new HashMap<>();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
public static void reset() {
|
||||
menusByFile.clear();
|
||||
menusByOpenCommand.clear();
|
||||
menusByOpenItem.clear();
|
||||
}
|
||||
|
||||
public InternalMenu getMenuByFileName(String fileName) {
|
||||
public static InternalMenu getMenuByFileName(String fileName) {
|
||||
return menusByFile.get(fileName);
|
||||
}
|
||||
|
||||
public void registerMenu(LoadedMenu loadedMenu, ErrorCollector errorCollector) {
|
||||
public static void registerMenu(LoadedMenu loadedMenu, ErrorCollector errorCollector) {
|
||||
InternalMenu menu = loadedMenu.getMenu();
|
||||
|
||||
String fileName = loadedMenu.getSourceFile().getFileName().toString();
|
||||
|
@ -71,7 +67,7 @@ public class MenuManager {
|
|||
}
|
||||
}
|
||||
|
||||
public void openMenuByItem(Player player, ItemStack itemInHand, Action clickAction) {
|
||||
public static void openMenuByItem(Player player, ItemStack itemInHand, Action clickAction) {
|
||||
menusByOpenItem.forEach((openItem, menu) -> {
|
||||
if (openItem.matches(itemInHand, clickAction)) {
|
||||
menu.openCheckingPermission(player);
|
||||
|
@ -79,11 +75,11 @@ public class MenuManager {
|
|||
});
|
||||
}
|
||||
|
||||
public InternalMenu getMenuByOpenCommand(String openCommand) {
|
||||
public static InternalMenu getMenuByOpenCommand(String openCommand) {
|
||||
return menusByOpenCommand.get(openCommand);
|
||||
}
|
||||
|
||||
public Collection<String> getMenuFileNames() {
|
||||
public static Collection<String> getMenuFileNames() {
|
||||
return Collections.unmodifiableCollection(menusByFile.keySet());
|
||||
}
|
||||
|
||||
|
@ -91,15 +87,23 @@ public class MenuManager {
|
|||
return getMenuInventoryHolder(inventory) != null;
|
||||
}
|
||||
|
||||
public static void closeAllOpenMenuViews() {
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (getOpenMenuView(player) != null) {
|
||||
player.closeInventory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static DefaultMenuView getOpenMenuView(Player player) {
|
||||
InventoryView view = player.getOpenInventory();
|
||||
if (view == null) {
|
||||
InventoryView inventoryView = player.getOpenInventory();
|
||||
if (inventoryView == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
DefaultMenuView menuView = getOpenMenuView(view.getTopInventory());
|
||||
DefaultMenuView menuView = getOpenMenuView(inventoryView.getTopInventory());
|
||||
if (menuView == null) {
|
||||
menuView = getOpenMenuView(view.getBottomInventory());
|
||||
menuView = getOpenMenuView(inventoryView.getBottomInventory());
|
||||
}
|
||||
|
||||
return menuView;
|
||||
|
|
|
@ -6,17 +6,15 @@
|
|||
package me.filoghost.chestcommands.util;
|
||||
|
||||
import me.filoghost.fcommons.Strings;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public static String formatEnum(Enum<?> enumValue) {
|
||||
public static String formatEnum(@NotNull Enum<?> enumValue) {
|
||||
return Strings.capitalizeFully(enumValue.name().replace("_", " "));
|
||||
}
|
||||
|
||||
public static String addYamlExtension(String fileName) {
|
||||
if (fileName == null) {
|
||||
return null;
|
||||
}
|
||||
public static String addYamlExtension(@NotNull String fileName) {
|
||||
if (fileName.toLowerCase().endsWith(".yml")) {
|
||||
return fileName;
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue