Solved Save the level

Discussion in 'Spigot Plugin Development' started by Giorgino, Jan 18, 2020.

  1. Hello everyone, by chance someone knows how do I save the level?
     
  2. What exactly do you mean by „saving the level“?
     
    • Useful Useful x 1
  3. For example, save a player's experience level.
     
  4. What... why do you.
    I don't know... let level implement ConfigurationSerializable then creat a static method "deserialize" that accepts a single Map< String, Object> and returns the class?
    Just dont forget to call ConfigurationSerialization.registerClass(Level.class)?
     
    • Useful Useful x 1
  5. Oh ok.
    Create a file per player that is named after his uuid. Then write a value inside using the Yaml configuration API from Spigot.
    When loading just get the file 'uuid'.yml and load the value(s) again.
     
    • Winner Winner x 1
  6. I think you have to elaborate a bit more on what you mean by "save".
     
    • Useful Useful x 1
  7. Hi, try this... it seems to work

    Code (Java):
    public class Experience extends JavaPlugin implements Listener {

        public File players;

        public FileConfiguration pl;

        public void onEnable() {

            Bukkit.getPluginManager().registerEvents(this, this);

            players = new File("players.yml");

            if (!players.exists()) {

                try {

                    players.createNewFile();

                } catch (Exception e) {

                    System.out.println("[Experience] An error as occured while creating the players.yml file.");

                }

            }

            YamlConfiguration.loadConfiguration(players);

        }

        @EventHandler
        public void onJoin(PlayerJoinEvent e) {

            if (isNewPlayer(e.getPlayer())) {

                pl.set(e.getPlayer().getUniqueId().toString(), e.getPlayer().getExpToLevel());

            }

        }

        @EventHandler
        public void expModify(PlayerExpChangeEvent e) {

            pl.set(e.getPlayer().getUniqueId().toString(), e.getPlayer().getExpToLevel());

        }

        public boolean isNewPlayer(Player player) {

            try {

                pl.getString(player.getUniqueId().toString());

                return false;

            } catch (Exception e) {

                return true;

            }

        }

    }
     
    • Winner Winner x 1
  8. Hm ... I had already solved, but I forgot to change the title. Anyway, thanks anyway ..