Solved Minigame Leave Command returns Error?

Discussion in 'Plugin Development' started by AppleBabies, Dec 3, 2015.

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

    AppleBabies

    Hello. I am currently developing a minigame. There's only one slight(AKA very big) issue. Players cannot leave. Here is my error:
    Code:
    03.12 18:16:05 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at com.themadmc.magicbattle.CommandManager.onCommand(CommandManager.java:63) ~[?:?]
    03.12 18:16:05 [Server] INFO at com.themadmc.magicbattle.commands.Leave.onCommand(Leave.java:17) ~[?:?]
    03.12 18:16:05 [Server] INFO at com.themadmc.magicbattle.Arena.removePlayer(Arena.java:81) ~[?:?]
    03.12 18:16:05 [Server] INFO at com.themadmc.magicbattle.PlayerData.restorePlayer(Arena.java:205) ~[?:?]
    03.12 18:16:05 [Server] INFO Caused by: java.lang.NullPointerException
    03.12 18:16:05 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_51]
    03.12 18:16:05 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_51]
    03.12 18:16:05 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_51]
    03.12 18:16:05 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
    03.12 18:16:05 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'mb' in plugin MagicBattle v1.0
    03.12 18:16:05 [Server] INFO [18:16:05 ERROR]: null
    Leave.java:
    Code:
    package com.themadmc.magicbattle.commands;
    
    import org.bukkit.entity.Player;
    
    import com.themadmc.magicbattle.ArenaManager;
    import com.themadmc.magicbattle.MessageManager;
    import com.themadmc.magicbattle.MessageManager.MessageType;
    
    public class Leave extends MagicCommand {
    
        public void onCommand(Player p, String[] args) {
            if (ArenaManager.getInstance().getArena(p) == null) {
                MessageManager.getInstance().msg(p, MessageType.BAD, "You are not already in an arena!");
                return;
            }
    
            ArenaManager.getInstance().getArena(p).removePlayer(p);
        }
      
        public Leave() {
            super("Leave an arena.", "", "l");
        }
    }
    CommandManager.java:
    Code:
    package com.themadmc.magicbattle;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import com.themadmc.magicbattle.MessageManager.MessageType;
    import com.themadmc.magicbattle.commands.Create;
    import com.themadmc.magicbattle.commands.Delete;
    import com.themadmc.magicbattle.commands.ForceStart;
    import com.themadmc.magicbattle.commands.ForceStop;
    import com.themadmc.magicbattle.commands.Join;
    import com.themadmc.magicbattle.commands.Leave;
    import com.themadmc.magicbattle.commands.MagicCommand;
    import com.themadmc.magicbattle.commands.Reload;
    import com.themadmc.magicbattle.commands.SetLocation;
    
    public class CommandManager implements CommandExecutor{
    
    private ArrayList<MagicCommand> cmds = new ArrayList<MagicCommand>();
      
        public CommandManager() {
            cmds.add(new Create());
            cmds.add(new Delete());
            cmds.add(new ForceStart());
            cmds.add(new ForceStop());
            cmds.add(new Join());
            cmds.add(new Leave());
            cmds.add(new Reload());
            cmds.add(new SetLocation());
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (!(sender instanceof Player)) {
                MessageManager.getInstance().msg(sender, MessageType.BAD, "Only players can use MagicBattle!");
                return true;
            }
          
            Player p = (Player) sender;
          
            if (cmd.getName().equalsIgnoreCase("magicbattle")) {
                if (args.length == 0) {
                    for (MagicCommand mc : cmds) MessageManager.getInstance().msg(p, MessageType.INFO, "/mb " + aliases(mc) + " " + mc.getUsage() + " - " + mc.getMessage());
                    return true;
                }
              
                MagicCommand c = getCommand(args[0]);
              
                if (c == null) {
                    MessageManager.getInstance().msg(sender, MessageType.BAD, "That command doesn't exist!");
                    return true;
                }
              
                Vector<String> a = new Vector<String>(Arrays.asList(args));
                a.remove(0);
                args = a.toArray(new String[a.size()]);
              
                c.onCommand(p, args);
              
                return true;
            }
            return true;
        }
      
        private String aliases(MagicCommand cmd) {
            String fin = "";
          
            for (String a : cmd.getAliases()) {
                fin += a + " | ";
            }
          
            return fin.substring(0, fin.lastIndexOf(" | "));
        }
      
        private MagicCommand getCommand(String name) {
            for (MagicCommand cmd : cmds) {
                if (cmd.getClass().getSimpleName().equalsIgnoreCase(name)) return cmd;
                for (String alias : cmd.getAliases()) if (name.equalsIgnoreCase(alias)) return cmd;
            }
            return null;
        }
      
    }
    
    
    Arena.java:
    Code:
    package com.themadmc.magicbattle;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.block.Sign;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.themadmc.magicbattle.MessageManager.MessageType;
    import com.themadmc.magicbattle.listeners.SignManager;
    
    public class Arena {
      
        public enum ArenaState { DISABLED, WAITING, COUNTING_DOWN, STARTED; }
    
        private int id, numPlayers, currentPlayers;
        private ArrayList<PlayerData> data;
        private ArrayList<Sign> signs;
        private Location spawnPoint;
        protected ArenaState state = ArenaState.DISABLED;
      
        protected Arena(int id) {
            this.id = id;
            this.data = new ArrayList<PlayerData>();
            this.numPlayers = SettingsManager.getArenas().get("arenas." + id + ".numPlayers");
          
            if (SettingsManager.getArenas().contains("arenas." + id + ".spawn")) {
                ConfigurationSection s = SettingsManager.getArenas().get("arenas." + id + ".spawn");
                spawnPoint = LocationUtil.locationFromConfig(s, true);
            }
          
            state = ArenaState.WAITING;
          
            this.signs = SignManager.getSigns(this);
        }
      
        public int getID() {
            return id;
        }
      
        public ArenaState getState() {
            return state;
        }
      
        public int getCurrentPlayers() {
            return currentPlayers;
        }
      
        public void addPlayer(Player p) {
            if (currentPlayers >= numPlayers) {
                MessageManager.getInstance().msg(p, MessageType.BAD, "There are too many players.");
                return;
            }
          
            if (spawnPoint == null) {
                MessageManager.getInstance().msg(p, MessageType.BAD, "Spawn point not yet set.");
                return;
            }
          
            data.add(new PlayerData(p));
          
            p.getInventory().clear();
            p.getInventory().addItem(Wand.values()[new Random().nextInt(Wand.values().length)].createItemStack());
          
            p.teleport(spawnPoint);
          
            currentPlayers++;
          
            for (Sign s : signs) {
                s.setLine(2, currentPlayers + "");
            }
        }
      
        public void removePlayer(Player p) {
            PlayerData d = getPlayerData(p);
            d.restorePlayer();
            data.remove(d);
          
            currentPlayers--;
          
            for (Sign s : signs) {
                s.setLine(2, currentPlayers + "");
                s.update();
            }
          
            if (currentPlayers == 1) {
                stop(data.get(0).getPlayer());
            } else if (currentPlayers == 0) {
                stop();
            }
        }
      
        public void start() {
            this.state = ArenaState.COUNTING_DOWN;
            for (Sign s : signs) {
                s.setLine(3, getState().toString());
                s.update();
            }
          
            new Countdown(
                    30,
                    "Game starting in %t seconds!",
                    this,
                    30,
                    20,
                    10,
                    5,
                    4,
                    3,
                    2,
                    1
            ).runTaskTimer(MagicBattle.getPlugin(), 0, 20);
        }
      
        public void stop(Player winner) {
            if (winner != null) MessageManager.getInstance().broadcast(MessageType.GOOD, winner.getName() + " has won arena " + id + "!");
          
            for (PlayerData pd : data) pd.restorePlayer();
    
            this.state = ArenaState.WAITING;
        }
    
        public void stop() {
            MessageManager.getInstance().broadcast(MessageType.GOOD, "The arena " + id + " has ended!");
    
            for (PlayerData pd : data) pd.restorePlayer();
    
            this.state = ArenaState.WAITING;
        }
      
        public boolean containsPlayer(Player p) {
            return getPlayerData(p) != null;
        }
      
        private PlayerData getPlayerData(Player p) {
            for (PlayerData d : data) {
                if (d.isForPlayer(p)) return d;
            }
            return null;
        }
      
        protected void sendMessage(MessageType type, String... messages) {
            for (PlayerData d : data) MessageManager.getInstance().msg(d.getPlayer(), type, messages);
        }
      
        private class Countdown extends BukkitRunnable {
          
            private int timer;
            private String msg;
            private Arena a;
            private ArrayList<Integer> countingNums;
          
            public Countdown(int start, String msg, Arena a, int... countingNums) {
                this.timer = start;
                this.msg = msg;
                this.a = a;
                this.countingNums = new ArrayList<Integer>();
                for (int i : countingNums) this.countingNums.add(i);
            }
          
            public void run() {
                if (timer == 0) {
                    for (PlayerData pd : data) {
                        pd.getPlayer().teleport(spawnPoint);
                    }
                  
                    MessageManager.getInstance().broadcast(MessageType.GOOD, "The game has started!");
                    a.state = ArenaState.STARTED;
                    cancel();
                }
              
                if (countingNums.contains(timer)) {
                    MessageManager.getInstance().broadcast(MessageType.INFO, msg.replace("%t", timer + ""));
                }
              
                timer--;
            }
        }
    }
    
    class PlayerData {
    
        private Player puu;
        private String playerName;
        private ItemStack[] contents, armorContents;
        private Location location;
      
        protected PlayerData(Player p) {
            this.playerName = p.getName();
            this.contents = p.getInventory().getContents();
            this.armorContents = p.getInventory().getArmorContents();
            this.location = p.getLocation();
        }
      
        protected Player getPlayer() {
            return Bukkit.getServer().getPlayer(puu.getUniqueId());
        }
      
        protected void restorePlayer() {
            Player p = Bukkit.getServer().getPlayer(puu.getUniqueId());
          
            p.getInventory().setContents(contents);
            p.getInventory().setArmorContents(armorContents);
            p.teleport(location);
        }
      
        protected boolean isForPlayer(Player p) {
            return playerName.equalsIgnoreCase(p.getName());
        }
    }
     
  2. Offline

    Scimiguy

    puu is null; you're never setting it to anything
     
  3. Offline

    AppleBabies

    @Scimiguy Could you point me in the right direction? I thought I had it.
     
  4. Offline

    Scimiguy

    Do something with puu.

    Literally all you've done with it is:
    Player puu;

    What's that even meant to do?
     
  5. Offline

    AppleBabies

    @Scimiguy Look further down. It is a player reference so the player's UUID can be retrieved. (I'm going to later use this for configuration stuff) It also gets the player's inventory contents and location before they join the game. When they leave, it's SUPPOSED to restore their contents and teleport them to their previous location.
     
  6. Offline

    Scimiguy

    Sigh..

    1. 03.12 18:16:05 [Server] INFO at com.themadmc.magicbattle.PlayerData.restorePlayer(Arena.java:205) ~[?:?]
    2. 03.12 18:16:05 [Server] INFO Caused by: java.lang.NullPointerException
    Meaning:
    Something is null in your Arena class at line 205.

    Line 205:
    Player p = Bukkit.getServer().getPlayer(puu.getUniqueId());
    Given that it's practically impossible for Server to be null, the only remaining thing is puu.
    So puu is null.

    Where does puu start?
    Line 188:
    private Player puu;

    Do you initialize it in your constructors?
    Code:
        protected PlayerData(Player p) {
            this.playerName = p.getName();
            this.contents = p.getInventory().getContents();
            this.armorContents = p.getInventory().getArmorContents();
            this.location = p.getLocation();
        }
    So... no.

    puu is null.
     
  7. Offline

    AppleBabies

    @Scimiguy Right...but how do I initialize it? You can't just do this.puu = puu;
     
  8. Offline

    Scimiguy

    Set it to whatever you want it to be.

    I have no idea what puu is meant to be since you never refer to it at all in any other part of your code
     
  9. Offline

    AppleBabies

    @Scimiguy
    Code:
    protected void restorePlayer() {
            Player p = Bukkit.getServer().getPlayer(puu.getUniqueId());
        
            p.getInventory().setContents(contents);
            p.getInventory().setArmorContents(armorContents);
            p.teleport(location);
        }
    @Scimiguy Gosh...simple fix. Sorry I wasted your time! :p Marking as solved.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 4, 2015
Thread Status:
Not open for further replies.

Share This Page