YamlConfiguration help

Discussion in 'Spigot Plugin Development' started by SpoderMan, Jul 2, 2015.

  1. Okay so I edited my code as much as possible & attempted this & yet still this
    • Doesn't create the players.yml
    • Doesn't do anything with the emfile.set("Players." + p.getUniqueId().toString(), Integer.valueOf(0));
    Here's my Main.Main beginning:
    Code (Text):
    package Main;
    import ActionBarAPI.ActionBarAPI;
    import HoverAPI.FancyMessage;
    import SignEvents.SignClick;
    import SignEvents.SignCreate;

    import java.io.File;
    import java.io.IOException;
    import java.sql.Connection;

    import listeners.BlockBreak;
    import listeners.RightClickToken;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    import stats.Stats;
    public class Main
      extends JavaPlugin
    {
      public static Connection connection;
      public static FileConfiguration config;
      public static FileConfiguration emfile;
      public static File file;
      public static File mfile;
      static Main plugin;
      @SuppressWarnings({ "unused", "deprecation" })
    public void onEnable()
      {
        getConfig().options().copyDefaults(true);
        saveDefaultConfig();
        config = getConfig();
        emfile = YamlConfiguration.loadConfiguration(mfile);
        file = new File(getDataFolder(), "config.yml");
        mfile = new File(getDataFolder(), "players.yml");
        SignClick.loadEnchants();
        getServer().getPluginManager().registerEvents(new BlockBreak(this), this);
        getServer().getPluginManager().registerEvents(new SignClick(this), this);
        getServer().getPluginManager().registerEvents(new SignCreate(this), this);
        getServer().getPluginManager().registerEvents(new RightClickToken(this), this);
        Player[] arrayOfPlayer;
        int localIOException1 = (arrayOfPlayer = Bukkit.getOnlinePlayers()).length;
        for (int e = 0; e < localIOException1; e++)
        {
          Player p = arrayOfPlayer[e];
          Stats player;
          if (!emfile.contains("Players." + p.getUniqueId().toString()))
          {
            player = new Stats(p.getUniqueId().toString(), p.getName(), 0);
            emfile.set("Players." + p.getUniqueId().toString(), Integer.valueOf(0));
            try
            {
                emfile.save(mfile);
            }
            catch (IOException e1)
            {
              e1.printStackTrace();
            }
            }
          else
          {
            player = new Stats(p.getUniqueId().toString(), p.getName(), emfile.getInt("Players." + p.getUniqueId().toString()));
          }
          }
        if (!mfile.exists()) {
          try
          {
            mfile.createNewFile();
          }
          catch (IOException e)
          {
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("+--------------------------------------+");
              Bukkit.getServer().getLogger().severe("  > eTokens failed to create");
              Bukkit.getServer().getLogger().severe("  > players.yml please reload");
              Bukkit.getServer().getLogger().severe("  > your server or the plugin");
              Bukkit.getServer().getLogger().severe("+--------------------------------------+");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
          }
        }
        if (!emfile.contains("TokensMessage"))
        {
          try
          {
            emfile.save(mfile);
          }
          catch (IOException e)
          {
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("+--------------------------------------+");
              Bukkit.getServer().getLogger().severe("  > eTokens failed to create");
              Bukkit.getServer().getLogger().severe("  > players.yml please reload");
              Bukkit.getServer().getLogger().severe("  > your server or the plugin");
              Bukkit.getServer().getLogger().severe("+--------------------------------------+");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
              Bukkit.getServer().getLogger().severe("  ");
          }
        }
      }
     
      @SuppressWarnings("deprecation")
    public void onDisable()
      {
        Player[] arrayOfPlayer;
        int j = (arrayOfPlayer = Bukkit.getOnlinePlayers()).length;
        for (int i = 0; i < j; i++)
        {
          Player p = arrayOfPlayer[i];
          if (!emfile.contains("Players." + p.getUniqueId().toString()))
          {
              emfile.set("Players." + p.getUniqueId().toString(), Integer.valueOf(0));
          }
          else
          {
              emfile.set("Players." + p.getUniqueId().toString(), Integer.valueOf(((Stats)Stats.players.get(p.getUniqueId().toString())).getTokens()));
            try
            {
                emfile.save(mfile);
            }
            catch (IOException e1)
            {
              e1.printStackTrace();
            }
            Stats.players.remove(p.getUniqueId().toString());
          }
        }
        try
        {
          if ((connection != null) && (connection.isClosed())) {
            connection.close();
          }
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
     
    Now here's my stats.Stats which makes the players token balance:
    Code (Text):
    package stats;
    import java.util.HashMap;
    public class Stats
    {
      public static HashMap<String, Stats> players = new HashMap<String, Stats>();
      private String name;
      private String uuid;
      private int tokens;
      private int blocksBroken;
      public Stats(String uuid, String name, int tokens)
      {
        this.uuid = uuid;
        this.name = name;
        this.tokens = tokens;
        players.put(uuid, this);
      }
      public String getName()
      {
        return this.name;
      }
      public String getUUID()
      {
        return this.uuid;
      }
     
      public int getTokens()
      {
        return this.tokens;
      }
      public void setTokens(int tokens)
      {
        this.tokens = tokens;
      }
      public int getBlocksBroken()
      {
        return this.blocksBroken;
      }
      public void setBlocksBroken(int blocks)
      {
        this.blocksBroken = blocks;
      }
    }
    Now I also have an onPlayerJoin & Quit event here:
    Code (Text):
      @EventHandler
      public void onPlayerJoin(PlayerJoinEvent e)
      {
        Player p = e.getPlayer();
        if (!Main.emfile.contains("Players." + p.getUniqueId().toString()))
        {
          Main.emfile.set("Players." + p.getUniqueId().toString(), Integer.valueOf(0));
          try
          {
            Main.emfile.save(Main.mfile);
          }
          catch (IOException e1)
          {
            e1.printStackTrace();
          }
        }
      }
     
      @EventHandler
      public void onPlayerQuit(PlayerQuitEvent e)
      {
        Player p = e.getPlayer();
        if (!Main.emfile.contains("Players." + p.getUniqueId().toString()))
        {
          Main.emfile.set("Players." + p.getUniqueId().toString(), Integer.valueOf(0));
          try
          {
            Main.emfile.save(Main.mfile);
          }
          catch (IOException e1)
          {
            e1.printStackTrace();
          }
        }
        else
        {
          Main.emfile.set("Players." + p.getUniqueId().toString(), Integer.valueOf(((Stats)Stats.players.get(p.getUniqueId().toString())).getTokens()));
          try
          {
            Main.emfile.save(Main.mfile);
          }
          catch (IOException e1)
          {
            e1.printStackTrace();
          }
          Stats.players.remove(p.getUniqueId().toString());
        }
      }
    I seriously have no idea why this isn't working, please help.
     
  2. Creeoer

    Supporter

    Wouldn't emfile require at least 1 instance of your main to actually work..try creating 1 static instance of your class and use that to reference to your emfile.
     
  3. I'm a decent dev but I don't know coding terminology, can you give me an example lol
     
  4. That or would any of you know how to make another folder then have the files as there UUID & just keep there balance in there?
     
  5. I'm an above decent dev, and I don't use multiple configs and confuse myself… if you're as good a dev as khobbits (maybe not THAT good but still) then you should be using multiple configs. Otherwise just stick to one basic config
     
  6. This is an old way I used to do this. It's pretty straight-forward and simple for beginners.

    Code (Text):

    public class PlayerConfig extends YamlConfiguration {
            private static PlayerConfig config;

            public static PlayerConfig getConfig(MyPlugin plugin) {
                    if (config == null) {
                            config = new PlayerConfig (plugin);
                    }
                    return config;
            }

            private File file = null;
            private Object saveLock = new Object();

            public PlayerConfig (MyPlugin plugin) {
                    super();
                    file = new File(plugin.getDataFolder(), "players.yml");
                    load();
            }

            private void load() {
                    synchronized (saveLock) {
                            try {
                                    this.load(file);
                            } catch (Exception ignore) {
                            }
                    }
            }

            public void forceSave() {
                    synchronized (saveLock) {
                            try {
                                    save(file);
                            } catch (Exception ignore) {
                            }
                    }
            }
    }
     

    Just call the getConfig() static method from the class, and use it like you would normally do plugin.getConfig(). Example:

    Code (Text):

        PlayerConfig pConfig = PlayerConfig.getConfig(this); // "this" being an instance to your plugin's main class.
        player.sendMessage("Integer: " + pConfig.getInt(player.getUniqueId()));
     
    You can also pConfig.set() to it, just like on plugin.getConfig().

    It's designed so you can call it like this from anywhere and it will reuse itself for efficiency if its already been loaded once.

    ;)