APIv4, support for spigot 1.12 (and up) #6
@ -1,14 +0,0 @@
|
||||
package com.redstoner.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/** Debugable annotation, to be added to methods that invoke the Debugger.notifyMethod method for debugging purposes.
|
||||
*
|
||||
* @author Pepich */
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Debugable
|
||||
{}
|
@ -1,201 +0,0 @@
|
||||
package com.redstoner.coremods.debugger;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import com.nemez.cmdmgr.Command;
|
||||
import com.nemez.cmdmgr.CommandManager;
|
||||
import com.redstoner.annotations.Debugable;
|
||||
import com.redstoner.annotations.Version;
|
||||
import com.redstoner.misc.Main;
|
||||
import com.redstoner.misc.Utils;
|
||||
import com.redstoner.modules.CoreModule;
|
||||
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
/** The Debugger class, first Module to be loaded, responsible for debug interactions such as subscribing to method calls and getting field values on runtime.
|
||||
*
|
||||
* @author Pepich */
|
||||
@Version(major = 3, minor = 0, revision = 0, compatible = -1)
|
||||
public final class Debugger implements CoreModule, Listener
|
||||
{
|
||||
private static Debugger instance;
|
||||
private static HashMap<CommandSender, ArrayList<String>> subs;
|
||||
private static final boolean enabled = true;
|
||||
|
||||
private Debugger()
|
||||
{
|
||||
subs = new HashMap<>();
|
||||
}
|
||||
|
||||
public static void init()
|
||||
{
|
||||
if (instance == null)
|
||||
instance = new Debugger();
|
||||
CommandManager.registerCommand(instance.getCommandString(), instance, Main.plugin);
|
||||
}
|
||||
|
||||
public static void notifyMethod(CommandSender recipient, Object... params)
|
||||
{
|
||||
Exception e = new Exception();
|
||||
String method = e.getStackTrace()[1].getMethodName();
|
||||
if (!method.equals("notifyMethod"))
|
||||
notifyMethod((Object) recipient, params);
|
||||
String classname = e.getStackTrace()[1].getClassName();
|
||||
if (!classname.equals("com.redstoner.coremods.debugger.Debugger"))
|
||||
notifyMethod((Object) recipient, params);
|
||||
for (StackTraceElement element : e.getStackTrace())
|
||||
{
|
||||
if (element.getMethodName().equals("notifyMethod"))
|
||||
continue;
|
||||
classname = element.getClassName();
|
||||
method = element.getMethodName();
|
||||
break;
|
||||
}
|
||||
boolean subscribed = false;
|
||||
for (String s : subs.get(recipient))
|
||||
{
|
||||
if (s.equals(classname + "." + method))
|
||||
{
|
||||
subscribed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (subscribed)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder("&7");
|
||||
sb.append(method);
|
||||
sb.append("(");
|
||||
if (params != null)
|
||||
{
|
||||
for (Object obj : params)
|
||||
{
|
||||
if (obj == null)
|
||||
sb.append("&cNULL");
|
||||
else
|
||||
sb.append(obj.toString());
|
||||
sb.append("&7, &e");
|
||||
}
|
||||
sb.delete(sb.length() - 6, sb.length());
|
||||
}
|
||||
sb.append("&7)\n&eTypes:\n&7");
|
||||
int i = 0;
|
||||
for (Object obj : params)
|
||||
sb.append(i++ + ": &e" + (obj == null ? "&cNULL" : obj.getClass().getName()) + "&7\n");
|
||||
String message = "&2---=[ DEBUGGER ]=---\n" + sb.toString();
|
||||
message = ChatColor.translateAlternateColorCodes('&', message);
|
||||
recipient.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void notifyMethod(Object... params)
|
||||
{
|
||||
if (!enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
for (Player p : Bukkit.getOnlinePlayers())
|
||||
if (subs.containsKey(p))
|
||||
notifyMethod(p, params);
|
||||
CommandSender p = Bukkit.getConsoleSender();
|
||||
if (subs.containsKey(p))
|
||||
notifyMethod(p, params);
|
||||
}
|
||||
|
||||
// @noformat
|
||||
@Override
|
||||
public String getCommandString()
|
||||
{
|
||||
return "command debugger {\n" +
|
||||
" subscribe [string:classname] [string:methodname] {\n" +
|
||||
" help Subscribes to all calls of the corresponding debugable method.;\n" +
|
||||
" perm jutils.debugger.subscribe;\n" +
|
||||
" run subscribe classname methodname;\n" +
|
||||
" }\n" +
|
||||
" unsubscribe [string:classname] [string:methodname] {\n" +
|
||||
" help Unsubscribes from all calls of the corresponding debugable method.;\n" +
|
||||
" perm jutils.debugger.subscribe;\n" +
|
||||
" run unsubscribe classname methodname;\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
}
|
||||
// @format
|
||||
|
||||
@Command(hook = "subscribe")
|
||||
@Debugable
|
||||
public boolean subscribeCommand(CommandSender sender, String classname, String methodname)
|
||||
{
|
||||
if (!enabled)
|
||||
{
|
||||
Utils.sendMessage(sender, null, "Debugger is currently disabled!");
|
||||
return true;
|
||||
}
|
||||
Class<?> clazz = null;
|
||||
try
|
||||
{
|
||||
clazz = Class.forName(classname);
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
Utils.sendErrorMessage(sender, null, "Could not find the class: " + classname);
|
||||
return true;
|
||||
}
|
||||
boolean found = false;
|
||||
for (Method m : clazz.getMethods())
|
||||
{
|
||||
if (m.getName().matches(methodname))
|
||||
{
|
||||
if (m.isAnnotationPresent(Debugable.class))
|
||||
{
|
||||
found = true;
|
||||
if (!subs.containsKey(sender))
|
||||
subs.put(sender, new ArrayList<String>());
|
||||
subs.get(sender).add(classname + "." + methodname);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
Utils.sendErrorMessage(sender, null, "The method you chose either doesn't exist or is not debugable!");
|
||||
return true;
|
||||
}
|
||||
Utils.sendMessage(sender, null, "Successfully subsribed to the method &e" + classname + ":" + methodname, '&');
|
||||
return true;
|
||||
}
|
||||
|
||||
@Command(hook = "unsubscribe")
|
||||
@Debugable
|
||||
public boolean unsubscribeCommand(CommandSender sender, String classname, String methodname)
|
||||
{
|
||||
if (!enabled)
|
||||
{
|
||||
Utils.sendMessage(sender, null, "Debugger is currently disabled!");
|
||||
return true;
|
||||
}
|
||||
if (subs.containsKey(sender))
|
||||
{
|
||||
if (subs.get(sender).remove(classname + "." + methodname))
|
||||
{
|
||||
Utils.sendMessage(sender, null,
|
||||
"Successfully unsubscribed from the method &e" + classname + ":" + methodname, '&');
|
||||
}
|
||||
else
|
||||
{
|
||||
Utils.sendErrorMessage(sender, null, "You were not listening to &e" + classname + ":" + methodname,
|
||||
'&');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Utils.sendErrorMessage(sender, null, "You are not listening to any methods!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -24,9 +24,7 @@ import com.nemez.cmdmgr.Command.AsyncType;
|
||||
import com.nemez.cmdmgr.CommandManager;
|
||||
import com.redstoner.annotations.AutoRegisterListener;
|
||||
import com.redstoner.annotations.Commands;
|
||||
import com.redstoner.annotations.Debugable;
|
||||
import com.redstoner.annotations.Version;
|
||||
import com.redstoner.coremods.debugger.Debugger;
|
||||
import com.redstoner.misc.Main;
|
||||
import com.redstoner.misc.Utils;
|
||||
import com.redstoner.misc.VersionHelper;
|
||||
@ -153,45 +151,9 @@ public final class ModuleLoader implements CoreModule
|
||||
enableModules();
|
||||
}
|
||||
|
||||
/** This method will add a module to the module list, without enabling it.</br>
|
||||
* This method is deprecated, use addDynamicModule(String name) instead. When using this method, dynamic reloading of the module will not be supported.
|
||||
*
|
||||
* @param clazz The class of the module to be added. */
|
||||
@Debugable
|
||||
@Deprecated
|
||||
public static final void addModule(Class<? extends Module> clazz)
|
||||
{
|
||||
Debugger.notifyMethod(clazz);
|
||||
try
|
||||
{
|
||||
Module module = clazz.newInstance();
|
||||
modules.put(module, false);
|
||||
}
|
||||
catch (InstantiationException | IllegalAccessException e)
|
||||
{
|
||||
Utils.error("Could not add " + clazz.getName() + " to the list, constructor not accessible.");
|
||||
}
|
||||
}
|
||||
|
||||
@Debugable
|
||||
private static final void addLoadedModule(Module m)
|
||||
{
|
||||
Debugger.notifyMethod(m);
|
||||
if (modules.containsKey(m))
|
||||
if (modules.get(m))
|
||||
{
|
||||
Utils.error(
|
||||
"Module m was already loaded and enabled. Disable the module before attempting to reload it.");
|
||||
return;
|
||||
}
|
||||
modules.put(m, false);
|
||||
}
|
||||
|
||||
/** Call this to enable all not-yet enabled modules that are known to the loader. */
|
||||
@Debugable
|
||||
public static final void enableModules()
|
||||
{
|
||||
Debugger.notifyMethod();
|
||||
for (Module module : modules.keySet())
|
||||
{
|
||||
if (modules.get(module))
|
||||
@ -205,11 +167,9 @@ public final class ModuleLoader implements CoreModule
|
||||
*
|
||||
* @param clazz The class of the module to be enabled.
|
||||
* @return true, when the module was successfully enabled. */
|
||||
@Debugable
|
||||
@Deprecated
|
||||
public static final boolean enableModule(Class<? extends Module> clazz)
|
||||
{
|
||||
Debugger.notifyMethod(clazz);
|
||||
for (Module module : modules.keySet())
|
||||
{
|
||||
if (module.getClass().equals(clazz))
|
||||
@ -410,7 +370,7 @@ public final class ModuleLoader implements CoreModule
|
||||
{
|
||||
Utils.info(
|
||||
"Found existing module, attempting override. WARNING! This operation will halt the main thread until it is completed.");
|
||||
Utils.info("Attempting to load new class definition before disabling and removing the old module:");
|
||||
Utils.info("Attempting to load new class definition before disabling and removing the old module");
|
||||
boolean differs = false;
|
||||
Utils.info("Old class definition: Class@" + m.getClass().hashCode());
|
||||
ClassLoader delegateParent = mainLoader.getParent();
|
||||
@ -438,7 +398,10 @@ public final class ModuleLoader implements CoreModule
|
||||
}
|
||||
if (!differs)
|
||||
{
|
||||
Utils.warn("New class definition equals old definition, are you sure you did everything right?");
|
||||
if (!debugMode)
|
||||
{
|
||||
Utils.warn(
|
||||
"New class definition equals old definition, are you sure you did everything right?");
|
||||
Utils.info("Aborting now...");
|
||||
try
|
||||
{
|
||||
@ -450,6 +413,11 @@ public final class ModuleLoader implements CoreModule
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
Utils.warn(
|
||||
"New class definition equals old definition, but debugMode is enabled. Loading anyways.");
|
||||
}
|
||||
else
|
||||
Utils.info("Found new class definition, attempting to instantiate:");
|
||||
Module module = null;
|
||||
try
|
||||
@ -470,12 +438,14 @@ public final class ModuleLoader implements CoreModule
|
||||
}
|
||||
return;
|
||||
}
|
||||
Utils.info("Instantiated new class definition, checking versions:");
|
||||
Utils.info("Instantiated new class definition, checking versions");
|
||||
Version oldVersion = m.getClass().getAnnotation(Version.class);
|
||||
Utils.info("Current version: " + VersionHelper.getString(oldVersion));
|
||||
Version newVersion = module.getClass().getAnnotation(Version.class);
|
||||
Utils.info("Version of remote class: " + VersionHelper.getString(newVersion));
|
||||
if (oldVersion.equals(newVersion))
|
||||
{
|
||||
if (!debugMode)
|
||||
{
|
||||
Utils.error("Detected equal module versions, " + (debugMode
|
||||
? " aborting now... Set debugMode to true in your config if you want to continue!"
|
||||
@ -494,9 +464,12 @@ public final class ModuleLoader implements CoreModule
|
||||
}
|
||||
}
|
||||
else
|
||||
Utils.info("Versions differ, disabling old module:");
|
||||
Utils.warn("New version equals old version, but debugMode is enabled. Loading anyways.");
|
||||
}
|
||||
else
|
||||
Utils.info("Versions differ, disabling old module");
|
||||
disableModule(m);
|
||||
Utils.info("Disabled module, overriding the implementation:");
|
||||
Utils.info("Disabled module, overriding the implementation");
|
||||
modules.remove(m);
|
||||
try
|
||||
{
|
||||
@ -518,15 +491,27 @@ public final class ModuleLoader implements CoreModule
|
||||
{
|
||||
Class<?> clazz = mainLoader.loadClass(name);
|
||||
Module module = (Module) clazz.newInstance();
|
||||
addLoadedModule(module);
|
||||
modules.put(module, false);
|
||||
enableLoadedModule(module);
|
||||
}
|
||||
catch (ClassNotFoundException | InstantiationException | IllegalAccessException e)
|
||||
{
|
||||
if (name.endsWith(".class"))
|
||||
{
|
||||
Utils.warn(
|
||||
"Couldn't find class definition, but path ends with .class -> Attempting again with removed file suffix.");
|
||||
addDynamicModule(name.replaceAll(".class$", ""));
|
||||
}
|
||||
if (!name.contains("."))
|
||||
{
|
||||
Utils.warn(
|
||||
"Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of path by adding a packet name and trying again.");
|
||||
addDynamicModule(name.toLowerCase() + "." + name);
|
||||
}
|
||||
if (!name.startsWith("com.redstoner.modules."))
|
||||
{
|
||||
Utils.warn(
|
||||
"Couldn't find class definition, suspecting missing path. Autocompleting path, trying again.");
|
||||
"Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of packet name and trying again.");
|
||||
addDynamicModule("com.redstoner.modules." + name);
|
||||
}
|
||||
else
|
||||
@ -551,11 +536,27 @@ public final class ModuleLoader implements CoreModule
|
||||
}
|
||||
if (!name.startsWith("com.redstoner.modules."))
|
||||
{
|
||||
Utils.warn("Couldn't find class definition, suspecting missing path. Autocompleting path, trying again.");
|
||||
removeDynamicModule("com.redstoner.modules." + name);
|
||||
if (name.endsWith(".class"))
|
||||
{
|
||||
Utils.warn(
|
||||
"Couldn't find class definition, but path ends with .class -> Attempting again with removed file suffix.");
|
||||
addDynamicModule(name.replaceAll(".class$", ""));
|
||||
}
|
||||
if (!name.contains("."))
|
||||
{
|
||||
Utils.warn(
|
||||
"Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of path by adding a packet name and trying again.");
|
||||
addDynamicModule(name.toLowerCase() + "." + name);
|
||||
}
|
||||
if (!name.startsWith("com.redstoner.modules."))
|
||||
{
|
||||
Utils.warn(
|
||||
"Couldn't find class definition, suspecting incomplete path. Attempting autocompletion of packet name and trying again.");
|
||||
addDynamicModule("com.redstoner.modules." + name);
|
||||
}
|
||||
}
|
||||
else
|
||||
Utils.error("Couldn't find module! Couldn't ");
|
||||
Utils.error("Couldn't find module! Couldn't disable nonexisting module!");
|
||||
}
|
||||
|
||||
/** Finds a module by name for other modules to reference it.
|
||||
@ -569,4 +570,16 @@ public final class ModuleLoader implements CoreModule
|
||||
return m;
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Finds a module by name for other modules to reference it.
|
||||
*
|
||||
* @param name the name of the module. Use the full path if you are not sure about the module's SimpleClassName being unique.
|
||||
* @return the instance of the module or @null it none could be found */
|
||||
public static boolean exists(String name)
|
||||
{
|
||||
for (Module m : modules.keySet())
|
||||
if (m.getClass().getSimpleName().equals(name) || m.getClass().getName().equals(name))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package com.redstoner.misc;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.redstoner.annotations.Version;
|
||||
import com.redstoner.coremods.debugger.Debugger;
|
||||
import com.redstoner.coremods.moduleLoader.ModuleLoader;
|
||||
import com.redstoner.misc.mysql.MysqlHandler;
|
||||
|
||||
@ -20,7 +19,6 @@ public class Main extends JavaPlugin
|
||||
{
|
||||
plugin = this;
|
||||
// Configger.init();
|
||||
Debugger.init();
|
||||
MysqlHandler.init();
|
||||
ModuleLoader.init();
|
||||
// Load modules from config
|
||||
|
@ -9,14 +9,12 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.redstoner.annotations.Debugable;
|
||||
import com.redstoner.annotations.Version;
|
||||
import com.redstoner.coremods.debugger.Debugger;
|
||||
|
||||
/** The utils class containing utility functions. Those include but are not limited to sending formatted messages, broadcasts and more.
|
||||
*
|
||||
* @author Pepich */
|
||||
@Version(major = 4, minor = 0, revision = 2, compatible = 1)
|
||||
@Version(major = 4, minor = 0, revision = 0, compatible = 1)
|
||||
public final class Utils
|
||||
{
|
||||
/** The @SimpleDateFormat used for getting the current date. */
|
||||
@ -30,7 +28,6 @@ public final class Utils
|
||||
*
|
||||
* @param recipient Whom to sent the message to.
|
||||
* @param message The message to sent. Will default to &7 (light_grey) if not specified otherwise. */
|
||||
@Debugable
|
||||
public static void sendMessage(CommandSender recipient, String message)
|
||||
{
|
||||
sendMessage(recipient, null, message);
|
||||
@ -40,7 +37,6 @@ public final class Utils
|
||||
*
|
||||
* @param recipient Whom to sent the message to.
|
||||
* @param message The message to sent. Will default to &7 (light_grey) if not specified otherwise. */
|
||||
@Debugable
|
||||
public static void sendErrorMessage(CommandSender recipient, String message)
|
||||
{
|
||||
sendErrorMessage(recipient, null, message);
|
||||
@ -51,10 +47,8 @@ public final class Utils
|
||||
* @param recipient Whom to sent the message to.
|
||||
* @param prefix The prefix for the message. If null, the default prefix will be used: &8[&2MODULE&8]
|
||||
* @param message The message to sent. Will default to &7 (light_grey) if not specified otherwise. */
|
||||
@Debugable
|
||||
public static void sendMessage(CommandSender recipient, String prefix, String message)
|
||||
{
|
||||
Debugger.notifyMethod((Object) recipient, prefix, message);
|
||||
if (prefix == null)
|
||||
prefix = "§8[§2" + getCaller() + "§8]: ";
|
||||
recipient.sendMessage(prefix + "§7" + message);
|
||||
@ -65,10 +59,8 @@ public final class Utils
|
||||
* @param recipient Whom to sent the message to.
|
||||
* @param prefix The prefix for the message. If null, the default prefix will be used: &8[&cMODULE&8]
|
||||
* @param message The message to sent. Will default to &7 (light_grey) if not specified otherwise. */
|
||||
@Debugable
|
||||
public static void sendErrorMessage(CommandSender recipient, String prefix, String message)
|
||||
{
|
||||
Debugger.notifyMethod((Object) recipient, prefix, message);
|
||||
if (prefix == null)
|
||||
prefix = "§8[§c" + getCaller() + "§8]: ";
|
||||
recipient.sendMessage(prefix + "§7" + message);
|
||||
@ -126,7 +118,6 @@ public final class Utils
|
||||
* Write a class implementing the interface and pass it to this method, the "sendTo()" method will be called for each recipient.
|
||||
* @param logmessage the log message to appear in console. Set to null to not log this (you can still log the original message by returning true in the filter).
|
||||
* @return the amount of people that received the message. */
|
||||
@Debugable
|
||||
public static int broadcast(String prefix, String message, BroadcastFilter filter)
|
||||
{
|
||||
if (prefix == null)
|
||||
@ -168,10 +159,8 @@ public final class Utils
|
||||
/** Prints an info message into console. Supports "&" color codes.
|
||||
*
|
||||
* @param message The message to be put into console. Prefixes are automatically generated. Color defaults to grey. */
|
||||
@Debugable
|
||||
public static void info(String message)
|
||||
{
|
||||
Debugger.notifyMethod(message);
|
||||
String classname = getCaller();
|
||||
String prefix = "§8[§2" + classname + "§8]: ";
|
||||
Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
|
||||
@ -180,10 +169,8 @@ public final class Utils
|
||||
/** Prints a warning message into console. Supports "&" color codes.
|
||||
*
|
||||
* @param message The message to be put into console. Prefixes are automatically generated. Color defaults to grey. */
|
||||
@Debugable
|
||||
public static void warn(String message)
|
||||
{
|
||||
Debugger.notifyMethod(message);
|
||||
String classname = getCaller();
|
||||
String prefix = "§e[WARN]: §8[§e" + classname + "§8]: ";
|
||||
Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
|
||||
@ -192,10 +179,8 @@ public final class Utils
|
||||
/** Used to make an error output to console. Supports "&" color codes.
|
||||
*
|
||||
* @param message The message to be put into console. Prefixes are automatically generated. Color defaults to red. */
|
||||
@Debugable
|
||||
public static void error(String message)
|
||||
{
|
||||
Debugger.notifyMethod(message);
|
||||
String classname = getCaller();
|
||||
String prefix = "§c[ERROR]: §8[§c" + classname + "§8]: ";
|
||||
Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
|
||||
|
Reference in New Issue
Block a user