Solved Saving a File per player

Discussion in 'Plugin Development' started by rocket138, Dec 3, 2013.

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

    rocket138

    So I was told that the best way to save points would be a /Points/User/*playername*.yml sorta thing
    I've spent like 5 hours on this and I can't figure out why it won't work
    I apologize for my poor code setup all crammed into one class, but I'm just going to post the whole thing, hopefully somebody can explain why it doesn't work at all.

    Code:
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
     
     
    public class Points extends JavaPlugin implements Listener{
     
        private Scoreboard board;
        private Objective o;
        private HashMap<String, Score> scores = new HashMap<String, Score>();
        private ArrayList<String> YAMLS = new ArrayList<String>();
        private ArrayList<FileConfiguration> YAML = new ArrayList<FileConfiguration>();
     
        public void onEnable(){
            board = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
            o = board.registerNewObjective("test", "dummy");
            o.setDisplayName("Stats");
            o.setDisplaySlot(DisplaySlot.SIDEBAR);
            Bukkit.getPluginManager().registerEvents(this, this);
         
       
         
        }
     
        public void onDisable(){
         
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
         
            if(commandLabel.equalsIgnoreCase("slug")){
                        addPoints(player, 1);
            }
         
            return true;
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
     
            p.setScoreboard(board);
         
         
         
         
         
            if (!scores.containsValue(p.getName())) scores.put(p.getName(), o.getScore(Bukkit.getServer().getOfflinePlayer((ChatColor.GREEN + "Points: "))));
        }
     
        private void copy(InputStream in, File file) {
            try {
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
                while((len=in.read(buf))>0){
                    out.write(buf,0,len);
                }
                out.close();
                in.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
     
        public void addPoints(Player p, Integer i){
            if (p.hasPermission("PRO.points")){
                scores.get(p).setScore(scores.get(p).getScore() + (i * 3));
            } else if (p.hasPermission("VIP.points")){
                scores.get(p).setScore(scores.get(p).getScore() + (i * 2));
            } else {
                scores.get(p).setScore(scores.get(p).getScore() + i);
            }
        }
     
        public Score getScore(Player p){
         
            return scores.get(p.getName());
         
        }
     
        @EventHandler
        private void onLeave(PlayerQuitEvent e){
            Player p = e.getPlayer();
         
            File playerFile = new File((this.getDataFolder()) + File.separator + "Players", p.getName() + ".yml");
            if(!playerFile.exists()) { //if their file doesn't exist
                try {
                    FileConfiguration config = null;
                 
                    YAMLS.add(playerFile.getName());
                 
                    playerFile.createNewFile();
                 
                 
                 
                    playerFile.getParentFile().mkdirs();
                    copy(getResource("config.yml"), playerFile);
                 
                    config = YamlConfiguration.loadConfiguration(playerFile);
                 
                    config.set("scores", e.getPlayer().getName() + ":" + getScore(p));
                 
                 
                 
                    try {
                        config.save(playerFile);
                    } catch (IOException e2){
                        e2.printStackTrace();
                    }
                 
                 
                   
                 
                } catch (IOException e1) {
                 
                    e1.printStackTrace();
                }
            }
         
         
        }
     
    }
     
  2. Offline

    M0n0clink

  3. Offline

    masons123456

    The most efficient way would be a MySQL server, but if a Config is necessary, you aren't generating the Config onEnable if it exists or not
     
  4. Offline

    rocket138

    I'll try a MySQL server instead
     
Thread Status:
Not open for further replies.

Share This Page