diff --git a/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java b/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java index ed98c24..6514650 100644 --- a/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java +++ b/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java @@ -6,10 +6,12 @@ import de.fanta.challengesjoinentities.adventure.CategoriesConfig; import de.fanta.challengesjoinentities.commands.AdventureCommand.AdventureAddCategoryCommand; import de.fanta.challengesjoinentities.commands.AdventureCommand.AdventureAddMapCommand; +import de.fanta.challengesjoinentities.commands.AdventureCommand.AdventureClearSavesCommand; import de.fanta.challengesjoinentities.commands.AdventureCommand.AdventureConvertMapCommand; import de.fanta.challengesjoinentities.commands.AdventureCommand.AdventureLoadCommand; import de.fanta.challengesjoinentities.commands.AdventureCommand.AdventureMapsCommand; import de.fanta.challengesjoinentities.commands.AdventureCommand.AdventureTestMapCommand; +import de.fanta.challengesjoinentities.commands.AdventureCommand.ChallengeClearSavesCommand; import de.fanta.challengesjoinentities.commands.AdventureCommand.ChallengesRemovePlayerServer; import de.fanta.challengesjoinentities.commands.ChallengesCommand.ChallengeSetStatsCommand; import de.fanta.challengesjoinentities.commands.ChallengesCommand.ChallengesStatsCommand; @@ -119,6 +121,7 @@ challengesrouter.addCommandMapping(new ChallengesloadCommand(this), "load"); challengesrouter.addCommandMapping(new ChallengesStatsCommand(plugin.getStatistics()), "stats"); challengesrouter.addCommandMapping(new ChallengeSetStatsCommand(plugin), "setstats"); + challengesrouter.addCommandMapping(new ChallengeClearSavesCommand(), "clearoldsaves"); CommandRouter adventurerouter = new CommandRouter(getCommand("adventure")); adventurerouter.addCommandMapping(new AdventureLoadCommand(this), "load"); @@ -128,6 +131,7 @@ adventurerouter.addCommandMapping(new AdventureTestMapCommand(this), "testmap"); adventurerouter.addCommandMapping(new ChallengesRemovePlayerServer(this), "removeplayerfromserver"); adventurerouter.addCommandMapping(new AdventureConvertMapCommand(this), "convertmap"); + adventurerouter.addCommandMapping(new AdventureClearSavesCommand(), "clearoldsaves"); Bukkit.getPluginManager().registerEvents(new EntityListener(this), this); Bukkit.getPluginManager().registerEvents(new PlayerListener(this), this); diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureClearSavesCommand.java b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureClearSavesCommand.java new file mode 100644 index 0000000..2af099c --- /dev/null +++ b/src/main/java/de/fanta/challengesjoinentities/commands/AdventureCommand/AdventureClearSavesCommand.java @@ -0,0 +1,70 @@ +package de.fanta.challengesjoinentities.commands.AdventureCommand; + +import de.fanta.challengesjoinentities.ChatUtil; +import de.iani.cubesideutils.StringUtil; +import de.iani.cubesideutils.bukkit.commands.SubCommand; +import de.iani.cubesideutils.commands.ArgsParser; +import org.apache.commons.io.FileUtils; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileTime; +import java.util.Objects; +import java.util.logging.Level; + +public class AdventureClearSavesCommand extends SubCommand { + + public AdventureClearSavesCommand() { + } + + public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { + if (!(sender instanceof Player p)) { + ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); + return true; + } + + File adventureSaveFolder = new File("/home/minecraft/Adventure-saves"); //DELETE OLD AdventureMaps + if (adventureSaveFolder.isDirectory()) { + File[] playerFolders; + playerFolders = adventureSaveFolder.listFiles(); + if (playerFolders != null) { + int deletecounter = 0; + long starttime = System.currentTimeMillis(); + for (File playerFolder : playerFolders) { + File[] saveFolders; + saveFolders = playerFolder.listFiles(); + if (saveFolders != null) { + try { + for (File map : saveFolders) { + BasicFileAttributes attr = Files.readAttributes(map.toPath(), BasicFileAttributes.class); + FileTime fileTime = attr.creationTime(); + if (10368000000L + fileTime.toMillis() <= System.currentTimeMillis()) { //120 Tage + FileUtils.deleteDirectory(map); + deletecounter++; + } + } + if (Objects.requireNonNull(playerFolder.listFiles()).length == 0) { + FileUtils.deleteDirectory(playerFolder); + } + } catch (IOException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Fehler beim löschen einer Map", ex); + } + } + } + ChatUtil.sendNormalMessage(p, "Es wurden: " + deletecounter + " maps in " + StringUtil.formatTimespan(System.currentTimeMillis() - starttime) + " gelöscht"); + } + } + return true; + } + + @Override + public String getRequiredPermission() { + return "challenges.clearsaves"; + } +} diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/ChallengesCommand/ChallengeClearSavesCommand.java b/src/main/java/de/fanta/challengesjoinentities/commands/ChallengesCommand/ChallengeClearSavesCommand.java new file mode 100644 index 0000000..a232a40 --- /dev/null +++ b/src/main/java/de/fanta/challengesjoinentities/commands/ChallengesCommand/ChallengeClearSavesCommand.java @@ -0,0 +1,70 @@ +package de.fanta.challengesjoinentities.commands.AdventureCommand; + +import de.fanta.challengesjoinentities.ChatUtil; +import de.iani.cubesideutils.StringUtil; +import de.iani.cubesideutils.bukkit.commands.SubCommand; +import de.iani.cubesideutils.commands.ArgsParser; +import org.apache.commons.io.FileUtils; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileTime; +import java.util.Objects; +import java.util.logging.Level; + +public class ChallengeClearSavesCommand extends SubCommand { + + public ChallengeClearSavesCommand() { + } + + public boolean onCommand(CommandSender sender, Command command, String alias, String commandString, ArgsParser args) { + if (!(sender instanceof Player p)) { + ChatUtil.sendErrorMessage(sender, "Du musst ein Spieler sein!"); + return true; + } + + File challengeSaveFolder = new File("/home/minecraft/saves"); //DELETE OLD ChallengeMaps + if (challengeSaveFolder.isDirectory()) { + File[] playerFolders; + playerFolders = challengeSaveFolder.listFiles(); + if (playerFolders != null) { + int deletecounter = 0; + long starttime = System.currentTimeMillis(); + for (File playerFolder : playerFolders) { + File[] saveFolders; + saveFolders = playerFolder.listFiles(); + if (saveFolders != null) { + try { + for (File map : saveFolders) { + BasicFileAttributes attr = Files.readAttributes(map.toPath(), BasicFileAttributes.class); + FileTime fileTime = attr.creationTime(); + if (10368000000L + fileTime.toMillis() <= System.currentTimeMillis()) { //120 Tage + FileUtils.deleteDirectory(map); + deletecounter++; + } + } + if (Objects.requireNonNull(playerFolder.listFiles()).length == 0) { + FileUtils.deleteDirectory(playerFolder); + } + } catch (IOException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Fehler beim löschen einer Map", ex); + } + } + } + ChatUtil.sendNormalMessage(p, "Es wurden: " + deletecounter + " maps in " + StringUtil.formatTimespan(System.currentTimeMillis() - starttime) + " gelöscht"); + } + } + return true; + } + + @Override + public String getRequiredPermission() { + return "challenges.clearsaves"; + } +}