1.8.8 Daily Reward time reset after reload?

Discussion in 'Spigot Plugin Development' started by DasChaos44, Mar 16, 2020.

  1. Hello everyone,

    i'm writing a Lobby System with a Daily Reward, it works but if I do a reload I can pick up the daily reward again. Can I set the time via MySQL or a file so that the time is not reset when reloading?

    Some variables are in German because, I am German Guy.

    Here's the Code:
    Code (Java):
    if(e.getCurrentItem().getItemMeta().getDisplayName().equals("§6    -= §bDaily Rewards §6=-     ")) {
                                long jetzt = System.currentTimeMillis();
                                if(tagcooldown.containsKey(p.getName())) {
                                    long be = tagcooldown.get(p.getName());
                                    int rest = (int) ((be + (24*1000*60*60)) - jetzt);
                                    if(rest > 0) {
                                        int stunde = rest/1000/60/60;
                                        rest = rest-(stunde*1000*60*60);
                                        int min = rest/1000/60;

                                        if(language_config.getString("Language." + p.getName()).equals(Prefix.englishPrefix)) {
                                            p.sendMessage(Prefix.dailyrewardPrefix + "§cYou have §6" + stunde + (stunde == 1 ? " hour" : " hours") + "§c and§6 " + min
                                                    + (min == 1 ? " minute" : "minutes") + "§c to wait.");
                                        }
                                        if(language_config.getString("Language." + p.getName()).equals(Prefix.germanPrefix)) {
                                            p.sendMessage(Prefix.dailyrewardPrefix + "§cDu musst noch §6" + stunde + (stunde == 1 ? " Stunde" : " Stunden") + "§c und§6 " + min
                                                    + (min == 1 ? " Minute" : " Minuten") + "§c warten.");
                                        }
                                        p.playSound(p.getLocation(), Sound.VILLAGER_NO, 1, 1);
                                        return;
                                    }
                                }
                                int coins = 1000;
                                CoinAPI.addCoins(p, coins);
                                p.playSound(p.getLocation(), Sound.LEVEL_UP, 1, 1);
                                if(language_config.getString("Language." + p.getName()).equals(Prefix.englishPrefix)) {
                                    p.sendMessage(Prefix.dailyrewardPrefix + "§aYou have successfully collected the daily reward.");
                                }
                                if(language_config.getString("Language." + p.getName()).equals(Prefix.germanPrefix)) {
                                    p.sendMessage(Prefix.dailyrewardPrefix+ "§aDu hast dir erfolgreich deine Tägliche Belohnung abgeholt!");
                                }
                                tagcooldown.put(p.getName(), jetzt);
                                p.closeInventory();
                            }
     
  2. Hello,

    Save your tagcooldown map into a yaml file, or into your MySQL database.
    When the plugin load (onEnable), you'll can easily load the cooldowns into your map.
     
  3. How do i put my tagcooldown map in my mysql?

    Code (Java):
    public static int getCooldown(Player p) {
            try {
                PreparedStatement ps = (PreparedStatement) MySQL.getStatement("SELECT * FROM coinplayer WHERE UUID= ?");
                ps.setString(1, p.getUniqueId().toString());
                ResultSet rs = ps.executeQuery();
                rs.next();
                int cooldown = rs.getInt("dailyrewardcooldown")     ;
                rs.close();
                ps.close();
                return cooldown;
            }catch (Exception ex) {
                ex.printStackTrace();
            }
            return -1;
        }

        public static void setCooldown(Player p, int cooldown) {
            try {
                PreparedStatement ps = (PreparedStatement) MySQL.getStatement("UPDATE coinplayer SET dailyrewardcooldown= ? WHERE UUID= ?");
                ps.setInt(1, cooldown);
                ps.setString(2, p.getUniqueId().toString());
                ps.executeUpdate();
                ps.close();
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        public static int getCooldown(String name) {
            try {
                PreparedStatement ps = (PreparedStatement) MySQL.getStatement("SELECT * FROM coinplayer WHERE playername= ?");
                ps.setString(1, name);
                ResultSet rs = ps.executeQuery();
                rs.next();
                int cooldown = rs.getInt("dailyrewardcooldown")     ;
                rs.close();
                ps.close();
                return cooldown;
            }catch (Exception ex) {
                ex.printStackTrace();
            }
            return -1;
        }

        public static void setCooldown(String name, int cooldown) {
            try {
                PreparedStatement ps = (PreparedStatement) MySQL.getStatement("UPDATE coinplayer SET dailyrewardcooldown= ? WHERE playername= ?");
                ps.setInt(1, cooldown);
                ps.setString(2, name);
                ps.executeUpdate();
                ps.close();
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    MySQL

    Code (Java):
    public static void createTable() {
            try {
                PreparedStatement ps = MySQL.getStatement("CREATE TABLE IF NOT EXISTS coinplayer (playername VARCHAR(100), UUID VARCHAR(100), coins INT(100), " +
                        "flyperk TINYINT(0), speedperk TINYINT(0), doublejumpperk TINYINT(0), dailyrewardcooldown)");
                ps.executeUpdate();
            }catch(Exception e) {
                e.printStackTrace();
            }
        }

        public static void create(Player p) {
            try {
                PreparedStatement ps = MySQL.getStatement("INSERT INTO coinplayer (playername, UUID, coins, flyperk, speedperk, doublejumpperk, dailyrewardcooldown)" +
                        " VALUES (?, ?, ?, ?, ?, ?, ?)");
                ps.setString(1, p.getName());
                ps.setString(2, p.getUniqueId().toString());
                ps.setInt(3, 1000);
                ps.setBoolean(4, false);
                ps.setBoolean(5, false);
                ps.setBoolean(6, false);
                ps.setInt(7, 0);
                ps.executeUpdate();
                ps.close();
            }catch(Exception e) {
                e.printStackTrace();
            }
        }

        public static boolean isExisting(Player p) {
            try {
                PreparedStatement ps = MySQL.getStatement("SELECT * FROM coinplayer WHERE UUID= ?");
                ps.setString(1, p.getUniqueId().toString());
                ResultSet rs = ps.executeQuery();
                boolean user = rs.next();
                rs.close();
                ps.close();
                return user;
            }catch(Exception ex) {
                ex.printStackTrace();
            }
            return false;
        }

        public static boolean isExisting(String name) {
            try {
                PreparedStatement ps = MySQL.getStatement("SELECT * FROM coinplayer WHERE playername= ?");
                ps.setString(1, name);
                ResultSet rs = ps.executeQuery();
                boolean user = rs.next();
                rs.close();
                ps.close();
                return user;
            }catch(Exception ex) {
                ex.printStackTrace();
            }
            return false;
        }
     
  4. You could use java Date.
    Save the date the player took his reward and set the reward as not claimable by a bool or something. Then save another date which will be in the future serving as your cooldown. You could use a runnable and check every minute if the second cooldown is in the past, if so remove both dates and set the reward claimable again.