forked from BetonQuest/BetonQuest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request BetonQuest#2563 from J0B10/feature/log-event
Add `log` event
- Loading branch information
Showing
6 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/org/betonquest/betonquest/quest/event/log/LogEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.betonquest.betonquest.quest.event.log; | ||
|
||
import org.betonquest.betonquest.VariableString; | ||
import org.betonquest.betonquest.api.logger.BetonQuestLogger; | ||
import org.betonquest.betonquest.api.profiles.Profile; | ||
import org.betonquest.betonquest.api.quest.event.Event; | ||
import org.betonquest.betonquest.exceptions.QuestRuntimeException; | ||
|
||
/** | ||
* Prints a simple message to the server log. | ||
*/ | ||
public class LogEvent implements Event { | ||
|
||
/** | ||
* Message to log. | ||
*/ | ||
private final VariableString message; | ||
|
||
/** | ||
* Logger to use. | ||
*/ | ||
private final BetonQuestLogger logger; | ||
|
||
/** | ||
* Level to log the message at. | ||
*/ | ||
private final LogEventLevel level; | ||
|
||
/** | ||
* Create a new {@link LogEvent}. | ||
* | ||
* @param logger logger used for logging messages. | ||
* @param level level to log the message at. | ||
* @param message message that should be printed to the server log. | ||
*/ | ||
public LogEvent(final BetonQuestLogger logger, final LogEventLevel level, final VariableString message) { | ||
this.logger = logger; | ||
this.message = message; | ||
this.level = level; | ||
} | ||
|
||
@Override | ||
public void execute(final Profile profile) throws QuestRuntimeException { | ||
level.log(logger, message.getString(profile)); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/org/betonquest/betonquest/quest/event/log/LogEventFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.betonquest.betonquest.quest.event.log; | ||
|
||
import org.betonquest.betonquest.Instruction; | ||
import org.betonquest.betonquest.VariableString; | ||
import org.betonquest.betonquest.api.logger.BetonQuestLogger; | ||
import org.betonquest.betonquest.api.logger.BetonQuestLoggerFactory; | ||
import org.betonquest.betonquest.api.quest.event.Event; | ||
import org.betonquest.betonquest.api.quest.event.EventFactory; | ||
import org.betonquest.betonquest.api.quest.event.StaticEvent; | ||
import org.betonquest.betonquest.api.quest.event.StaticEventFactory; | ||
import org.betonquest.betonquest.exceptions.InstructionParseException; | ||
import org.betonquest.betonquest.quest.event.NullStaticEventAdapter; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Factory to parse new {@link LogEvent}s. | ||
*/ | ||
public class LogEventFactory implements EventFactory, StaticEventFactory { | ||
|
||
/** | ||
* Regex used to detect a conditions statement at the end of the instruction. | ||
*/ | ||
private static final Pattern CONDITIONS_REGEX = Pattern.compile("conditions?:\\S*\\s*$"); | ||
|
||
/** | ||
* Regex used to detect a level statement at the beginning of the instruction. | ||
*/ | ||
private static final Pattern LEVEL_REGEX = Pattern.compile("^\\s*level:\\S*\\s"); | ||
|
||
/** | ||
* Logger factory to create a logger for events. | ||
*/ | ||
private final BetonQuestLoggerFactory loggerFactory; | ||
|
||
/** | ||
* Create a new log event factory. | ||
* | ||
* @param loggerFactory BetonQuest logger factory used to retrieve the {@link BetonQuestLogger} for new events. | ||
*/ | ||
public LogEventFactory(final BetonQuestLoggerFactory loggerFactory) { | ||
this.loggerFactory = loggerFactory; | ||
} | ||
|
||
@Override | ||
public Event parseEvent(final Instruction instruction) throws InstructionParseException { | ||
final LogEventLevel level = instruction.getEnum("level", LogEventLevel.class, LogEventLevel.INFO); | ||
final String raw = instruction.getInstruction(); | ||
final Matcher conditionsMatcher = CONDITIONS_REGEX.matcher(raw); | ||
final Matcher levelMatcher = LEVEL_REGEX.matcher(raw); | ||
final int msgStart = levelMatcher.find() ? levelMatcher.end() : 0; | ||
final int msgEnd = conditionsMatcher.find() ? conditionsMatcher.start() : raw.length(); | ||
final VariableString message = new VariableString(instruction.getPackage(), raw.substring(msgStart, msgEnd)); | ||
return new LogEvent(loggerFactory.create(LogEvent.class), level, message); | ||
} | ||
|
||
@Override | ||
public StaticEvent parseStaticEvent(final Instruction instruction) throws InstructionParseException { | ||
return new NullStaticEventAdapter(parseEvent(instruction)); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/org/betonquest/betonquest/quest/event/log/LogEventLevel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.betonquest.betonquest.quest.event.log; | ||
|
||
import org.betonquest.betonquest.api.logger.BetonQuestLogger; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
/** | ||
* Human-readable log level used by the {@link LogEvent}. | ||
* <p> | ||
* While there is already a ton of similar enums for log levels, | ||
* they are all not really suitable for this use case for multiple reasons. | ||
* Some of them use different names for the levels, some have different levels (e.g. TRACE, ALL), | ||
* some are not direct dependencies of BetonQuest and most of them are not compatible with the {@link BetonQuestLogger}. | ||
*/ | ||
public enum LogEventLevel { | ||
/** | ||
* Level used for normal messages, default level. | ||
*/ | ||
INFO(BetonQuestLogger::info), | ||
|
||
/** | ||
* Level used for debug messages. | ||
*/ | ||
DEBUG(BetonQuestLogger::debug), | ||
|
||
/** | ||
* Level used for warnings. | ||
*/ | ||
WARNING(BetonQuestLogger::warn), | ||
|
||
/** | ||
* Level used for errors. | ||
*/ | ||
ERROR(BetonQuestLogger::error); | ||
|
||
/** | ||
* Method of the {@link BetonQuestLogger} used to log the message. | ||
*/ | ||
private final BiConsumer<BetonQuestLogger, String> logFunction; | ||
|
||
/** | ||
* Create a new {@link LogEventLevel}. | ||
* | ||
* @param logFunction method of the {@link BetonQuestLogger} used to log the message. | ||
*/ | ||
LogEventLevel(final BiConsumer<BetonQuestLogger, String> logFunction) { | ||
this.logFunction = logFunction; | ||
} | ||
|
||
/** | ||
* Log the message with the given logger at this level. | ||
* | ||
* @param logger logger used to log the message. | ||
* @param message message to log. | ||
*/ | ||
public void log(final BetonQuestLogger logger, final String message) { | ||
logFunction.accept(logger, message); | ||
} | ||
|
||
} |