From 094f354086b3aba7f066e2bbacde4bdb94d6a073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bro=C4=8Dko?= Date: Sat, 8 Sep 2018 18:04:15 +0200 Subject: [PATCH] singleton goodies --- src/cz/marwland/mc/core/EventListener.java | 19 ------------------- src/cz/marwland/mc/core/MarwCore.java | 14 ++++++++++---- src/cz/marwland/mc/core/features/Base.java | 7 +++---- src/cz/marwland/mc/core/features/Feature.java | 16 ++++++++++------ .../mc/essentials/features/Borders.java | 12 ++++++------ .../mc/essentials/features/ChatNotifier.java | 9 +++------ 6 files changed, 32 insertions(+), 45 deletions(-) delete mode 100644 src/cz/marwland/mc/core/EventListener.java diff --git a/src/cz/marwland/mc/core/EventListener.java b/src/cz/marwland/mc/core/EventListener.java deleted file mode 100644 index 1231ac4..0000000 --- a/src/cz/marwland/mc/core/EventListener.java +++ /dev/null @@ -1,19 +0,0 @@ -package cz.marwland.mc.core; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerLoginEvent; - -public class EventListener implements Listener { - private final MarwCore plugin; - - public EventListener(MarwCore plugin) { - this.plugin = plugin; - } - - @EventHandler - public void normalLogin(PlayerLoginEvent event) { - plugin.getServer().broadcastMessage("Welcome " + event.getPlayer().getDisplayName() + "!"); - } - -} diff --git a/src/cz/marwland/mc/core/MarwCore.java b/src/cz/marwland/mc/core/MarwCore.java index 66e4d53..74d37b9 100644 --- a/src/cz/marwland/mc/core/MarwCore.java +++ b/src/cz/marwland/mc/core/MarwCore.java @@ -15,17 +15,19 @@ public class MarwCore extends JavaPlugin { private ConfigManager configManager; private HashMap features = new HashMap<>(); + private static MarwCore INSTANCE = null; @Override public void onEnable() { + INSTANCE = this; //getServer().getPluginManager().registerEvents(new EventListener(this), this); configManager = new ConfigManager(this); - this.addFeature(new Base(this)); + this.addFeature(new Base()); - this.addFeature(new Borders(this)); - this.addFeature(new ChatNotifier(this)); - this.addFeature(new RandomTeleport(this)); + this.addFeature(new Borders()); + this.addFeature(new ChatNotifier()); + this.addFeature(new RandomTeleport()); configManager.load(); this.features.forEach((k, v) -> v.onEnable()); @@ -63,4 +65,8 @@ public class MarwCore extends JavaPlugin { return "marw.core"; } + public static MarwCore getInstance() { + return INSTANCE; + } + } diff --git a/src/cz/marwland/mc/core/features/Base.java b/src/cz/marwland/mc/core/features/Base.java index 2903f34..7584b64 100644 --- a/src/cz/marwland/mc/core/features/Base.java +++ b/src/cz/marwland/mc/core/features/Base.java @@ -6,7 +6,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.configuration.file.FileConfiguration; -import cz.marwland.mc.core.MarwCore; import cz.marwland.mc.core.features.Feature; @@ -14,10 +13,10 @@ public class Base extends Feature { FileConfiguration cfg; - public Base(MarwCore plugin) { - super(plugin, "marw"); + public Base() { + super("marw"); // String name, String description, String usageMessage, List aliases) - this.addCommand(new BukkitCommand("marw", "Core plugin controls.", "/marw", Arrays.asList("marwcore")) { + this.addCommand(new BukkitCommand("marw", "Core plugin controls.", "/marw", Arrays.asList()) { @Override public boolean execute(CommandSender sender, String commandLabel, String[] args) { if (args.length < 1) { diff --git a/src/cz/marwland/mc/core/features/Feature.java b/src/cz/marwland/mc/core/features/Feature.java index 9b5d0dd..6d0fd70 100644 --- a/src/cz/marwland/mc/core/features/Feature.java +++ b/src/cz/marwland/mc/core/features/Feature.java @@ -4,6 +4,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; +import org.bukkit.Bukkit; import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; import org.bukkit.command.defaults.BukkitCommand; @@ -14,33 +15,36 @@ import cz.marwland.mc.core.MarwCore; import net.md_5.bungee.api.ChatColor; public abstract class Feature implements Listener { - protected final MarwCore plugin; + protected final MarwCore plugin = MarwCore.getInstance(); private String name = ""; private ArrayList commands = new ArrayList<>(); private static CommandMap cmap; - public Feature(MarwCore plugin, String name) { - this.plugin = plugin; + public Feature(String name) { this.name = name; if (name == null) { this.name = this.getClass().getSimpleName().toLowerCase(); } Method commandMap; try { - commandMap = plugin.getServer().getClass().getMethod("getCommandMap"); - cmap = (CommandMap) commandMap.invoke(plugin.getServer()); + commandMap = Bukkit.getServer().getClass().getMethod("getCommandMap"); + cmap = (CommandMap) commandMap.invoke(Bukkit.getServer()); } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } } + public Feature() { + this(null); + } + public void onEnable() { if (cmap != null) { for (BukkitCommand cmd : commands) { cmap.register(plugin.getName().toLowerCase(), cmd); } } - plugin.getServer().getPluginManager().registerEvents(this, plugin); + Bukkit.getServer().getPluginManager().registerEvents(this, plugin); } public void onDisable() { diff --git a/src/cz/marwland/mc/essentials/features/Borders.java b/src/cz/marwland/mc/essentials/features/Borders.java index 2266664..2d786d8 100644 --- a/src/cz/marwland/mc/essentials/features/Borders.java +++ b/src/cz/marwland/mc/essentials/features/Borders.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.command.defaults.BukkitCommand; @@ -12,7 +13,6 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.util.StringUtil; import org.bukkit.util.Vector; -import cz.marwland.mc.core.MarwCore; import cz.marwland.mc.core.config.ConfigEntry; import cz.marwland.mc.core.config.EntryManager; import cz.marwland.mc.core.config.IValueExecuter; @@ -25,8 +25,8 @@ public class Borders extends Feature { FileConfiguration cfg; EntryManager emgr = new EntryManager(); - public Borders(MarwCore plugin) { - super(plugin, null); + public Borders() { + super(); // String name, String description, String usageMessage, List aliases) this.addCommand(new BukkitCommand( "worldborder", @@ -45,7 +45,7 @@ public class Borders extends Feature { ChatColor.YELLOW + ChatColor.GRAY + " (world) (key) [value]"); return true; } - World w = plugin.getServer().getWorld(args[0]); + World w = Bukkit.getServer().getWorld(args[0]); if (w == null) { sender.sendMessage(ChatColor.RED + "Invalid world!"); return true; @@ -110,7 +110,7 @@ public class Borders extends Feature { String lastWord = args[args.length - 1]; if (args.length == 1) { - for (World w : plugin.getServer().getWorlds()) { + for (World w : Bukkit.getServer().getWorlds()) { if (StringUtil.startsWithIgnoreCase(w.getName(), lastWord)) hints.add(w.getName()); } @@ -148,7 +148,7 @@ public class Borders extends Feature { } public void loadWorlds() { - for(World w : plugin.getServer().getWorlds()) { + for(World w : Bukkit.getServer().getWorlds()) { final String wname = w.getName(); if (!cfg.contains(wname)) { w.getWorldBorder().reset(); diff --git a/src/cz/marwland/mc/essentials/features/ChatNotifier.java b/src/cz/marwland/mc/essentials/features/ChatNotifier.java index 838495a..faa4267 100644 --- a/src/cz/marwland/mc/essentials/features/ChatNotifier.java +++ b/src/cz/marwland/mc/essentials/features/ChatNotifier.java @@ -1,5 +1,6 @@ package cz.marwland.mc.essentials.features; +import org.bukkit.Bukkit; import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -7,19 +8,14 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; import com.connorlinfoot.bountifulapi.BountifulAPI; -import cz.marwland.mc.core.MarwCore; import cz.marwland.mc.core.features.Feature; import net.md_5.bungee.api.ChatColor; public class ChatNotifier extends Feature { - public ChatNotifier(MarwCore plugin) { - super(plugin, null); - } - @EventHandler public void onPlayerChat(AsyncPlayerChatEvent e) { - for (Player p : plugin.getServer().getOnlinePlayers()) { + for (Player p : Bukkit.getServer().getOnlinePlayers()) { String msg = e.getMessage(); if (e.getMessage().toLowerCase().contains(p.getName().toLowerCase()) && p.getPlayer() != e.getPlayer()) { e.getRecipients().remove(p); @@ -30,4 +26,5 @@ public class ChatNotifier extends Feature { p.sendMessage(String.format(e.getFormat(), e.getPlayer().getDisplayName(), msg)); } } + }