singleton goodies
This commit is contained in:
parent
58f9a4d349
commit
094f354086
@ -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() + "!");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -15,17 +15,19 @@ public class MarwCore extends JavaPlugin {
|
|||||||
|
|
||||||
private ConfigManager configManager;
|
private ConfigManager configManager;
|
||||||
private HashMap<String, Feature> features = new HashMap<>();
|
private HashMap<String, Feature> features = new HashMap<>();
|
||||||
|
private static MarwCore INSTANCE = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
INSTANCE = this;
|
||||||
//getServer().getPluginManager().registerEvents(new EventListener(this), this);
|
//getServer().getPluginManager().registerEvents(new EventListener(this), this);
|
||||||
|
|
||||||
configManager = new ConfigManager(this);
|
configManager = new ConfigManager(this);
|
||||||
this.addFeature(new Base(this));
|
this.addFeature(new Base());
|
||||||
|
|
||||||
this.addFeature(new Borders(this));
|
this.addFeature(new Borders());
|
||||||
this.addFeature(new ChatNotifier(this));
|
this.addFeature(new ChatNotifier());
|
||||||
this.addFeature(new RandomTeleport(this));
|
this.addFeature(new RandomTeleport());
|
||||||
|
|
||||||
configManager.load();
|
configManager.load();
|
||||||
this.features.forEach((k, v) -> v.onEnable());
|
this.features.forEach((k, v) -> v.onEnable());
|
||||||
@ -63,4 +65,8 @@ public class MarwCore extends JavaPlugin {
|
|||||||
return "marw.core";
|
return "marw.core";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static MarwCore getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import org.bukkit.command.CommandSender;
|
|||||||
import org.bukkit.command.defaults.BukkitCommand;
|
import org.bukkit.command.defaults.BukkitCommand;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
|
||||||
import cz.marwland.mc.core.MarwCore;
|
|
||||||
import cz.marwland.mc.core.features.Feature;
|
import cz.marwland.mc.core.features.Feature;
|
||||||
|
|
||||||
|
|
||||||
@ -14,10 +13,10 @@ public class Base extends Feature {
|
|||||||
|
|
||||||
FileConfiguration cfg;
|
FileConfiguration cfg;
|
||||||
|
|
||||||
public Base(MarwCore plugin) {
|
public Base() {
|
||||||
super(plugin, "marw");
|
super("marw");
|
||||||
// String name, String description, String usageMessage, List<String> aliases)
|
// 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
|
@Override
|
||||||
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
|
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
|
||||||
if (args.length < 1) {
|
if (args.length < 1) {
|
||||||
|
@ -4,6 +4,7 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandMap;
|
import org.bukkit.command.CommandMap;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.defaults.BukkitCommand;
|
import org.bukkit.command.defaults.BukkitCommand;
|
||||||
@ -14,33 +15,36 @@ import cz.marwland.mc.core.MarwCore;
|
|||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
|
|
||||||
public abstract class Feature implements Listener {
|
public abstract class Feature implements Listener {
|
||||||
protected final MarwCore plugin;
|
protected final MarwCore plugin = MarwCore.getInstance();
|
||||||
private String name = "";
|
private String name = "";
|
||||||
private ArrayList<BukkitCommand> commands = new ArrayList<>();
|
private ArrayList<BukkitCommand> commands = new ArrayList<>();
|
||||||
private static CommandMap cmap;
|
private static CommandMap cmap;
|
||||||
|
|
||||||
public Feature(MarwCore plugin, String name) {
|
public Feature(String name) {
|
||||||
this.plugin = plugin;
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
this.name = this.getClass().getSimpleName().toLowerCase();
|
this.name = this.getClass().getSimpleName().toLowerCase();
|
||||||
}
|
}
|
||||||
Method commandMap;
|
Method commandMap;
|
||||||
try {
|
try {
|
||||||
commandMap = plugin.getServer().getClass().getMethod("getCommandMap");
|
commandMap = Bukkit.getServer().getClass().getMethod("getCommandMap");
|
||||||
cmap = (CommandMap) commandMap.invoke(plugin.getServer());
|
cmap = (CommandMap) commandMap.invoke(Bukkit.getServer());
|
||||||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Feature() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
if (cmap != null) {
|
if (cmap != null) {
|
||||||
for (BukkitCommand cmd : commands) {
|
for (BukkitCommand cmd : commands) {
|
||||||
cmap.register(plugin.getName().toLowerCase(), cmd);
|
cmap.register(plugin.getName().toLowerCase(), cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
|
@ -5,6 +5,7 @@ import java.util.Arrays;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.defaults.BukkitCommand;
|
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.StringUtil;
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
|
|
||||||
import cz.marwland.mc.core.MarwCore;
|
|
||||||
import cz.marwland.mc.core.config.ConfigEntry;
|
import cz.marwland.mc.core.config.ConfigEntry;
|
||||||
import cz.marwland.mc.core.config.EntryManager;
|
import cz.marwland.mc.core.config.EntryManager;
|
||||||
import cz.marwland.mc.core.config.IValueExecuter;
|
import cz.marwland.mc.core.config.IValueExecuter;
|
||||||
@ -25,8 +25,8 @@ public class Borders extends Feature {
|
|||||||
FileConfiguration cfg;
|
FileConfiguration cfg;
|
||||||
EntryManager emgr = new EntryManager();
|
EntryManager emgr = new EntryManager();
|
||||||
|
|
||||||
public Borders(MarwCore plugin) {
|
public Borders() {
|
||||||
super(plugin, null);
|
super();
|
||||||
// String name, String description, String usageMessage, List<String> aliases)
|
// String name, String description, String usageMessage, List<String> aliases)
|
||||||
this.addCommand(new BukkitCommand(
|
this.addCommand(new BukkitCommand(
|
||||||
"worldborder",
|
"worldborder",
|
||||||
@ -45,7 +45,7 @@ public class Borders extends Feature {
|
|||||||
ChatColor.YELLOW + ChatColor.GRAY + " (world) (key) [value]");
|
ChatColor.YELLOW + ChatColor.GRAY + " (world) (key) [value]");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
World w = plugin.getServer().getWorld(args[0]);
|
World w = Bukkit.getServer().getWorld(args[0]);
|
||||||
if (w == null) {
|
if (w == null) {
|
||||||
sender.sendMessage(ChatColor.RED + "Invalid world!");
|
sender.sendMessage(ChatColor.RED + "Invalid world!");
|
||||||
return true;
|
return true;
|
||||||
@ -110,7 +110,7 @@ public class Borders extends Feature {
|
|||||||
|
|
||||||
String lastWord = args[args.length - 1];
|
String lastWord = args[args.length - 1];
|
||||||
if (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))
|
if (StringUtil.startsWithIgnoreCase(w.getName(), lastWord))
|
||||||
hints.add(w.getName());
|
hints.add(w.getName());
|
||||||
}
|
}
|
||||||
@ -148,7 +148,7 @@ public class Borders extends Feature {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void loadWorlds() {
|
public void loadWorlds() {
|
||||||
for(World w : plugin.getServer().getWorlds()) {
|
for(World w : Bukkit.getServer().getWorlds()) {
|
||||||
final String wname = w.getName();
|
final String wname = w.getName();
|
||||||
if (!cfg.contains(wname)) {
|
if (!cfg.contains(wname)) {
|
||||||
w.getWorldBorder().reset();
|
w.getWorldBorder().reset();
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package cz.marwland.mc.essentials.features;
|
package cz.marwland.mc.essentials.features;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Sound;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@ -7,19 +8,14 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
|
|||||||
|
|
||||||
import com.connorlinfoot.bountifulapi.BountifulAPI;
|
import com.connorlinfoot.bountifulapi.BountifulAPI;
|
||||||
|
|
||||||
import cz.marwland.mc.core.MarwCore;
|
|
||||||
import cz.marwland.mc.core.features.Feature;
|
import cz.marwland.mc.core.features.Feature;
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
|
|
||||||
public class ChatNotifier extends Feature {
|
public class ChatNotifier extends Feature {
|
||||||
|
|
||||||
public ChatNotifier(MarwCore plugin) {
|
|
||||||
super(plugin, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerChat(AsyncPlayerChatEvent e) {
|
public void onPlayerChat(AsyncPlayerChatEvent e) {
|
||||||
for (Player p : plugin.getServer().getOnlinePlayers()) {
|
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
|
||||||
String msg = e.getMessage();
|
String msg = e.getMessage();
|
||||||
if (e.getMessage().toLowerCase().contains(p.getName().toLowerCase()) && p.getPlayer() != e.getPlayer()) {
|
if (e.getMessage().toLowerCase().contains(p.getName().toLowerCase()) && p.getPlayer() != e.getPlayer()) {
|
||||||
e.getRecipients().remove(p);
|
e.getRecipients().remove(p);
|
||||||
@ -30,4 +26,5 @@ public class ChatNotifier extends Feature {
|
|||||||
p.sendMessage(String.format(e.getFormat(), e.getPlayer().getDisplayName(), msg));
|
p.sendMessage(String.format(e.getFormat(), e.getPlayer().getDisplayName(), msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user