Initial Commit

This commit is contained in:
FirephoenixX02 2024-06-06 17:08:54 +02:00
commit c48bb701f2
4 changed files with 431 additions and 0 deletions

113
.gitignore vendored Normal file
View file

@ -0,0 +1,113 @@
# User-specific stuff
.idea/
*.iml
*.ipr
*.iws
# IntelliJ
out/
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.flattened-pom.xml
# Common working directory
run/

179
pom.xml Normal file
View file

@ -0,0 +1,179 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.firephoenix</groupId>
<artifactId>VelocityLogging</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>VelocityLogging</name>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>filter-src</id>
<goals>
<goal>filter-sources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
<dependencies>
<dependency>
<groupId>net.trajano.wagon</groupId>
<artifactId>wagon-git</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-module-markdown</artifactId>
<version>1.9.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<tagNameFormat>@{project.version}</tagNameFormat>
<scmCommentPrefix xml:space="preserve">[RELEASE] </scmCommentPrefix>
<goals>install deploy site-deploy</goals> <!-- install is here to fix javadoc generation in multi-module projects -->
<releaseProfiles>release</releaseProfiles>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</reporting>
<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId>
<version>3.3.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,110 @@
package me.firephoenix.velocitylogging;
import com.google.inject.Inject;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import lombok.Getter;
import me.firephoenix.velocitylogging.listener.ChatCommandListener;
import org.slf4j.Logger;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Plugin(
id = "velocitylogging",
name = "VelocityLogging",
version = "1.0"
)
public class VelocityLogging {
private static final ExecutorService executor = Executors.newSingleThreadExecutor();
public static VelocityLogging INSTANCE;
private final ProxyServer server;
@Getter
private final Logger logger;
public Path dataFolderPath;
@Inject
public VelocityLogging(ProxyServer server, Logger logger, @DataDirectory final Path folder) {
this.server = server;
this.logger = logger;
this.dataFolderPath = folder;
}
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
setInstance(this);
server.getEventManager().register(this, new ChatCommandListener());
createLogFolderAndFileIfItNotExists();
}
public void setInstance(VelocityLogging INSTANCE) {
VelocityLogging.INSTANCE = INSTANCE;
}
public void addToLogFile(String messageOrCommand, String playerName, String server) {
executor.execute(() -> {
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String formattedDate = date.format(formatter);
Path logFilePath = Path.of(dataFolderPath + "/" + formattedDate + ".log");
if (!logFilePath.toFile().exists()) createLogFolderAndFileIfItNotExists();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFilePath.toFile()))) {
String logLine = "[" + playerName + "] " +
"[" + server + "]:" +
messageOrCommand;
writer.write(logLine);
writer.newLine();
} catch (Exception e) {
e.printStackTrace();
}
});
}
public void createLogFolderAndFileIfItNotExists() {
File folder = dataFolderPath.toFile();
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String formattedDate = date.format(formatter);
File file = new File(folder, formattedDate + ".log");
if (!file.getParentFile().exists()) {
boolean created = file.getParentFile().mkdirs();
if (!created) {
throw new RuntimeException("Failed to create directories for log file.");
}
}
if (!file.exists()) {
try {
Files.createFile(file.toPath());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed to create log file.");
}
}
}
}

View file

@ -0,0 +1,29 @@
package me.firephoenix.velocitylogging.listener;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.command.CommandExecuteEvent;
import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.proxy.Player;
import me.firephoenix.velocitylogging.VelocityLogging;
/**
* @author NieGestorben
* Copyright© (c) 2024, All Rights Reserved.
*/
public class ChatCommandListener {
@Subscribe
public void onChatEvent(PlayerChatEvent event) {
Player player = event.getPlayer();
VelocityLogging.INSTANCE.addToLogFile(event.getMessage(), player.getUsername(), player.getCurrentServer().isEmpty() ? "Unknown" : player.getCurrentServer().get().getServerInfo().getName());
}
@Subscribe
public void onCommand(CommandExecuteEvent event) {
if (!(event.getCommandSource() instanceof Player player)) return;
VelocityLogging.INSTANCE.addToLogFile(event.getCommand(), player.getUsername(), player.getCurrentServer().isEmpty() ? "Unknown" : player.getCurrentServer().get().getServerInfo().getName());
}
}