I know this question has been asked a million times, but I can't find a decent answer.
I have a bunch of messenger methods like debug(String) and tell(CommandSender, String) that I want to use a variety of classes. It sounds simple and easy, but Java's lack of support for multiple inheritance has made the problem incredibly difficult for classes that already have to extend something else.
Interfaces won't work as far as I can tell because interfaces don't allow you to actually implement the methods that you put in them.
I have considered adding extra arguments to the method to make them work from a static utilities class, but this makes calls to the methods go from debug(String) to MessageUtil.debug(Plugin, String), which is far more bulky than I would like.
I have considered making a Messenger object that can handle these messages so that I can call messenger.debug(String), which is better, but I can't shake the feeling that there must be a better way to get debug(String) alone.
Any advice?
EDIT: These methods, unfortunately, cannot be made static without adding extra parameters; therefore, static imports will not work.
EDIT: Here is an example of one of the methods that I'm trying to use in multiple classes. As you can see by its use of non-static global variables like "plugin" and "debuggers", it cannot be made static.
protected myPlugin plugin;
private myList<String> debuggers = new myList<String>();
public void debug(String message) {
if (debuggers.size() == 0)
return;
if (debuggers.contains("\\console")) {
plugin.getServer().getConsoleSender().sendMessage(plugin.getColor() + message);
if (debuggers.size() == 1)
return;
}
for (Player player : plugin.getServer().getOnlinePlayers())
if (debuggers.contains(player.getName()))
player.sendMessage(plugin.getColor() + message);
}
Here's another example that boradcasts a message to every Player on the server and to the console using the same global variables as the one above:
public void broadcast(String message) {
for (Player player : mCL.getServer().getOnlinePlayers())
player.sendMessage(plugin.getColor() + message);
mCL.getServer().getConsoleSender().sendMessage(message);
}
EDIT: The broadcast() method above actually isn't static; that was a copy-paste error. I have modified it to reflect that.
broadcast()could be import-static as is. Yourdebug()method appears to have quite a lot of state. Pass theMessageUtilinstance to your callers, or use a Singleton.