From 01cf90a8ba679b3755a80f405f05c9fbb0859e7f Mon Sep 17 00:00:00 2001 From: md_5 Date: Fri, 5 Oct 2012 16:20:07 +1000 Subject: [PATCH] Fix dodgy gitignore --- .gitignore | 2 +- src/main/java/net/md_5/bungee/Logger.java | 83 +++++++++++++++++++ .../net/md_5/bungee/packet/Packet1Login.java | 39 +++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/md_5/bungee/Logger.java create mode 100644 src/main/java/net/md_5/bungee/packet/Packet1Login.java diff --git a/.gitignore b/.gitignore index b091776d..92725511 100644 --- a/.gitignore +++ b/.gitignore @@ -34,5 +34,5 @@ .idea/ # other files -*log* +/*log* *.yml diff --git a/src/main/java/net/md_5/bungee/Logger.java b/src/main/java/net/md_5/bungee/Logger.java new file mode 100644 index 00000000..6365c1f7 --- /dev/null +++ b/src/main/java/net/md_5/bungee/Logger.java @@ -0,0 +1,83 @@ +package net.md_5.bungee; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.text.SimpleDateFormat; +import java.util.logging.FileHandler; +import java.util.logging.Formatter; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +public class Logger extends java.util.logging.Logger { + + private static final Formatter formatter = new ConsoleLogFormatter(); + private static final Logger instance = new Logger(); + + public Logger() { + super("RubberBand", null); + try { + FileHandler handler = new FileHandler("proxy.log", BungeeCord.instance.config.logNumLines, 1, true); + handler.setFormatter(formatter); + addHandler(handler); + } catch (IOException | SecurityException ex) { + System.err.println("Could not register logger!"); + ex.printStackTrace(); + } + } + + @Override + public void log(LogRecord record) { + super.log(record); + String message = formatter.format(record); + if (record.getLevel() == Level.SEVERE || record.getLevel() == Level.WARNING) { + System.err.print(message); + } else { + System.out.print(message); + } + } + + public static Logger $() { + return instance; + } + + public static class ConsoleLogFormatter extends Formatter { + + private SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); + + @Override + public String format(LogRecord logrecord) { + StringBuilder formatted = new StringBuilder(); + + formatted.append(formatter.format(logrecord.getMillis())); + Level level = logrecord.getLevel(); + + if (level == Level.FINEST) { + formatted.append(" [FINEST] "); + } else if (level == Level.FINER) { + formatted.append(" [FINER] "); + } else if (level == Level.FINE) { + formatted.append(" [FINE] "); + } else if (level == Level.INFO) { + formatted.append(" [INFO] "); + } else if (level == Level.WARNING) { + formatted.append(" [WARNING] "); + } else if (level == Level.SEVERE) { + formatted.append(" [SEVERE] "); + } + + formatted.append(logrecord.getMessage()); + formatted.append('\n'); + Throwable throwable = logrecord.getThrown(); + + if (throwable != null) { + StringWriter writer = new StringWriter(); + + throwable.printStackTrace(new PrintWriter(writer)); + formatted.append(writer); + } + + return formatted.toString(); + } + } +} diff --git a/src/main/java/net/md_5/bungee/packet/Packet1Login.java b/src/main/java/net/md_5/bungee/packet/Packet1Login.java new file mode 100644 index 00000000..0555f8ff --- /dev/null +++ b/src/main/java/net/md_5/bungee/packet/Packet1Login.java @@ -0,0 +1,39 @@ +package net.md_5.bungee.packet; + +import lombok.EqualsAndHashCode; +import lombok.ToString; + +@ToString +@EqualsAndHashCode(callSuper = false) +public class Packet1Login extends DefinedPacket { + + public int entityId; + public String levelType; + public byte gameMode; + public byte dimension; + public byte difficulty; + public byte unused; + public byte maxPlayers; + + public Packet1Login(int entityId, String levelType, byte gameMode, byte dimension, byte difficulty, byte unused, byte maxPlayers) { + super(0x01); + writeInt(entityId); + writeUTF(levelType); + writeByte(gameMode); + writeByte(dimension); + writeByte(difficulty); + writeByte(unused); + writeByte(maxPlayers); + } + + public Packet1Login(byte[] buf) { + super(0x01, buf); + this.entityId = readInt(); + this.levelType = readUTF(); + this.gameMode = readByte(); + this.dimension = readByte(); + this.difficulty = readByte(); + this.unused = readByte(); + this.maxPlayers = readByte(); + } +}