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

54 lines
1.4 KiB
Java
Raw Normal View History

2021-06-12 11:43:58 +00:00
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() {
2021-06-12 11:47:13 +00:00
FileConfiguration config = getConfig();
if (config.getBoolean("also-delete-logs-on-shutdown")) {
new Thread(() -> {
2021-06-12 11:43:58 +00:00
deleteLogs();
2021-06-12 11:47:13 +00:00
}).start();
}
2021-06-12 11:43:58 +00:00
}
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?");
}
}
}
}
}
}