Save Timer Counts On Reload/Shutdown

Discussion in 'Plugin Development' started by OMGitzFROST, Dec 27, 2021.

  1. Offline

    OMGitzFROST

    Ok so basically i am making a plugin that contains multiple runnables, but i am running into an issue where every time i reload or restart the server, the tasks count get reset, and it starts over, i need it to save its timer since it will be used again on startup therefore i don't want the counter to start over again, If the execution time is 1 day, and they are 23 hours into the timer, i don't want them to have to start from 0 again if that makes sense. here is a example class of what i have so far.

    PHP:
    package com.frostdeveloper.playerlogs.module;

    import com.frostdeveloper.playerlogs.manager.CacheManager;
    import com.frostdeveloper.playerlogs.manager.ModuleManager;
    import com.frostdeveloper.playerlogs.model.Module;
    import com.frostdeveloper.playerlogs.model.Scheduler;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scheduler.BukkitTask;

    public class 
    ExampleModule extends ModuleManager implements ModuleScheduler
    {
        
    // CLASS INSTANCES
        
    private final CacheManager cache plugin.getCacheManager();
       
        
    // CLASS SPECIFIC OBJECTS
        
    private final String identifier "example-module";
        private final 
    String cacheIdentifier "example-timer";
        private static 
    BukkitTask task;
       
        @
    Override
        
    public void initialize() { start(); }
       
        @
    Override
        
    public void start()
        {
            
    String cachedTimer cache.getCache(cacheIdentifier) != null cache.getCache(cacheIdentifier) : "0";
           
            
    task  = new BukkitRunnable() {
                
    int counter Integer.parseInt(cachedTimer);
               
                @
    Override
                
    public void run() {
                    final 
    int interval api.toMinute(30); // EVERY 30 MINUTES
                   
                    // STOP TASK IN-CASE THE DATA FOLDER IS DELETED
                    
    if (!plugin.getDataFolder().exists() || !isRegistered()) {
                        
    shutdown();
                        return;
                    }
                   
                    
    // IF COUNTER IS GREATER THAN INTERVAL, DELETE CACHE.
                    
    if (counter interval) {
                        
    cache.deleteCache(cacheIdentifier);
                    }
                   
                    
    // IF COUNTER IS LESS THAN INTERVAL, ADD TO COUNTER AND SET CACHE
                    
    if (counter interval) {
                        
    counter++;
                        
    cache.setCache(cacheIdentifiercounter);
                    }
                    else {
                        
    System.out.println("This message is shown when counter hits the desired interval"); /* TODO TEMP */
                       
                        
    counter 0;
                        
    cache.setCache(cacheIdentifiercounter);
                    }
                }
            }.
    runTaskTimer(plugin020);
        }
       
        @
    Override
        
    public void registerModule()
        {
            
    addToMaster(this);
           
            if (
    getConfig().getBoolean("example-task.enabled")) {
                
    addToRegistry(this);
            }
        }
       
        @
    Override
        
    public void shutdown()
        {
            
    cancel();
           
            if (
    isCancelled()) {
                
    plugin.debug(getClass(), "module.unload.success"identifier);
            }
        }
       
        @
    Override
        
    public boolean isRegistered() { return getRegisteredList().contains(this); }
       
        public 
    String getIdentifier() { return identifier; }
       
        @
    Override
        
    public int getTaskId() { return getRegisteredList().indexOf(this); }
       
        @
    Override
        
    public boolean isCancelled() { return task == null || task.isCancelled(); }
       
        @
    Override
        
    public void cancel()
        {
            if (
    task != null) {
                
    task.cancel();
            }
        }
    }
    Currently, this code will save the current count every second to a file in my plugin folder, but i would like to avoid this since i do not want the server owner to be able to modify the timer. Is there a better way to store my timers?
     
  2. Offline

    ForbiddenSoul

    If you just want to make sure the serer op can not modify the values you can use encryption, or encoding.
    Search the internet for some simple string encryption.
    You can also use this code snippet I wrote to save to a "Flat File" instead of as text. It saves as a GZip compressed binary file, which will just look like gobbledy-gook if opened with a text editor.
    https://bukkit.org/threads/flat-file-generic-save-load.489508/
     
  3. Offline

    timtower Administrator Administrator Moderator

    @OMGitzFROST Why not save when the timer should end?

    @ForbiddenSoul Bukkit has a build-in configuration system perfect for this stuff.
     

Share This Page