Solved Is this the most efficient way to make this?

Discussion in 'Plugin Development' started by Offlical, Mar 13, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Offlical

    Hey folks!

    A bit new here to the Bukkit API so please don't go too harsh on me, So i've been developing a plugin that has boosters, that help you gain more exp/coins/whatever

    Now i've wondered if this is the most efficient way of doing this or is system time better or is there a complete different more efficient way of doing this?
    (this method is later on in a BukkitRunnable)

    Code:
        public void pbooster() {
            for(Object a : plugin.plboosterlist.toArray()) {
                if(a instanceof PersonalBooster) {
                    PersonalBooster booster = (PersonalBooster) a;
                    for(Player p : Bukkit.getOnlinePlayers()) {
                        if(p.getUniqueId().equals(booster.getPlayer().getUniqueId())) {
                            int tmlft = booster.getTimeleft();
                            String type =  booster.getType();
                           
                            if(!(tmlft <= 0)) {
                                tmlft = tmlft -1;
                                booster.setTimeleft(tmlft);
                                plugin.getConfig().set("ActivePersonalBoosters." + type + "." + booster.getPlayer().getUniqueId() + ".timeleft", tmlft);
                                plugin.saveConfig();
                                plugin.plboosterlist.remove(booster);
                                plugin.plboosterlist.add(booster);
                                return;
                               
                            }else {
                                plugin.getConfig().set("UnfinishedBoosters." + booster.getPlayer().getUniqueId() + "." + type + ".timeleft", null);
                                plugin.getConfig().set("UnfinishedBoosters." + booster.getPlayer().getUniqueId() + ".LastBoosterType" , null);
                                plugin.getConfig().set("ActivePersonalBoosters." + type + "." + booster.getPlayer().getUniqueId() + ".timeleft", null);
                                plugin.getConfig().set("ActivePersonalBoosters." + type + "." + booster.getPlayer().getUniqueId() + ".active", null);
                                plugin.saveConfig();
                                plugin.plboosterlist.remove(booster);
                                return;
                               
                            }
                           
                           
                        }
                    }
                   
                }
               
            }
        }   
        
    (UnfinishedBoosters is for if the player logs off, it still keeps the duration for him until he logs back on so it doesn't run out when he's not using it)

    Cheers, Offlical
     
  2. Online

    timtower Administrator Administrator Moderator

    @Offlical Why are you converting a list to an array?
    And there is no need to remove and add the booster if it isn't a clone of something (the list to array part)
     
  3. Offline

    Tango_

    Only thing I would mention is that in a large server, you probably don't want to do lots of config saving for performance reasons, best way is to store this at runtime in a player data object, and then save when the player leaves / server closes, and ofcourse load it when the player joins again. Try looking into individual player files too.
     
  4. Offline

    Offlical

    Well i looked on my code and quickly realized that putting stuff into the config and back is completely useless and just takes more time off the server, and just removed that, if there's anything else thats more efficient let me know, but i don't really need help anymore
     
Thread Status:
Not open for further replies.

Share This Page