singleton goodies

This commit is contained in:
Erik Bročko 2018-09-08 18:04:15 +02:00
parent 58f9a4d349
commit 094f354086
6 changed files with 32 additions and 45 deletions

View File

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

View File

@ -15,17 +15,19 @@ public class MarwCore extends JavaPlugin {
private ConfigManager configManager;
private HashMap<String, Feature> 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;
}
}

View File

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

View File

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

View File

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

View File

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