AutomaticLogDeleter/src/main/java/net/limework/automaticlogdeleter/AutomaticLogDeleter.java

56 lines
1.5 KiB
Java

package net.limework.automaticlogdeleter;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.logging.Level;
public final class AutomaticLogDeleter extends JavaPlugin {
@Override
public void onEnable() {
new Thread(() -> {
this.saveDefaultConfig();
deleteLogs();
}).start();
}
@Override
public void onDisable() {
FileConfiguration config = getConfig();
if (config.getBoolean("also-delete-logs-on-shutdown")) {
new Thread(() -> {
deleteLogs();
}).start();
}
}
public void deleteLogs() {
File path = new File("logs");
File[] files = path.listFiles();
if (files == null) { return; }
FileConfiguration config = getConfig();
//maximum file age allowed
long maxOldMillis = config.getLong("delete-logs-older-than-seconds") * 1000;
for (File file : files) {
if (file.isFile()) {
if ((System.currentTimeMillis() - file.lastModified()) > maxOldMillis) {
if (!file.delete()) {
Bukkit.getLogger().log(Level.WARNING, "Failed to delete server log file! Are you sure the file permissions are correct?");
} else {
Bukkit.getLogger().log(Level.INFO, "Deleted log file " + file.getName());
}
}
}
}
}
}