Solved Team Plugin

Discussion in 'Spigot Plugin Development' started by TheWackyTV, Jun 23, 2016.

  1. Hello, I am trying to make a team plugin, but there is a lot of errors, I would hear if anyone could help me.

    Team class:
    Code (Text):
    Core plugin;
        public Team(Core instance) {
            plugin = instance;
        }

        public ArrayList<String> teams = new ArrayList<>();
        public HashMap<String, List<String>> teamMembers = new HashMap<>();
        public HashMap<String, String> teamLeaders = new HashMap<>();

        public void createTeam(String name, Player creator) {
            if(checkIfPlayerHasATeam(creator.getName())) {
                creator.sendMessage("Leave your current team");
                return;
            }
            if(teams.contains(name)) {
                creator.sendMessage("The name is already taken");
                return;
            }
            if(name.equalsIgnoreCase("null")) {
                creator.sendMessage("The name cannot be 'null'");
                return;
            }
            creator.sendMessage("§aYou created a team: §c" + name);
            String player = creator.getName();
            String teamName = name;
            List<String> members = new ArrayList<String>();

            teams.add(teamName);
            teamMembers.put(teamName, members);
            teamLeaders.put(teamName, player);
        }

        public void saveTeams() {
            for(String t : teams) {
                plugin.getConfig().set("teams." + t, t);
                plugin.getConfig().set("teams." + t + ".members", teamMembers.get(t));
                plugin.getConfig().set("teams." + t + ".kills", getTeamKills(t));
                plugin.getConfig().set("teams." + t + ".leader", teamLeaders.get(t));
                plugin.saveConfig();
            }
        }

        public int getTeamKills(String team) {
            return plugin.getConfig().getInt("teams." + team + ".kills");
        }

        public String getPlayerTeam(String player) {
            String teamName = "No team";
            for(String s : teams) {
                if(getTeamMembers(s).contains(player) || getTeamLeader(s).contains(s)) {
                    teamName = s;
                }
            }
            return teamName;
        }

        public String getPlayerPrefixTeam(String player) {
            return getPlayerTeam(player);
        }

        public String getTeamLeader(String team) {
            return plugin.getConfig().getString("teams." + team + ".leader");
        }

        public List<String> getTeamMembers(String team) {
            return plugin.getConfig().getStringList("teams." + team + ".members");
        }

        public void loadTeams() {
            for(String s : plugin.getConfig().getConfigurationSection("teams").getKeys(false)) {
                teams.add(s);
                teamMembers.put(s, getTeamMembers(s));
                teamLeaders.put(s, getTeamLeader(s));
            }
        }

        public void increaseTeamKills(String team) {
            plugin.getConfig().set("teams." + team + ".kills", getTeamKills(team) + 1);
            plugin.saveConfig();
        }

        public void removeTeam(String team) {
            if(teams.contains(team)) {
                teams.remove(team);
                teamLeaders.remove(team);
                teamMembers.remove(team);
                plugin.getConfig().set("teams." + team, null);
                plugin.saveConfig();
            }
        }

        public void leaveTeam(String player) {
            for(String s : teams) {
                    if(getTeamMembers(s).contains(player)) {
                        teamMembers.remove(s, player);
                    } else if(getTeamLeader(s).contains(player)) {
                        removeTeam(s);
                    }
            }
        }

        public boolean checkIfPlayerHasATeam(String player) {
            String name = player;
            boolean isInATeam = false;
            for(String s : teams) {
                if(getTeamLeader(s).equals(name) || getTeamMembers(s).contains(name)) {
                    isInATeam = true;
                } else {
                    isInATeam = false;
                }
            }
            return isInATeam;
        }

    Chat Listeners:
    Code (Text):
    Team teamManagaer;
        public chatListener(Team instance) {
            teamManagaer = instance;
    [SPOILER="Error 1"][/SPOILER]    }

        @EventHandler
        public void chat(AsyncPlayerChatEvent e) {
            String oldFormat = e.getFormat();
            e.setFormat(teamManagaer.getPlayerPrefixTeam(e.getPlayer().getName()) + oldFormat);
        }

    Command Class
    Code (Text):
    Team teamManagaer;
        public createTeam(Team instance) {
            teamManagaer = instance;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage("Nej!");
                return true;
            }
            if(command.getName().equalsIgnoreCase("team")) {
                Player player = (Player) sender;
                if(args.length < 1) {
                    player.sendMessage("/team create <navn>");
                } else if(args.length == 2) {
                    if(args[0].equalsIgnoreCase("create")) {
                        String teamName = args[1];
                        teamManagaer.createTeam(teamName, player);
                    }
                }
            }
            return false;
        }

    This is the first error I get:

    Code (Text):
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-db
    6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-db6d
    e12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-db
    6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.8.8.jar:git-Spigot-db
    6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.chat(PlayerConnection.java:1084) [spigot-1.8.8.jar:git-Spigot-d
    b6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1022) [spigot-1.8.8.jar:git-Spigot-db6d
    e12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot-1.8.8.jar:git-Spigot-db
    6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_72]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_72]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_72]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_72]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_72]
    Caused by: java.lang.NullPointerException
            at me.itzmarcus.teams.Team.getPlayerTeam(Team.java:59) ~[?:?]
            at me.itzmarcus.teams.Team.getPlayerPrefixTeam(Team.java:67) ~[?:?]
            at me.itzmarcus.teams.chatListener.chat(chatListener.java:20) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_72]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_72]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_72]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_72]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-db
    6de12-18fbb24]
            ... 11 more
     
  2. Can you show which line is line 59 in your Teams class?
     
  3. There is nothing on line 59.
    Code (Text):
        public int getTeamKills(String team) {
            return plugin.getConfig().getInt("teams." + team + ".kills");
        }
            // Line 59
        public String getPlayerTeam(String player) {
            String teamName = "No team";
            for(String s : teams) {
                if(getTeamMembers(s).contains(player) || getTeamLeader(s).contains(s)) {
                    teamName = s;
                }
            }
            return teamName;
        }
     
  4. Did you change your code since you made this thread? If so, run your changed plugin and recreate the error, but don't change your code once you've run it. Then, post your code, the stacktrace, and the line number the error is coming from.
     
  5. I will run the code again, but I did not change anything.
     
  6. Here is the error:

    Code (Text):
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-db
    6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-db6d
    e12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-db
    6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.8.8.jar:git-Spigot-db
    6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.chat(PlayerConnection.java:1084) [spigot-1.8.8.jar:git-Spigot-d
    b6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1022) [spigot-1.8.8.jar:git-Spigot-db6d
    e12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot-1.8.8.jar:git-Spigot-db
    6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_72]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_72]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_72]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_72]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_72]
    Caused by: java.lang.NullPointerException
            at me.itzmarcus.teams.Team.getPlayerTeam(Team.java:59) ~[?:?]
            at me.itzmarcus.teams.Team.getPlayerPrefixTeam(Team.java:67) ~[?:?]
            at me.itzmarcus.teams.chatListener.chat(chatListener.java:20) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_72]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_72]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_72]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_72]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-db
    6de12-18fbb24]
            ... 11 more

    Here is the line 67
    Code (Text):
    public String getPlayerTeam(String player) {
            String teamName = "No team";
            for(String s : teams) {
                if(getTeamMembers(s).contains(player) || getTeamLeader(s).contains(s)) {
                    teamName = s;
                }
            }
            return teamName; // Line 67
        }
     
  7. Code (Java):
    Caused by: java.lang.NullPointerException
            at me.itzmarcus.teams.Team.getPlayerTeam(Team.java:59) ~[?:?]
            at me.itzmarcus.teams.Team.getPlayerPrefixTeam(Team.java:67) ~[?:?]
            at me.itzmarcus.teams.chatListener.chat(chatListener.java:20) ~[?:?]
    NullPointerException

    give me the line 59.
    chat listener class line 20.
     
  8. Here you go:

    Line 59:
    Code (Text):
        public int getTeamKills(String team) {
            return plugin.getConfig().getInt("teams." + team + ".kills");
        }
            // Line 59
        public String getPlayerTeam(String player) {
            String teamName = "No team";
            for(String s : teams) {
                if(getTeamMembers(s).contains(player) || getTeamLeader(s).contains(s)) {
                    teamName = s;
                }
            }
            return teamName; // Line 67
        }
    Chat listener line 20:
    Code (Text):
        Team teamManagaer;
        public chatListener(Team instance) {
            teamManagaer = instance;
        }

        @EventHandler
        public void chat(AsyncPlayerChatEvent e) {
            String oldFormat = e.getFormat();
            e.setFormat(teamManagaer.getPlayerPrefixTeam(e.getPlayer().getName()) + oldFormat); // Line 20
        }
     
  9. must be
    Code (Java):
    Team teamManager = new Team();
     
  10. Should I change the instance thing to Team teamManager = new Team(); in the chatlistener class?
     
  11. yep or make an getting instance method inside the Team class so you can do Team.getInstance();

    or make methods static for Team.blablabla();
     
  12. So like this?

    Code (Text):
    Team teamManager = new Team(JavaPlugin.getPlugin(Core.class));

        @EventHandler
        public void chat(AsyncPlayerChatEvent e) {
            String oldFormat = e.getFormat();
            e.setFormat(teamManager.getPlayerPrefixTeam(e.getPlayer().getName()) + oldFormat); // Line 20
        }
     
  13. sort of, output of the console ?
     
  14. Okay no errors, but it returns "No team" even though I created a team just before I typed something.
     
  15. create your Team constructor inside its class,

    Code (Java):
    public Team() {}
    Code (Java):
    // more preferred way
    Team manager = new Team();
     
  16. So inside the Team class I should do: public Team() {} ?

    Edit: I did this:

    Code (Text):
        public Team() {
           
        }
    Inside the Team class.