Save Arena to Config

Discussion in 'Plugin Development' started by Irantwomiles, Aug 20, 2016.

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

    Irantwomiles

    Hello everyone. Getting straight to the point, I can't seem to be able to save my arenas to my config. Everytime I try it, it gives me a NPE. Here are my two classes

    Area Class
    Code:
    package me.iran.simplearenas;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    
    public class Arena {
    
        private int id;
        private Location player1;
        private Location player2;
        private String name;
        private List<String> players = new ArrayList<String>();
    
        public Arena(int id, String name) {
            this.name = name;
            this.id = id;
        }
    
        public Location setPlayer1(Location player1) {
            return this.player1 = player1;
        }
       
        public Location setPlayer2(Location player2) {
            return this.player2 = player2;
        }
       
        public Location locPlayer1() {
            return this.player1;
        }
       
    
        public Location locPlayer2() {
            return this.player2;
        }
       
        public int getId() {
            return this.id;
        }
       
        public List<String> getPlayers() {
            return this.players;
        }
    
    }
    
    ArenaManager
    Code:
    package me.iran.simplearenas;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    
    public class ArenaManager {
    
        SimpleArenas plugin;
        private List<Arena> arenas = new ArrayList<Arena>();
       
        int arenaSize = 0;
       
        public void loadArena() {
            for(int i = 0; i < arenaSize; i++) {
                Arena a = this.getArena(i);
                arenas.add(a);
            }
        }
       
        public Arena getArena(int i) {
            for(Arena a : this.arenas) {
                if(a.getId() == i) {
                    return a;
                }
            }
            return null;
        }
    
        public void addPlayer1(Player player, int id) {
                Arena a = this.getArena(id);
           
                if(a == null) {
                    player.sendMessage("Couldn't find arena");
                    return;
                }
               
                if(inArena(player, id)==true) {
                    player.sendMessage("Already in arena");
                    return;
                }
               
                if(a.locPlayer1() == null) {
                    player.sendMessage("Null location");
                    return;
                }
               
                a.getPlayers().add(player.getName());
                player.teleport(a.locPlayer1());
                player.sendMessage(ChatColor.GREEN + "Added to arena " + id);
               
        }
       
        public void addPlayer2(Player player, int id) {
            Arena a = this.getArena(id);
       
            if(a == null) {
                player.sendMessage("Couldn't find arena");
                return;
            }
           
            if(inArena(player, id)==true) {
                player.sendMessage("Already in arena");
                return;
            }
           
            if(a.locPlayer2() == null) {
                player.sendMessage("Null location");
                return;
            }
           
            a.getPlayers().add(player.getName());
            player.teleport(a.locPlayer2());
            player.sendMessage(ChatColor.GREEN + "Added to arena " + id);
           
    }
       
        public void removePlayer(Player player, int id) {
            Arena a = this.getArena(id);
           
            if(a == null) {
                player.sendMessage("Couldn't find arena");
                return;
            }
           
            if(inArena(player, id)==false) {
                player.sendMessage("Not in arena");
                return;
            }
           
            a.getPlayers().remove(player.getName());
            player.sendMessage(ChatColor.GREEN + "Removed from arena " + id);
        }
       
        public Arena createArena(String name) {
            arenaSize++;
            Arena a = new Arena(this.arenaSize, name);
           
            this.arenas.add(a);
           
            return a;
        }
       
       
        public boolean inArena(Player player, int id) {
            Arena a = this.getArena(id);
           
            if(a == null) {
                player.sendMessage("Arena doesn't exit");
                return false;
            }
           
            if(a.getPlayers().contains(player.getName())) {
                return true;
            }
            return false;
           
        }
       
        public void setPlayer1(int id, Location loc, Player player) {
            Arena a = this.getArena(id);
           
            if(a == null) {
                player.sendMessage("could not find arena");
                return;
            }
            a.setPlayer1(loc);
            player.sendMessage("Set player1 for id " + id);
        }
       
        public void setPlayer2(int id, Location loc, Player player) {
            Arena a = this.getArena(id);
           
            if(a == null) {
                player.sendMessage("could not find arena");
                return;
            }
           
            a.setPlayer2(loc);
            player.sendMessage("Set player2 for id " + id);
        }
    
    }
    
    I followed a tutorial that was posted here but I added some things my self to make it what I wanted. Everything works fine but I really need to be able to save it to the config. I've tried but failed. Any tips?
     
  2. Offline

    HeartandSoul

    Although people more advanced then me may find it, it helps if you paste your stack trace! The stack trace tells us what line the NPE is on, and inheritance (If there's any).
     
  3. Offline

    aztk

    Well, i don't see in code that you've posted any line that would save something to the config (or i'm blind).
    However, i would recommend using something like this: http://pastebin.com/GdpBwdi7
    And add in onEnable(): classname.load(); and in onDisable(): classname.save();
    (replace classname with name of class where you added this code)
    PS. dont copypaste mindlessly, it might contain some small bugs cause I didn't write it in eclipse
    PS2. it will create new file for every arena
     
Thread Status:
Not open for further replies.

Share This Page