Move to Master #8
@ -5,7 +5,6 @@ import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
@ -28,6 +27,7 @@ import com.nemez.cmdmgr.CommandManager;
|
||||
import com.redstoner.annotations.AutoRegisterListener;
|
||||
import com.redstoner.annotations.Commands;
|
||||
import com.redstoner.annotations.Version;
|
||||
import com.redstoner.logging.PrivateLogManager;
|
||||
import com.redstoner.misc.Main;
|
||||
import com.redstoner.misc.ModuleInfo;
|
||||
import com.redstoner.misc.VersionHelper;
|
||||
@ -40,7 +40,7 @@ import net.nemez.chatapi.click.Message;
|
||||
/** The module loader, mother of all modules. Responsible for loading and taking care of all modules.
|
||||
*
|
||||
* @author Pepich */
|
||||
@Version(major = 5, minor = 0, revision = 0, compatible = 5)
|
||||
@Version(major = 5, minor = 2, revision = 0, compatible = 5)
|
||||
public final class ModuleLoader implements CoreModule
|
||||
{
|
||||
private static ModuleLoader instance;
|
||||
@ -370,8 +370,8 @@ public final class ModuleLoader implements CoreModule
|
||||
{
|
||||
HandlerList.unregisterAll((Listener) module);
|
||||
}
|
||||
String[] commands = getAllHooks(module).toArray(new String[] {});
|
||||
CommandManager.unregisterAll(commands);
|
||||
CommandManager.unregisterAllWithFallback(module.getClass().getSimpleName());
|
||||
PrivateLogManager.unregister(module);
|
||||
try
|
||||
{
|
||||
URLClassLoader loader = loaders.get(module);
|
||||
@ -387,19 +387,6 @@ public final class ModuleLoader implements CoreModule
|
||||
}
|
||||
}
|
||||
|
||||
private static ArrayList<String> getAllHooks(Module module)
|
||||
{
|
||||
ArrayList<String> commands = new ArrayList<>();
|
||||
for (Method m : module.getClass().getMethods())
|
||||
{
|
||||
Command cmd = m.getDeclaredAnnotation(Command.class);
|
||||
if (cmd == null)
|
||||
continue;
|
||||
commands.add(cmd.hook());
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
|
||||
@Command(hook = "load")
|
||||
public boolean loadModule(CommandSender sender, String name)
|
||||
{
|
||||
|
54
src/main/java/com/redstoner/logging/Log4JFilter.java
Normal file
54
src/main/java/com/redstoner/logging/Log4JFilter.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.redstoner.logging;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.Marker;
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.Logger;
|
||||
import org.apache.logging.log4j.core.filter.AbstractFilter;
|
||||
import org.apache.logging.log4j.message.Message;
|
||||
|
||||
public class Log4JFilter extends AbstractFilter {
|
||||
|
||||
private static final long serialVersionUID = -5594073755007974254L;
|
||||
|
||||
private static Result validateMessage(Message message) {
|
||||
if (message == null) {
|
||||
return Result.NEUTRAL;
|
||||
}
|
||||
return validateMessage(message.getFormattedMessage());
|
||||
}
|
||||
|
||||
private static Result validateMessage(String message) {
|
||||
return PrivateLogManager.isHidden(message)
|
||||
? Result.DENY
|
||||
: Result.NEUTRAL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(LogEvent event) {
|
||||
Message candidate = null;
|
||||
if (event != null) {
|
||||
candidate = event.getMessage();
|
||||
}
|
||||
return validateMessage(candidate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) {
|
||||
return validateMessage(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, String msg, Object... params) {
|
||||
return validateMessage(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) {
|
||||
String candidate = null;
|
||||
if (msg != null) {
|
||||
candidate = msg.toString();
|
||||
}
|
||||
return validateMessage(candidate);
|
||||
}
|
||||
}
|
89
src/main/java/com/redstoner/logging/PrivateLogManager.java
Normal file
89
src/main/java/com/redstoner/logging/PrivateLogManager.java
Normal file
@ -0,0 +1,89 @@
|
||||
package com.redstoner.logging;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
import com.redstoner.misc.Utils;
|
||||
import com.redstoner.modules.Module;
|
||||
import com.redstoner.modules.ModuleLogger;
|
||||
|
||||
public class PrivateLogManager {
|
||||
|
||||
private static Map<String, Module> registrar = new HashMap<>();
|
||||
private static Map<String, String> commands = new HashMap<>();
|
||||
|
||||
private static final String ISSUED_COMMAND_TEXT = "issued server command: /";
|
||||
private static final int ISSUED_COMMAND_TEXT_LENGTH = ISSUED_COMMAND_TEXT.length();
|
||||
|
||||
private static ModuleLogger logger;
|
||||
|
||||
public static void initialize() {
|
||||
org.apache.logging.log4j.core.Logger logger;
|
||||
logger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger();
|
||||
logger.addFilter(new Log4JFilter());
|
||||
PrivateLogManager.logger = new ModuleLogger("PrivateLogManager");
|
||||
}
|
||||
|
||||
public static void register(Module module, String command, String replacement) {
|
||||
command = command.toLowerCase();
|
||||
registrar.put(command, module);
|
||||
commands.put(command, replacement);
|
||||
logger.info(module.getClass().getSimpleName() + " registered &e/" + command
|
||||
+ (replacement.equals("")? "&7. Command will not be logged!"
|
||||
: "&7, using replacement, &e" + replacement + "&7."));
|
||||
}
|
||||
|
||||
public static void unregister(Module module) {
|
||||
String unregestered = "";
|
||||
Iterator<Map.Entry<String, Module>> i = registrar.entrySet().iterator();
|
||||
while (i.hasNext()) {
|
||||
Map.Entry<String, Module> entry = i.next();
|
||||
if (entry.getValue() == module) {
|
||||
i.remove();
|
||||
commands.remove(entry.getKey());
|
||||
unregestered += "&e" + entry.getKey() + "&7, ";
|
||||
}
|
||||
}
|
||||
if (!unregestered.equals(""))
|
||||
logger.info("Unregistered " + unregestered.substring(0, unregestered.length() - 2) + "&7 for module, " + module.getClass().getSimpleName() + ".");
|
||||
}
|
||||
|
||||
public static void unregister(Module module, String... toRemove) {
|
||||
String unregestered = "";
|
||||
for (int i = 0; i < toRemove.length; i++) {
|
||||
String command = toRemove[i].toLowerCase();
|
||||
registrar.remove(command);
|
||||
if (commands.remove(command) != null)
|
||||
unregestered += "&e" + command + "&7, ";
|
||||
}
|
||||
if (!unregestered.equals(""))
|
||||
logger.info(module.getClass().getSimpleName() + " unregistered " + unregestered.substring(0, unregestered.length() - 2) + "&7.");
|
||||
}
|
||||
|
||||
public static boolean isHidden(String message) {
|
||||
if (message == null)
|
||||
return false;
|
||||
|
||||
int index = message.indexOf(ISSUED_COMMAND_TEXT);
|
||||
if (index == -1)
|
||||
return false;
|
||||
|
||||
String command = message.substring(index + ISSUED_COMMAND_TEXT_LENGTH);
|
||||
|
||||
int spaceIndex = command.indexOf(" ");
|
||||
command = spaceIndex == -1? command.toLowerCase() : command.substring(0, spaceIndex).toLowerCase();
|
||||
|
||||
String replacement = commands.get(command);
|
||||
if (replacement == null)
|
||||
return false;
|
||||
if (replacement.equals(""))
|
||||
return true;
|
||||
|
||||
String player = message.substring(0, message.indexOf(" "));
|
||||
Utils.run(() -> System.out.println(replacement.replace("$s", player)));
|
||||
return true;
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.redstoner.annotations.Version;
|
||||
import com.redstoner.coremods.moduleLoader.ModuleLoader;
|
||||
import com.redstoner.logging.PrivateLogManager;
|
||||
import com.redstoner.misc.mysql.MysqlHandler;
|
||||
|
||||
import net.nemez.chatapi.ChatAPI;
|
||||
@ -11,7 +12,7 @@ import net.nemez.chatapi.ChatAPI;
|
||||
/** Main class. Duh.
|
||||
*
|
||||
* @author Pepich */
|
||||
@Version(major = 4, minor = 0, revision = 0, compatible = -1)
|
||||
@Version(major = 5, minor = 1, revision = 0, compatible = -1)
|
||||
public class Main extends JavaPlugin
|
||||
{
|
||||
public static JavaPlugin plugin;
|
||||
@ -20,6 +21,9 @@ public class Main extends JavaPlugin
|
||||
public void onEnable()
|
||||
{
|
||||
plugin = this;
|
||||
|
||||
PrivateLogManager.initialize();
|
||||
|
||||
ChatAPI.initialize(this);
|
||||
// Configger.init();
|
||||
MysqlHandler.init();
|
||||
|
@ -11,6 +11,7 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.redstoner.annotations.Version;
|
||||
import com.redstoner.coremods.moduleLoader.ModuleLoader;
|
||||
|
||||
import net.nemez.chatapi.ChatAPI;
|
||||
import net.nemez.chatapi.click.Message;
|
||||
@ -90,7 +91,7 @@ public final class Utils
|
||||
{
|
||||
for (Player p : Bukkit.getOnlinePlayers())
|
||||
ChatAPI.createMessage(p).appendText(prefix).appendMessage(message).send();
|
||||
Bukkit.getConsoleSender().sendMessage(prefix + message);
|
||||
Bukkit.getConsoleSender().sendMessage(prefix + message.getRawMessage());
|
||||
return Bukkit.getOnlinePlayers().size() + 1;
|
||||
}
|
||||
else
|
||||
@ -104,7 +105,7 @@ public final class Utils
|
||||
}
|
||||
if (filter.sendTo(Bukkit.getConsoleSender()))
|
||||
{
|
||||
Bukkit.getConsoleSender().sendMessage(prefix + message);
|
||||
Bukkit.getConsoleSender().sendMessage(prefix + message.getRawMessage());
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
@ -188,4 +189,15 @@ public final class Utils
|
||||
{
|
||||
return UUID_pattern.matcher(toCheck).matches();
|
||||
}
|
||||
|
||||
public static void run(Runnable r) {
|
||||
run(r, 0);
|
||||
}
|
||||
|
||||
public static void run(Runnable r, int delay) {
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(ModuleLoader.getPlugin(), r, delay);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user