kelson8

ResworldTest

Sep 2nd, 2021 (edited)
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. package net.Kelsoncraft.KBP.testing;
  2.  
  3. import java.io.File;
  4.  
  5. import net.Kelsoncraft.KBP.KbpMain;
  6.  
  7. public class ResworldTest {
  8.  
  9.     static KbpMain plugin;
  10.  
  11.     /** Values obtained from IDP, ServerRestartTimer testing. */
  12.  
  13.     // Resworld deletion settings
  14.     private static final String RESOURCEWORLD_DATANAME = "resworld";
  15.     private static final long RESOURCEWORLD_RESET_SECONDS = 3600 * 24;
  16.    
  17.     protected long delay = 2000;
  18.    
  19.     /**
  20.      * This will mark the given file or directory to be deleted after shutdown.
  21.      * If the given file is a directory, it will recursively go into the given folders.
  22.      * @param curFile
  23.      */
  24.  
  25.     private static void recursiveFileDeletionMarking(File curFile) {
  26.         curFile.deleteOnExit();
  27.         if (curFile.isDirectory()) {
  28.             for (File f : curFile.listFiles()) {
  29.                 recursiveFileDeletionMarking(f);
  30.             }
  31.         }
  32.     }
  33.    
  34.    
  35.     public static void deleteFinal() {
  36.  
  37.         // No more restart time, so let's restart
  38.         /*
  39.         if (delayIdx == ALL_POSSIBLE_DELAYS.length) {
  40.            
  41.             Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "Server restarting now!");
  42.          */
  43.            
  44.             if (shouldDeleteResourceWorld()) {
  45.                 deleteResourceWorld();
  46.             }
  47.  
  48.            
  49.             //plugin.getServer().shutdown();
  50.             //delay = Integer.MAX_VALUE;
  51.         }
  52.    
  53.     /**
  54.      * This will check if resource world should be deleted this restart.
  55.      * If DEBUGMODE is enabled, it will return false.
  56.      * If the world hasn't been reset in over X time, it will return true.
  57.      * @return
  58.      */
  59.    
  60.     private static boolean shouldDeleteResourceWorld() {
  61.  
  62.         // Get last reset time from database.
  63.         String value = plugin.getConfig().getString("resworld-last_reset"); //possibly work?
  64.  
  65.         // Hasn't ever been reset? Yes, its due!
  66.         if (value == null) {
  67.             return true;
  68.         }
  69.  
  70.         try {
  71.             Long lastReset = Long.parseLong(value);
  72.             return (System.currentTimeMillis() > lastReset + RESOURCEWORLD_RESET_SECONDS * 1000);
  73.         } catch (NumberFormatException ex) {
  74.             //InnPlugin.logError("Unable to find get last resource zone reset! Resetting anyway!");
  75.             return true;
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * This will mark the resource world to be deleted on plugin-shutdown.
  81.      * This will then save the last time this world was deleted.
  82.      */
  83.     private static void deleteResourceWorld() {
  84.         // Delete the resworld
  85.         File resworld = new File(RESOURCEWORLD_DATANAME);
  86.         recursiveFileDeletionMarking(resworld);
  87.  
  88.         //InnPlugin.logInfo("Deleted Resource World!");
  89.  
  90.         // Save that resource world was deleted.
  91.         plugin.getConfig().set("resworld-last_reset", String.valueOf(System.currentTimeMillis()));; // Should this work?
  92.         //ConfigValueHandler.saveValue(RESOURCEWORLD_DATANAME, String.valueOf(System.currentTimeMillis()));
  93.     }
  94.  
  95. }
  96.  
Add Comment
Please, Sign In to add comment