ArenaManager

Discussion in 'Spigot Plugin Development' started by darkshadow22mc, Jun 4, 2016.

  1. Hi!
    I've started making a SkyWars own plugin for my server. I'd like to create
    files per arenas. I've created a file called data.yml where I will store all the names of the
    arenas. Then, my plan is to create a manager that allows me to read all of the arenas listed in the array.
    The problem is that I don't know how to create that file manager. I have the next code:
    Code (Text):
    package arenaManager;

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.UnsupportedEncodingException;
    import java.util.logging.Level;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    import core.NexusSW;

    public class FileManager {
       
        NexusSW nexus;
        public FileManager(NexusSW nexus){
            this.nexus = nexus;
        }
       
        int numberOfArenas = getArenaFile().getStringList("arenaList").size();
       
        public FileConfiguration arena = null;
        public File arenaFile = null;
       
        /*
         *
         */
        public void reloadArenaFile() {
            if (arenaFile == null) {
            arenaFile = new File(nexus.plugin.getDataFolder(), "data.yml");
            }
            arena = YamlConfiguration.loadConfiguration(arenaFile);

            // Look for defaults in the jar
            Reader defConfigStream = null;
            try {
                defConfigStream = new InputStreamReader(this.getResource("data.yml"), "UTF8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                arena.setDefaults(defConfig);
            }
        }
        public FileConfiguration getArenaFile() {
            if (arena == null) {
                reloadData();
            }
            return arena;
        }
        public void saveArenaFile() {
            if (arena == null || arenaFile == null) {
                return;
            }
            try {
                getData().save(arenaFile);
            } catch (IOException ex) {
                getLogger().log(Level.SEVERE, "Could not save config to " + arenaFile, ex);
            }
        }
        public void saveDefaultArenaFile() {
            if (arenaFile == null) {
                arenaFile = new File(getDataFolder(), "data.yml");
            }
            if (!arenaFile.exists()) {          
                 plugin.saveResource("data.yml", false);
             }
        }
        /*
         *
         */
       
    }
    I'm at a death point. Could someone give me a tip?
    Thanks!​
     
  2. Just saying, but by looking at all the questions you asked, I don't think you are ready to make a minigame :/
     
  3. Please, I've requested help with that problem. Not tips about what should or shouldn't do. Thanks :)
     
  4. Okay,I've developed the next code with my basic knowledge of Java.
    Could someone tell me if it's good?​
    Code (Text):
    package arenaManager;

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.UnsupportedEncodingException;
    import java.util.logging.Level;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    import core.NexusSW;

    public class FileManager {
       
        NexusSW nexus;
        public FileManager(NexusSW nexus){
            this.nexus = nexus;
        }
       
        int numberOfArenas = nexus.getData().getStringList("arenaList").size();
       
        public FileConfiguration arena[] = null;
        public File arenaFile[] = null;
       
        /*
         *
         */
        public void reloadArenaFile(int x) {
            if (arenaFile[x] == null) {
                arenaFile[x] = new File(nexus.plugin.getDataFolder(), nexus.getData().getStringList("arenaList").get(x)+".yml");
            }
            arena[x] = YamlConfiguration.loadConfiguration(arenaFile[x]);

            // Look for defaults in the jar
            Reader defConfigStream = null;
            try {
                defConfigStream = new InputStreamReader(nexus.plugin.getResource("arenatemplate.yml"), "UTF8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                arena[x].setDefaults(defConfig);
            }
        }
        public FileConfiguration getArenaFile(int x) {
            if (arena[x] == null) {
                reloadArenaFile(x);
            }
            return arena[x];
        }
        public void saveArenaFile(int x) {
            if (arena == null || arenaFile == null) {
                return;
            }
            try {
                getArenaFile(x).save(arenaFile[x]);
            } catch (IOException ex) {
                nexus.plugin.getLogger().log(Level.SEVERE, "Could not save config to " + arenaFile, ex);
            }
        }
        public void saveDefaultArenaFile(int x) {
            if (arenaFile[x] == null) {
                arenaFile[x] = new File(nexus.plugin.getDataFolder(), nexus.getData().getStringList("arenaList").get(x)+".yml");
            }
            if (!arenaFile[x].exists()) {          
                 nexus.plugin.saveResource(nexus.getData().getStringList("arenaList").get(x)+".yml", false);
             }
        }
        /*
         *
         */
       
    }
     
     
  5. This may just be a personal preference, but I think it would be better to use ArrayLists instead of Arrays. When you check if arenaFile[x].exists(), then proceed to save the resource, why? Why not just create the file, as there is no data being stored yet so it is useless to save it.