add logger & revert back to executor because yes
not sure if executor will break again i set it to 100 instead of 69 now
This commit is contained in:
parent
b24a4c6951
commit
cd2fa73976
10 changed files with 292 additions and 159 deletions
|
@ -13,8 +13,9 @@ import org.apache.commons.lang3.RandomStringUtils;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Bot {
|
||||
private final ArrayList<SessionListener> listeners = new ArrayList<>();
|
||||
|
@ -31,6 +32,8 @@ public class Bot {
|
|||
|
||||
@Getter private Session session;
|
||||
|
||||
@Getter private ScheduledExecutorService executor = Executors.newScheduledThreadPool(100);
|
||||
|
||||
@Getter @Setter private ConsolePlugin console;
|
||||
@Getter @Setter private LoggerPlugin logger; // in ConsolePlugin
|
||||
@Getter private final ChatPlugin chat;
|
||||
|
@ -140,13 +143,7 @@ public class Bot {
|
|||
|
||||
if (reconnectDelay < 0) return; // to disable reconnecting
|
||||
|
||||
final Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
reconnect();
|
||||
}
|
||||
}, reconnectDelay);
|
||||
executor.schedule(() -> reconnect(), reconnectDelay, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
173
src/main/java/land/chipmunk/chayapak/chomens_bot/Logger.java
Normal file
173
src/main/java/land/chipmunk/chayapak/chomens_bot/Logger.java
Normal file
|
@ -0,0 +1,173 @@
|
|||
package land.chipmunk.chayapak.chomens_bot;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
// totallynotskidded™ from HBot
|
||||
public class Logger {
|
||||
public static File logDir = new File("logs");
|
||||
public static File logFile = new File(logDir, "log.txt");
|
||||
public static OutputStreamWriter logWriter;
|
||||
|
||||
public static LocalDate currentLogDate;
|
||||
public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("'['dd/MM/yyyy HH:mm:ss']' ");
|
||||
public static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("'dd/MM/yyyy'");
|
||||
|
||||
public static String prevEntry = "";
|
||||
public static int duplicateCounter = 1;
|
||||
|
||||
public static ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
|
||||
public static int spamLevel = 0;
|
||||
public static long freezeTime = 0;
|
||||
|
||||
static {
|
||||
init();
|
||||
executor.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
tick();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 0, 50, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
if (!logDir.exists()) logDir.mkdir();
|
||||
|
||||
try {
|
||||
if (!logFile.exists()) {
|
||||
makeNewLogFile();
|
||||
} else if (!logIsCurrent(logFile)) {
|
||||
compressLogFile();
|
||||
makeNewLogFile();
|
||||
} else {
|
||||
openLogFile();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void tick() {
|
||||
if (freezeTime <= System.currentTimeMillis() && spamLevel > 0) {
|
||||
spamLevel--;
|
||||
}
|
||||
if (!currentLogDate.equals(LocalDate.now())) {
|
||||
try {
|
||||
compressLogFile();
|
||||
makeNewLogFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void makeNewLogFile() throws IOException {
|
||||
currentLogDate = LocalDate.parse(LocalDate.now().format(dateFormatter));
|
||||
logWriter = new OutputStreamWriter(new FileOutputStream(logFile, false), StandardCharsets.UTF_8);
|
||||
logWriter.write(currentLogDate.toString() + '\n');
|
||||
logWriter.flush();
|
||||
}
|
||||
|
||||
public static synchronized void openLogFile() throws IOException {
|
||||
currentLogDate = LocalDate.parse(getLogDate(logFile));
|
||||
logWriter = new OutputStreamWriter(new FileOutputStream(logFile, true), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static synchronized void compressLogFile() throws IOException {
|
||||
if (Files.size(logFile.toPath()) > 100*1024*1024) { // Will not save because log file is too big
|
||||
return;
|
||||
}
|
||||
|
||||
File gzipFile = new File(logDir, getLogDate(logFile) + ".txt.gz");
|
||||
FileInputStream in = new FileInputStream(logFile);
|
||||
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzipFile));
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int size;
|
||||
while ((size = in.read(buffer)) > 0) {
|
||||
out.write(buffer, 0, size);
|
||||
}
|
||||
in.close();
|
||||
out.finish();
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static synchronized BufferedReader getLogReader(File file) throws IOException {
|
||||
GZIPInputStream in = new GZIPInputStream(new FileInputStream(file));
|
||||
return new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public static synchronized String getLogDate(File file) throws IOException {
|
||||
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
|
||||
BufferedReader reader = new BufferedReader(isr);
|
||||
String date = reader.readLine();
|
||||
reader.close();
|
||||
return date;
|
||||
}
|
||||
|
||||
public static synchronized boolean logIsCurrent(File file) throws IOException {
|
||||
LocalDate date = LocalDate.now();
|
||||
return getLogDate(file).equals(date.toString());
|
||||
}
|
||||
|
||||
public static synchronized void log(String str) {
|
||||
if (freezeTime > System.currentTimeMillis()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (spamLevel >= 100) {
|
||||
spamLevel = 80;
|
||||
freezeTime = System.currentTimeMillis() + 20000;
|
||||
|
||||
if (duplicateCounter > 1) {
|
||||
logWriter.write(String.format(" [%sx]\n", duplicateCounter));
|
||||
} else {
|
||||
logWriter.write("\n");
|
||||
}
|
||||
|
||||
logWriter.write("Spam detected, logs temporarily frozen");
|
||||
logWriter.flush();
|
||||
|
||||
duplicateCounter = 1;
|
||||
prevEntry = "";
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (str.equalsIgnoreCase(prevEntry)) {
|
||||
duplicateCounter++;
|
||||
} else {
|
||||
if (duplicateCounter > 1) {
|
||||
logWriter.write(String.format(" [%sx]\n", duplicateCounter));
|
||||
} else {
|
||||
logWriter.write("\n");
|
||||
}
|
||||
|
||||
logWriter.write(getTimePrefix() + str.replaceAll("\\[(\\d+?)x](?=$|[\r\n])", "[/$1x]")); // the replaceAll will prevent conflicts with the duplicate counter
|
||||
logWriter.flush();
|
||||
|
||||
duplicateCounter = 1;
|
||||
prevEntry = str;
|
||||
|
||||
spamLevel += 2;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public static String getTimePrefix() {
|
||||
LocalDateTime dateTime = LocalDateTime.now();
|
||||
return dateTime.format(dateTimeFormatter);
|
||||
}
|
||||
}
|
|
@ -8,8 +8,7 @@ import net.kyori.adventure.text.Component;
|
|||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class BruhifyPlugin {
|
||||
@Getter @Setter private String bruhifyText = "";
|
||||
|
@ -17,11 +16,7 @@ public class BruhifyPlugin {
|
|||
private int startHue = 0;
|
||||
|
||||
public BruhifyPlugin (Bot bot) {
|
||||
final Timer timer = new Timer();
|
||||
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
bot.executor().scheduleAtFixedRate(() -> {
|
||||
if (bruhifyText.equals("")) return;
|
||||
|
||||
int hue = startHue;
|
||||
|
@ -39,7 +34,6 @@ public class BruhifyPlugin {
|
|||
bot.core().run("minecraft:title @a actionbar " + GsonComponentSerializer.gson().serialize(component));
|
||||
|
||||
startHue = (startHue + increment) % 360;
|
||||
}
|
||||
}, 50, 100);
|
||||
}, 50, 100, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import lombok.Getter;
|
|||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class CorePlugin extends PositionPlugin.PositionListener {
|
||||
private final Bot bot;
|
||||
|
@ -118,13 +119,7 @@ public class CorePlugin extends PositionPlugin.PositionListener {
|
|||
if (!ready) {
|
||||
ready = true;
|
||||
|
||||
final Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
refill();
|
||||
}
|
||||
}, 1, bot.config().core().refillInterval());
|
||||
bot.executor().scheduleAtFixedRate(this::refill, 0, bot.config().core().refillInterval(), TimeUnit.MILLISECONDS);
|
||||
for (Listener listener : listeners) listener.ready();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,9 +26,8 @@ import org.jetbrains.annotations.NotNull;
|
|||
import javax.security.auth.login.LoginException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class DiscordPlugin {
|
||||
@Getter private JDA jda;
|
||||
|
@ -171,15 +170,7 @@ public class DiscordPlugin {
|
|||
}
|
||||
});
|
||||
|
||||
final TimerTask task = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
onDiscordTick(channelId);
|
||||
}
|
||||
};
|
||||
|
||||
final Timer timer = new Timer();
|
||||
timer.schedule(task, 50, 50);
|
||||
bot.executor().scheduleAtFixedRate(() -> onDiscordTick(channelId), 0, 50, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -5,8 +5,7 @@ import land.chipmunk.chayapak.chomens_bot.Bot;
|
|||
import lombok.Getter;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class HashingPlugin {
|
||||
private final Bot bot;
|
||||
|
@ -17,13 +16,7 @@ public class HashingPlugin {
|
|||
public HashingPlugin (Bot bot) {
|
||||
this.bot = bot;
|
||||
|
||||
final Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
update();
|
||||
}
|
||||
}, 1, 1000);
|
||||
bot.executor().scheduleAtFixedRate(this::update, 0, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public void update () {
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.github.steveice10.packetlib.event.session.ConnectedEvent;
|
|||
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
|
||||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.Logger;
|
||||
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
|
@ -29,11 +30,16 @@ public class LoggerPlugin extends ChatPlugin.ChatListener {
|
|||
}
|
||||
|
||||
public void log (String message) {
|
||||
bot.console().reader().printAbove(
|
||||
String.format(
|
||||
final String formattedMessage = String.format(
|
||||
"[%s] %s",
|
||||
bot.host() + ":" + bot.port(),
|
||||
message
|
||||
);
|
||||
bot.console().reader().printAbove(formattedMessage);
|
||||
Logger.log(
|
||||
formattedMessage.replaceAll( // use replaceAll for regexes, use replace for normal string
|
||||
"\u001B\\[[;\\d]*[ -/]*[@-~]",
|
||||
""
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import land.chipmunk.chayapak.chomens_bot.song.*;
|
|||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.song.*;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
|
@ -16,13 +15,13 @@ import java.net.URL;
|
|||
import java.nio.file.Path;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class MusicPlayerPlugin extends SessionAdapter {
|
||||
private final Bot bot;
|
||||
|
||||
private TimerTask playTask;
|
||||
private ScheduledFuture<?> playTask;
|
||||
|
||||
public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic]";
|
||||
public static File SONG_DIR = new File("songs");
|
||||
|
@ -86,9 +85,7 @@ public class MusicPlayerPlugin extends SessionAdapter {
|
|||
}
|
||||
|
||||
public void coreReady () {
|
||||
playTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
final Runnable task = () -> {
|
||||
if (loaderThread != null && !loaderThread.isAlive()) {
|
||||
if (loaderThread.exception != null) {
|
||||
bot.chat().tellraw(Component.translatable("Failed to load song: %s", loaderThread.exception.message()).color(NamedTextColor.RED));
|
||||
|
@ -149,11 +146,9 @@ public class MusicPlayerPlugin extends SessionAdapter {
|
|||
currentSong.play();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
final Timer timer = new Timer();
|
||||
timer.schedule(playTask, 1, 50);
|
||||
playTask = bot.executor().schedule(task, 50, TimeUnit.MILLISECONDS);
|
||||
|
||||
if (currentSong != null) currentSong.play();
|
||||
}
|
||||
|
@ -226,7 +221,7 @@ public class MusicPlayerPlugin extends SessionAdapter {
|
|||
|
||||
@Override
|
||||
public void disconnected (DisconnectedEvent event) {
|
||||
playTask.cancel();
|
||||
playTask.cancel(true);
|
||||
|
||||
if (currentSong != null) currentSong.pause(); // nice.
|
||||
}
|
||||
|
|
|
@ -16,19 +16,19 @@ import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
|
|||
import com.github.steveice10.packetlib.event.session.SessionAdapter;
|
||||
import com.github.steveice10.packetlib.packet.Packet;
|
||||
import com.github.steveice10.packetlib.packet.PacketProtocol;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||
import land.chipmunk.chayapak.chomens_bot.Configuration;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class SelfCarePlugin extends SessionAdapter {
|
||||
private final Bot bot;
|
||||
|
||||
private TimerTask checkTask;
|
||||
private ScheduledFuture<?> checkTask;
|
||||
|
||||
@Getter @Setter boolean visibility = false;
|
||||
|
||||
|
@ -108,9 +108,7 @@ public class SelfCarePlugin extends SessionAdapter {
|
|||
muted = false;
|
||||
prefix = false;
|
||||
|
||||
checkTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
final Runnable task = () -> {
|
||||
final Session session = bot.session();
|
||||
final PacketProtocol protocol = session.getPacketProtocol();
|
||||
if (
|
||||
|
@ -122,11 +120,9 @@ public class SelfCarePlugin extends SessionAdapter {
|
|||
) return;
|
||||
|
||||
check();
|
||||
}
|
||||
};
|
||||
|
||||
final Timer timer = new Timer();
|
||||
timer.schedule(checkTask, 1, bot.config().selfCare().checkInterval());
|
||||
checkTask = bot.executor().scheduleAtFixedRate(task, 0, bot.config().selfCare().checkInterval(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public void packetReceived (ClientboundGameEventPacket packet) {
|
||||
|
@ -156,6 +152,6 @@ public class SelfCarePlugin extends SessionAdapter {
|
|||
|
||||
@Override
|
||||
public void disconnected (DisconnectedEvent event) {
|
||||
checkTask.cancel();
|
||||
checkTask.cancel(true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,8 +12,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
|||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TPSPlugin extends SessionAdapter {
|
||||
private final Bot bot;
|
||||
|
@ -32,13 +31,7 @@ public class TPSPlugin extends SessionAdapter {
|
|||
|
||||
bot.addListener(this);
|
||||
|
||||
final Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateTPSBar();
|
||||
}
|
||||
}, 1, 50);
|
||||
bot.executor().scheduleAtFixedRate(this::updateTPSBar, 0, 50, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public void on () {
|
||||
|
|
Loading…
Reference in a new issue