From a15bd4aa8348aaa74fd6e940965f1215386424df Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 27 Jun 2024 15:33:00 +0200 Subject: [PATCH] Reworked Checklist Channel, Working ErrorLogger Signed-off-by: Lixfel --- src/de/steamwar/velocitycore/ErrorLogger.java | 62 ++++++++----------- .../velocitycore/commands/CheckCommand.java | 2 +- .../velocitycore/discord/DiscordBot.java | 2 +- .../discord/channels/ChecklistChannel.java | 46 +++++++------- 4 files changed, 54 insertions(+), 58 deletions(-) diff --git a/src/de/steamwar/velocitycore/ErrorLogger.java b/src/de/steamwar/velocitycore/ErrorLogger.java index ac1dcb74..0da1ff00 100644 --- a/src/de/steamwar/velocitycore/ErrorLogger.java +++ b/src/de/steamwar/velocitycore/ErrorLogger.java @@ -20,71 +20,63 @@ package de.steamwar.velocitycore; import de.steamwar.sql.SWException; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.AbstractConfiguration; +import org.apache.logging.log4j.core.config.Property; import java.io.ByteArrayOutputStream; import java.io.PrintStream; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.logging.Handler; -import java.util.logging.Level; -import java.util.logging.LogRecord; -import java.util.logging.Logger; -public class ErrorLogger extends Handler /*AbstractAppender*/ { +public class ErrorLogger extends AbstractAppender { - ErrorLogger(){ - //super("SW ErrorLogger", filter, layout, false, null); - //((LoggerContext) org.apache.logging.log4j.LogManager.getContext(false)).getConfiguration().addAppender(this); - VelocityCore.getLogger().addHandler(this); + ErrorLogger() { + super("SW ErrorLogger", null, null, false, Property.EMPTY_ARRAY); + start(); + + LoggerContext context = (LoggerContext) LogManager.getContext(false); + context.getConfiguration().addAppender(this); + context.getRootLogger().addAppender(this); + context.updateLoggers(); } - void unregister(){ - Logger.getGlobal().removeHandler(this); + void unregister() { + LoggerContext context = (LoggerContext) LogManager.getContext(false); + ((AbstractConfiguration) context.getConfiguration()).removeAppender(getName()); + context.getRootLogger().removeAppender(this); + context.updateLoggers(); } @Override - public void publish(LogRecord logRecord) { - if(logRecord.getLevel().intValue() < Level.WARNING.intValue()) + public void append(LogEvent event) { + if(event.getLevel().isLessSpecificThan(Level.WARN)) return; - String message = logRecord.getParameters() != null ? MessageFormat.format(logRecord.getMessage(), logRecord.getParameters()) : logRecord.getMessage(); + String message = event.getMessage().getFormattedMessage(); for(String reason : ignoreContains) if(message.contains(reason)) return; ByteArrayOutputStream stacktraceOutput = new ByteArrayOutputStream(); - if(logRecord.getThrown() != null) - logRecord.getThrown().printStackTrace(new PrintStream(stacktraceOutput)); + if(event.getThrown() != null) + event.getThrown().printStackTrace(new PrintStream(stacktraceOutput)); String stacktrace = stacktraceOutput.toString(); - if (stacktrace.contains("ErrorLogger")) { + if (stacktrace.contains("ErrorLogger")) return; - } - SWException.log(message, stacktrace); } - @Override - public void flush() { - //ignored - } - - @Override - public void close() { - //ignored - } - private static final List ignoreContains; static { List contains = new ArrayList<>(); ignoreContains = Collections.unmodifiableList(contains); } - - /*@Override - public void append(LogEvent event) { - - }*/ } diff --git a/src/de/steamwar/velocitycore/commands/CheckCommand.java b/src/de/steamwar/velocitycore/commands/CheckCommand.java index 5945239b..b4abc189 100644 --- a/src/de/steamwar/velocitycore/commands/CheckCommand.java +++ b/src/de/steamwar/velocitycore/commands/CheckCommand.java @@ -169,7 +169,7 @@ public class CheckCommand extends SWCommand { } public static List getSchemsToCheck(){ - List schematicList = new LinkedList<>(); + List schematicList = new ArrayList<>(); for (SchematicType type : SchematicType.values()) { if (type.check()) diff --git a/src/de/steamwar/velocitycore/discord/DiscordBot.java b/src/de/steamwar/velocitycore/discord/DiscordBot.java index d816cc23..e7063433 100644 --- a/src/de/steamwar/velocitycore/discord/DiscordBot.java +++ b/src/de/steamwar/velocitycore/discord/DiscordBot.java @@ -147,7 +147,7 @@ public class DiscordBot { .build()) .setActionRows(ActionRow.of(Arrays.stream(DiscordTicketType.values()).map(DiscordTicketType::toButton).toArray(Button[]::new))), DiscordTicketHandler::openTicket); eventChannel = new StaticMessageChannel(config.channel("events"), EventChannel::get); - StaticMessageChannel checklistChannel = new StaticMessageChannel(config.channel("checklist"), ChecklistChannel::get); + ChecklistChannel checklistChannel = new ChecklistChannel(config.channel("checklist")); announcementChannel = new DiscordChannel(config.channel("announcement")) { @Override diff --git a/src/de/steamwar/velocitycore/discord/channels/ChecklistChannel.java b/src/de/steamwar/velocitycore/discord/channels/ChecklistChannel.java index af30ad0d..88d9a357 100644 --- a/src/de/steamwar/velocitycore/discord/channels/ChecklistChannel.java +++ b/src/de/steamwar/velocitycore/discord/channels/ChecklistChannel.java @@ -19,34 +19,38 @@ package de.steamwar.velocitycore.discord.channels; -import de.steamwar.velocitycore.commands.CheckCommand; +import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SteamwarUser; -import lombok.experimental.UtilityClass; +import de.steamwar.velocitycore.commands.CheckCommand; import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.MessageBuilder; import java.awt.*; +import java.util.ArrayList; +import java.util.List; -@UtilityClass -public class ChecklistChannel { +public class ChecklistChannel extends DiscordChannel { - public MessageBuilder get() { - EmbedBuilder embedBuilder = new EmbedBuilder() - .setColor(Color.GRAY) - .setTitle("Check List") - .setAuthor("SteamWar", "https://www.steamwar.de"); + private final List lastSchematics = new ArrayList<>(); - CheckCommand.getSchemsToCheck().forEach(schematic -> { - StringBuilder st = new StringBuilder(); - st.append("Typ: ").append(schematic.getSchemtype().getKuerzel()); - st.append("\nVon: ").append(SteamwarUser.get(schematic.getOwner()).getUserName()); - String checker = CheckCommand.getChecker(schematic); - if (checker != null) { - st.append("\nWird Geprüft von: ").append(checker); - } - embedBuilder.addField(schematic.getName(), st.toString(), true); - }); + public ChecklistChannel(String channel) { + super(channel); + } - return StaticMessageChannel.toMessageBuilder(embedBuilder); + public void update() { + List schems = CheckCommand.getSchemsToCheck(); + int currentListSize = schems.size(); + + lastSchematics.removeIf(id -> schems.stream().noneMatch(schem -> schem.getId() == id)); + schems.removeIf(schem -> lastSchematics.contains(schem.getId())); + + for(SchematicNode schem : schems) { + send(StaticMessageChannel.toMessageBuilder(new EmbedBuilder() + .setColor(Color.GRAY) + .setTitle("Check List (" + currentListSize + ")") + .setAuthor("SteamWar", "https://www.steamwar.de") + .addField(schem.getName(), "Typ: " + schem.getSchemtype().getKuerzel() + "\nVon: " + SteamwarUser.get(schem.getOwner()).getUserName(), true))); + + lastSchematics.add(schem.getId()); + } } }