Make methods in ChestCommands class static

This commit is contained in:
filoghost 2020-07-25 10:48:51 +02:00
parent cf8e5da6bf
commit a86e3a6951
7 changed files with 34 additions and 31 deletions

View File

@ -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,24 +79,24 @@ 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();
@ -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;
}

View File

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

View File

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

View File

@ -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.");

View File

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

View File

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

View File

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