singleton goodies

This commit is contained in:
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() {