2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs;
|
|
|
|
|
|
2022-11-03 02:47:55 -04:00
|
|
|
import noppes.npcs.constants.EnumScriptType;
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.io.StringWriter;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.Date;
|
2022-11-03 02:47:55 -04:00
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2018-09-13 12:03:28 -04:00
|
|
|
import java.util.logging.ConsoleHandler;
|
|
|
|
|
import java.util.logging.Formatter;
|
|
|
|
|
import java.util.logging.Handler;
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.LogRecord;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
import java.util.logging.StreamHandler;
|
|
|
|
|
|
|
|
|
|
public class LogWriter {
|
|
|
|
|
private final static String name = "CustomNPCs";
|
2022-11-05 21:30:06 -04:00
|
|
|
private final static String nameLog = "CustomNPCLog";
|
2018-09-13 12:03:28 -04:00
|
|
|
private final static Logger logger = Logger.getLogger(name);
|
2022-11-03 02:47:55 -04:00
|
|
|
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> InitCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> TickCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> InteractCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> DialogCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> DamagedCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> KilledCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> AttackCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> TargetCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> CollideCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> KillsCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> DialogCloseCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
private final static WeakHashMap<UUID, NPCStamp> TimerCacheMap = new WeakHashMap<UUID, NPCStamp>();
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
private final static SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm:ss");
|
|
|
|
|
private static Handler handler;
|
|
|
|
|
static {
|
|
|
|
|
try {
|
|
|
|
|
File dir = new File("logs");
|
|
|
|
|
if(!dir.exists())
|
|
|
|
|
dir.mkdir();
|
|
|
|
|
File file = new File(dir, name + "-latest.log");
|
|
|
|
|
File lock = new File(dir, name + "-latest.log.lck");
|
|
|
|
|
File file1 = new File(dir, name + "-1.log");
|
|
|
|
|
File file2 = new File(dir, name + "-2.log");
|
|
|
|
|
File file3 = new File(dir, name + "-3.log");
|
|
|
|
|
|
|
|
|
|
if(lock.exists())
|
|
|
|
|
lock.delete();
|
|
|
|
|
|
|
|
|
|
if(file3.exists())
|
|
|
|
|
file3.delete();
|
|
|
|
|
if(file2.exists())
|
|
|
|
|
file2.renameTo(file3);
|
|
|
|
|
if(file1.exists())
|
|
|
|
|
file1.renameTo(file2);
|
|
|
|
|
if(file.exists())
|
|
|
|
|
file.renameTo(file1);
|
|
|
|
|
|
|
|
|
|
handler = new StreamHandler(new FileOutputStream(file), new Formatter() {
|
|
|
|
|
@Override
|
|
|
|
|
public String format(LogRecord record) {
|
2022-11-05 21:30:06 -04:00
|
|
|
String line = "";
|
|
|
|
|
if (record.getLevel() != NPCLogLevel.CNPCLog){
|
|
|
|
|
StackTraceElement element = Thread.currentThread().getStackTrace()[8];
|
|
|
|
|
line = "[" + element.getClassName() + ":" + element.getLineNumber() + "] ";
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
String time = "[" + dateformat.format(new Date(record.getMillis())) + "][" + record.getLevel() + "]" + line;
|
|
|
|
|
if(record.getThrown() != null){
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
record.getThrown().printStackTrace(pw);
|
|
|
|
|
return time + sw.toString();
|
|
|
|
|
}
|
|
|
|
|
return time + record.getMessage() + System.getProperty("line.separator");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
handler.setLevel(Level.ALL);
|
|
|
|
|
logger.addHandler(handler);
|
|
|
|
|
logger.setUseParentHandlers(false);
|
|
|
|
|
Handler consoleHandler = new ConsoleHandler();
|
|
|
|
|
consoleHandler.setFormatter(handler.getFormatter());
|
2022-11-05 21:30:06 -04:00
|
|
|
consoleHandler.setLevel(Level.ALL);
|
2018-09-13 12:03:28 -04:00
|
|
|
logger.addHandler(consoleHandler);
|
|
|
|
|
logger.setLevel(Level.ALL);
|
|
|
|
|
info(new Date().toString());
|
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 02:47:55 -04:00
|
|
|
public static void postScriptLog(UUID npcUUID, EnumScriptType type, String message) {
|
|
|
|
|
switch (type){
|
|
|
|
|
case INIT:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.InitIgnore){
|
|
|
|
|
scriptLogCalculator(InitCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
case TICK:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.TickIgnore){
|
|
|
|
|
scriptLogCalculator(TickCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
case INTERACT:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.InteractIgnore){
|
|
|
|
|
scriptLogCalculator(InteractCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
case DIALOG:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.DialogIgnore){
|
|
|
|
|
scriptLogCalculator(DialogCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
case DAMAGED:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.DamagedIgnore){
|
|
|
|
|
scriptLogCalculator(DamagedCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
case KILLED:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.KilledIgnore){
|
|
|
|
|
scriptLogCalculator(KilledCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
case ATTACK:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.AttackIgnore){
|
|
|
|
|
scriptLogCalculator(AttackCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
case TARGET:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.TargetIgnore){
|
|
|
|
|
scriptLogCalculator(TargetCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
case COLLIDE:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.CollideIgnore){
|
|
|
|
|
scriptLogCalculator(CollideCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
case KILLS:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.KillsIgnore){
|
|
|
|
|
scriptLogCalculator(KillsCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
case DIALOG_CLOSE:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.DialogCloseIgnore){
|
|
|
|
|
scriptLogCalculator(DialogCloseCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
case TIMER:
|
2022-11-05 23:32:54 -04:00
|
|
|
if(!CustomNpcs.TimerIgnore){
|
|
|
|
|
scriptLogCalculator(TimerCacheMap, npcUUID, message);
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void scriptLogCalculator(WeakHashMap<UUID, NPCStamp> map, UUID npcUUID, String message) {
|
|
|
|
|
if(map.containsKey(npcUUID)){
|
|
|
|
|
NPCStamp stamp = map.get(npcUUID);
|
2022-11-03 20:07:28 -04:00
|
|
|
long secondsSinceFirst = TimeUnit.MILLISECONDS.toSeconds(new Date().getTime() - stamp.makeDate.getTime());
|
|
|
|
|
long millisecSinceLast = TimeUnit.MILLISECONDS.toMillis(stamp.recentDate.getTime() - stamp.makeDate.getTime());
|
|
|
|
|
|
2022-11-03 02:47:55 -04:00
|
|
|
double frequency = (float)CustomNpcs.ScriptFrequency / 60;
|
2022-11-05 21:30:06 -04:00
|
|
|
// Reset Log if 2 Minutes Pass
|
2022-11-03 21:28:18 -04:00
|
|
|
if (secondsSinceFirst > 120) {
|
2022-11-03 02:47:55 -04:00
|
|
|
LogWriter.script(message);
|
2022-11-03 20:07:28 -04:00
|
|
|
stamp.counter = 1;
|
|
|
|
|
stamp.makeDate = new Date();
|
|
|
|
|
stamp.recentDate = new Date();
|
|
|
|
|
}
|
|
|
|
|
// ALWAYS Log the First 3 of Event
|
2022-11-03 21:28:18 -04:00
|
|
|
else if(secondsSinceFirst < 10 && stamp.counter < 3) {
|
2022-11-03 20:07:28 -04:00
|
|
|
LogWriter.script(message);
|
|
|
|
|
stamp.recentDate = new Date();
|
|
|
|
|
}
|
|
|
|
|
// IF event occurs to QUICKLY Ignore.
|
|
|
|
|
else if (millisecSinceLast < CustomNpcs.ScriptIgnoreTime){
|
|
|
|
|
stamp.recentDate = new Date();
|
2022-11-03 02:47:55 -04:00
|
|
|
}
|
|
|
|
|
else if(secondsSinceFirst > 10 && (double)stamp.counter / secondsSinceFirst > frequency){
|
|
|
|
|
// WARN
|
|
|
|
|
LogWriter.script("[SPAM]:" + message);
|
|
|
|
|
stamp.counter = 0;
|
2022-11-03 20:07:28 -04:00
|
|
|
stamp.recentDate = new Date();
|
2022-11-03 02:47:55 -04:00
|
|
|
}
|
2022-11-03 21:28:18 -04:00
|
|
|
stamp.counter++;
|
2022-11-03 02:47:55 -04:00
|
|
|
} else {
|
|
|
|
|
map.put(npcUUID, new NPCStamp());
|
|
|
|
|
LogWriter.script(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void script(Object msg) {
|
2022-11-05 21:30:06 -04:00
|
|
|
logger.log(NPCLogLevel.CNPCLog, msg.toString());
|
2022-11-03 02:47:55 -04:00
|
|
|
handler.flush();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public static void info(Object msg) {
|
2022-06-17 21:26:40 -04:00
|
|
|
logger.log(Level.INFO, msg.toString());
|
2018-09-13 12:03:28 -04:00
|
|
|
handler.flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void error(Object msg) {
|
|
|
|
|
logger.log(Level.SEVERE, msg.toString());
|
|
|
|
|
handler.flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void error(Object msg, Exception e) {
|
|
|
|
|
logger.log(Level.SEVERE, msg.toString());
|
|
|
|
|
logger.log(Level.SEVERE, e.getMessage(), e);
|
|
|
|
|
handler.flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void except(Exception e) {
|
|
|
|
|
logger.log(Level.SEVERE, e.getMessage(), e);
|
|
|
|
|
handler.flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
|
|
|
|
|
class NPCStamp
|
|
|
|
|
{
|
2022-11-03 20:07:28 -04:00
|
|
|
public Date makeDate = new Date();
|
|
|
|
|
public Date recentDate = new Date();
|
2022-11-03 02:47:55 -04:00
|
|
|
public int counter = 1;
|
|
|
|
|
};
|