Solved HousePoints error

Discussion in 'Plugin Development' started by Fhbgsdhkfbl, Jun 11, 2016.

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

    Fhbgsdhkfbl

    Hey bukkit forums, I found an error in my house points plugin, and I can't figure out why it's doing it,
    if you help me out, I 'd appreciate it


    Error:
    Code:
    [16:25:58 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'points' in plugin HousePoints v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_71]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_71]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_71]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.Fhbgsdhkfbl.points.HousePoints.onCommand(HousePoints.java:305) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            ... 14 more
    >

    I've tried checking if house == null, that didn't fix it.

    onCommand boolean, I marked which line is null

    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(!sender.hasPermission(permission))
                return true;
            if(args.length != 2)
                error(sender);
         
            String house = args[0].toUpperCase().substring(0, 1) + args[0].toLowerCase().substring(1); //Line 305
            int value = 0;
            try{
                value = Integer.parseInt(args[1]);
            }catch(Exception e){
                error(sender);
            }
            if(value == 0){
                sender.sendMessage(greaterThanZero);
                return true;
            }
            if(!houses.contains(house)){
                sender.sendMessage(enterValidHouse);
                return true;
            }
            modifyHousePoints(house, value);
            informServerOfPointsChange(value, house);
            return true;
        }
     
    Last edited: Jun 11, 2016
  2. Online

    timtower Administrator Administrator Moderator

    @Fhbgsdhkfbl You check args[0] without checking if it exists.
     
  3. Offline

    Fhbgsdhkfbl

    @timtower that was fast, thanks lmao

    so putting if(arg[0] == null) {

    would fix it?
     
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    Fhbgsdhkfbl

    I'm still getting the same error on the same line
    I did (args.length > 0) {
    return true;
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Offline

    Fhbgsdhkfbl

    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(!sender.hasPermission(permission))
                return true;
            if(args.length != 2)
                error(sender);
            if(args.length > 0) {
                return true;
            }
            String house = args[0].toUpperCase().substring(0, 1) + args[0].toLowerCase().substring(1); //Line 307
            int value = 0;
            try{
                value = Integer.parseInt(args[1]);
            }catch(Exception e){
                error(sender);
            }
            if(value == 0){
                sender.sendMessage(greaterThanZero);
                return true;
            }
            if(!houses.contains(house)){
                sender.sendMessage(enterValidHouse);
                return true;
            }
            modifyHousePoints(house, value);
            informServerOfPointsChange(value, house);
            return true;
        }

    Code:
    [16:37:57 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'points' in plugin HousePoints v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_71]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_71]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_71]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.Fhbgsdhkfbl.Points.HousePoints.onCommand(HousePoints.java:307) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            ... 14 more
    Same error aswell
     
    Last edited: Jun 11, 2016
  8. Online

    timtower Administrator Administrator Moderator

  9. Offline

    Fhbgsdhkfbl

    the command still works, but when you run the command in game, it says in internal error as occured, but you still can run the command and take points, etc
     
  10. Online

    timtower Administrator Administrator Moderator

  11. Offline

    Fhbgsdhkfbl

    I can show you the whole class if you would like
     
  12. Online

    timtower Administrator Administrator Moderator

  13. Offline

    Fhbgsdhkfbl

    @timtower
    Code:
    package me.Fhbgsdhkfbll.Points;
    
    
    import java.util.ArrayList;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class HousePoints extends JavaPlugin implements Listener, CommandExecutor{
    
        private static HousePoints instance;
        private static ArrayList<String> houses;
        private static String permission = "housepoints.staff";
        private static String signCommand = "{{House Points}}";
        private static String pluginUsing = ChatColor.RED + "A plugin may not have set HousePoints as a dependency, but is attempting to use it";
        private static String enterValidHouse = ChatColor.RED + "You didn't enter a valid House!";
        private static String addedTo = ChatColor.GREEN + "" + ChatColor.ITALIC + "%s points have been added to %s.";
        private static String removedFrom = ChatColor.RED + "" + ChatColor.ITALIC + "%s points have been removed from %s.";
        private static String greaterThanZero = ChatColor.RED + "Please enter a value other than 0!";
        private static String signCreated = ChatColor.GREEN + "Sign created for %s!";
        private static String signDestroyed = ChatColor.RED + "Sign for %s destroyed!";
        private static String noPermission = ChatColor.RED + "You don't have permission to break House point signs!";
       
        public static String[] getHouses(){
            if(houses == null){
                Bukkit.getConsoleSender().sendMessage(pluginUsing);
                return null;
            }
            String[] s = new String[houses.size()];
            for(int i=0;i<houses.size();i++)
                s[i] = houses.get(i);
            return s;
        }
       
        public void onEnable(){
            instance = this;
            houses = new ArrayList<String>();
            houses.add("Slytherin");
            houses.add("Gryffindor");
            houses.add("Ravenclaw");
            houses.add("Hufflepuff");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            FileManager.init(this);
            // Initialize the file if it doesn't exist
            if(!FileManager.fileExists("signs.yml")) {
                FileManager.getFile("signs.yml", true);
            }
            if(!FileManager.fileExists("points.yml")) {
                YamlFile file = new YamlFile(FileManager.getFile("points.yml", true));
                for(String s : houses)
                    file.set(s, 0);
            }
            if(!FileManager.fileExists("strings.yml")) {
                YamlFile file = new YamlFile(FileManager.getFile("strings.yml", true));
                file.set("enter_valid_house", enterValidHouse);
                file.set("points_added_to_house", addedTo);
                file.set("points_removed_from_house", removedFrom);
                file.set("greater_than_zero", greaterThanZero);
                file.set("sign_created", signCreated);
            }else{
                YamlFile file = new YamlFile(FileManager.getFile("strings.yml", false));
                enterValidHouse = file.getString("enter_valid_house");
                addedTo = file.getString("points_added_to_house");
                removedFrom = file.getString("points_removed_from_house");
                greaterThanZero = file.getString("greater_than_zero");
                signCreated = file.getString("sign_created");
            }
            if(!FileManager.fileExists("config.yml")) {
                YamlFile file = new YamlFile(FileManager.getFile("config.yml", true));
                file.set("staff_permission", permission);
                file.set("sign_command", signCommand);
            }else{
                YamlFile file = new YamlFile(FileManager.getFile("config.yml", false));
                permission = file.getString("staff_permission");
                signCommand = file.getString("sign_command");
            }
        }
       
        public void onDisable(){
            YamlFile file = new YamlFile(FileManager.getFile("signs.yml", false));
            file.save();
        }
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event){
            if(!event.getPlayer().hasPermission("housepoints.staff"))
                return;
            String a = "";
            String b = "";
            String c = "";
            String d = "";
            for(String s : ChatColor.stripColor(event.getMessage()).split(" ")){
                a = b;
                b = c;
                c = d;
                d = s.toLowerCase();
                if(d.length() > 8 && (c.equalsIgnoreCase("to") || c.equalsIgnoreCase("from")) && b.equalsIgnoreCase("points") && getHouse(d) != null){
                    try{
                        d = getHouse(d);
                        int value = Integer.parseInt(a);
                        if(c.equalsIgnoreCase("to")){
                            modifyHousePoints(d, value);
                            event.getPlayer().sendMessage(String.format(addedTo, a, d));
                        }else if(c.equalsIgnoreCase("from")){
                            modifyHousePoints(d, -value);
                            event.getPlayer().sendMessage(String.format(removedFrom, a, d));
                        }
                    }catch(Exception e){} //means that 'a' wasn't an integer.
                }
            }
           
        }
       
        private String getHouse(String s){
            for(String str : houses){
                if(s.toLowerCase().startsWith(str.toLowerCase())){
                    return str;
                }
            }
            return null;
        }
       
        /**
         * Gets the House points for the House.
         * @param house The Houses you want points for.
         * @return Returns the House points for the House. May return -100,000,000 if there was an error.
         */
        public static int getHousePoints(String house){
            if(houses == null){
                Bukkit.getConsoleSender().sendMessage(pluginUsing);
                return -100000000;
            }
            try{
                house = house.toUpperCase().substring(0, 1) + house.toLowerCase().substring(1);
                YamlFile file = new YamlFile(FileManager.getFile("points.yml", false));
                return file.getInt(house);
            }catch(Exception e){
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "ERROR while retrieving House points!");
                e.printStackTrace();
                return -100000000;
            }
        }
       
        /**
         * Will modify the House points in the points.yml and then update all signs in the signs.yml
         * @param house The House you would like to modify the points of.
         * @param amount The amount to modify them by.
         */
        public static boolean modifyHousePoints(String house, int amount){
            if(houses == null){
                Bukkit.getConsoleSender().sendMessage(pluginUsing);
                return false;
            }
            try{
                house = house.toUpperCase().substring(0, 1) + house.toLowerCase().substring(1);
                YamlFile file = new YamlFile(FileManager.getFile("points.yml", false));
                file.set(house, file.getInt(house) + amount);
                updateSigns();
                return true;
            }catch(Exception e){
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "ERROR while modifying House points!");
                e.printStackTrace();
                return false;
            }
        }
       
        /**
         * Updates all the signs in the sign.yml
         * @return Returns true if it successfully updated all the signs
         */
        public static boolean updateSigns(){
            if(houses == null){
                Bukkit.getConsoleSender().sendMessage(pluginUsing);
                return false;
            }
            try{
                YamlFile file = new YamlFile(FileManager.getFile("signs.yml", false));
                if(file.getKeys("", false) == null)
                    return false;
                boolean delete = true;
                YamlFile points = new YamlFile(FileManager.getFile("points.yml", false));
                for(String s : file.getKeys("", false)){
                    World world = Bukkit.getServer().getWorld(UUID.fromString(file.getString(s + ".uuid")));
                    Block b = world.getBlockAt(new Location(world, file.getDouble(s + ".x"), file.getDouble(s + ".y"),file.getDouble(s + ".z")));
                    if(b.getState() instanceof Sign){
                        Sign sign = (Sign) b.getState();
                        sign.setLine(0, "");
                        String house = file.getString(s + ".house");
                        sign.setLine(2, points.getInt(house) + "");
                        switch(house){
                            case "Slytherin":
                                house = ChatColor.GREEN + house;
                                break;
                            case "Gryffindor":
                                house = ChatColor.RED + house;
                                break;
                            case "Ravenclaw":
                                house = ChatColor.BLUE + house;
                                break;
                            case "Hufflepuff":
                                house = ChatColor.GOLD + house;
                                break;
                        }
                        sign.setLine(1, house);
                        sign.update(true);
                        delete = false;
                    }
                    if(delete)
                        file.set(s, null);
                    delete = true;
                }
                return true;
            }catch(Exception e){
                Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "ERROR while updating House point signs!");
                e.printStackTrace();
                return false;
            }
        }
       
        @EventHandler
        public void signPlace(SignChangeEvent event){
            if(!event.getPlayer().hasPermission(permission))
                return;
            Player player = event.getPlayer();
            String line1 = event.getLine(0);
            String line2 = event.getLine(1);
            if(line1 == null)
                return;
            if(line1.equalsIgnoreCase("{{House Points}}")){
                if(line2 == null)
                    return;
                line2 = line2.toUpperCase().substring(0, 1) + line2.toLowerCase().substring(1);
                if(!houses.contains(line2)){
                    player.sendMessage(enterValidHouse);
                    return;
                }
                YamlFile file = new YamlFile(FileManager.getFile("signs.yml", false));
                Location loc = event.getBlock().getLocation();
                String path = String.format("%s%s%s%s", loc.getWorld().getUID().toString(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
                file.set(path + ".world_name", loc.getWorld().getName());
                file.set(path + ".sign_creator", player.getName());
                file.set(path + ".uuid", loc.getWorld().getUID().toString());
                file.set(path + ".x", loc.getBlockX());
                file.set(path + ".y", loc.getBlockY());
                file.set(path + ".z", loc.getBlockZ());
                file.set(path + ".house", line2);
                try {
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(instance, new Runnable() {
                        public void run(){
                            try {
                                updateSigns();
                            } catch(Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, 2);
                } catch(Exception e) {
                    e.printStackTrace();
                }
                updateSigns();
                player.sendMessage(String.format(signCreated, line2));
            }
        }
       
        @EventHandler
        public void signBreak(BlockBreakEvent event){
            if(!(event.getBlock().getState() instanceof Sign))
                return;
            Sign sign = (Sign) event.getBlock().getState();
            Location loc = sign.getLocation();
            String path = String.format("%s%s%s%s", loc.getWorld().getUID().toString(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
            YamlFile file = new YamlFile(FileManager.getFile("signs.yml", false));
            if(!file.getKeys(false).contains(path))
                return;
            if(!event.getPlayer().hasPermission(permission)){
                event.setCancelled(true);
                event.getPlayer().sendMessage(noPermission);
                return;
            }
            file.set(path, null);
            event.getPlayer().sendMessage(signDestroyed);
        }
       
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(!sender.hasPermission(permission))
                return true;
            if(args.length != 2)
                error(sender);
            if(args.length > 0) {
                return true;
            }
            String house = args[0].toUpperCase().substring(0, 1) + args[0].toLowerCase().substring(1); //Line 307
            int value = 0;
            try{
                value = Integer.parseInt(args[1]);
            }catch(Exception e){
                error(sender);
            }
            if(value == 0){
                sender.sendMessage(greaterThanZero);
                return true;
            }
            if(!houses.contains(house)){
                sender.sendMessage(enterValidHouse);
                return true;
            }
            modifyHousePoints(house, value);
            informServerOfPointsChange(value, house);
            return true;
        }
       
        private void informServerOfPointsChange(int amount, String house){
            for(Player p : Bukkit.getServer().getOnlinePlayers()){
                if(amount < 0)
                    p.sendMessage(String.format(removedFrom , amount * -1, house));
                else
                    p.sendMessage(String.format(addedTo , amount, house));
            }
        }
       
        private boolean error(CommandSender sender){
            sender.sendMessage(ChatColor.RED + "Please try again!");
            sender.sendMessage(ChatColor.ITALIC + "Usage: /points <house> <amount>");
            return true;
        }
    
    }
    
     
  14. Online

    timtower Administrator Administrator Moderator

    @Fhbgsdhkfbl You are still returning true when the length > 0, don't do that.
    That is causing those errors, but in your case: you might want to check if args.length<2 {return true;}
     
  15. Offline

    Fhbgsdhkfbl

    @timtower
    Great! It fixed it, Appreciate the help, wish I could've fixed it without a post!
     
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page