summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/logging/PrivateLogManager.java
blob: e8451e02ab1ea5168b2a4aead584b3ed9e6924b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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;
	}
}