Can't load the EXP from the config?

Discussion in 'Plugin Development' started by PlEAseHelpMe333, Mar 30, 2016.

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

    PlEAseHelpMe333

    If you asking, yes its showing me the amount of exp in the config...

    Code:
    package daniel;
    
    import java.io.File;
    import java.io.IOException;
    import java.lang.ref.WeakReference;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import org.apache.commons.lang.Validate;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class SaveInventory implements Listener {
     
        @EventHandler
        public void BLOCKED_COMMANDS(PlayerCommandPreprocessEvent e) {
            Player p = e.getPlayer();
            String cmd = e.getMessage();
            YamlConfiguration config = YamlConfiguration
                    .loadConfiguration(new File(Utils.DATABASE + p.getUniqueId()
                            + Utils.DATABASE_STATS));
            if (cmd.equalsIgnoreCase("/saveinv")) {
                ArrayList<ItemStack> list = new ArrayList<>();
                ItemStack[] contents = p.getInventory().getContents();
                for (int i = 0; i < contents.length; i++) {
                    ItemStack item = contents[i];
                    if (!(item == null)) {
                        list.add(item);
                    }
                }
                config.set("inventory", list);
                config.set("armor.helmet", p.getInventory().getHelmet());
                config.set("armor.chestplate", p.getInventory().getChestplate());
                config.set("armor.leggings", p.getInventory().getLeggings());
                config.set("armor.boots", p.getInventory().getBoots());
                config.set("health", p.getHealth());
                config.set("hunger", p.getFoodLevel());
             
                // LEVELS, EXP
                config.set("exp.level", p.getLevel());
                config.set("exp.exp", p.getExp());
             
                Utils.REMOVE_ARMOUR(p);
                Utils.REMOVE_INVENTORY(p);
                Utils.REMOVE_POTIONS(p);
                p.setLevel(0);
                p.setExp(0);
                p.setHealth(20);
                p.setFoodLevel(20);
                try {
                    config.save(new File(Utils.DATABASE + p.getUniqueId()
                            + Utils.DATABASE_STATS));
                } catch (IOException ee) {
                    ee.printStackTrace();
                }
            }
            if (cmd.equalsIgnoreCase("/loadinv")) {
                Utils.REMOVE_ARMOUR(p);
                Utils.REMOVE_INVENTORY(p);
                Utils.REMOVE_POTIONS(p);
                ItemStack[] contents = p.getInventory().getContents();
                List<?> list = config.getList("inventory");
                for (int i = 0; i < list.size(); i++) {
                    contents[i] = (ItemStack) list.get(i);
                }
                p.getInventory().setContents(contents);
                p.getInventory().setHelmet((ItemStack) config.get("armor.helmet"));
                p.getInventory().setChestplate(
                        (ItemStack) config.get("armor.chestplate"));
                p.getInventory().setLeggings(
                        (ItemStack) config.get("armor.leggings"));
                p.getInventory().setBoots((ItemStack) config.get("armor.boots"));
                p.setHealth((double) config.get("health"));
                p.setFoodLevel((int) config.get("hunger"));
             
                // LEVELS, EXP
                p.setLevel((int) config.get("exp.level"));
                p.setExp((int) config.get("exp.exp"));
             
                try {
                    config.save(new File(Utils.DATABASE + p.getUniqueId()
                            + Utils.DATABASE_STATS));
                } catch (IOException ee) {
                    ee.printStackTrace();
                }
            }
    
        }
    }
    
     
  2. Offline

    FabeGabeMC

    @PlEAseHelpMe333 You should probably use getInt() instead of get() and casting, saves you time too, plus, it's really bad practice to cast without checking anyways.
     
  3. Offline

    mine-care

    Ahh got quite a few corrections and possible improvlements to sugest:

    First of all, follow Java naming conventions (Method names follow cammelCapitalization)
    Its a bad idea to load a config every time a command is performed.
    It is equally bad to save the file every time a command is performed :/

    Yeah, it is a very very bad idea to cast without checking :S

    EDIT: use the onCommand method instead of the CommandPreProcessEvent.
     
    Last edited: Mar 30, 2016
    Zombie_Striker and bwfcwalshy like this.
Thread Status:
Not open for further replies.

Share This Page