Dead code?

Discussion in 'Plugin Development' started by BizeaxPvP, Aug 6, 2019.

  1. Offline

    BizeaxPvP

    I'm not sure why this code is a dead code, please help!
    Code:
    package me.Zxoir.OnlyzBan.Commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    
    import me.Zxoir.OnlyzBan.Utils;
    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.TextComponent;
    
    public class commands implements CommandExecutor, Listener{
        public String punish = "punish";
        TextComponent mute = new TextComponent( Utils.chat("&cMute" ));
        TextComponent ban = new TextComponent( Utils.chat("&cBan" ));
     
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase(punish)) {
            if(sender instanceof Player) {
                Player player = (Player) sender;
            if(args.length == 0) {
                player.sendMessage(Utils.chat("&cInvalid usage: &a/punish <player>"));
                return true;
            }
            Player target = Bukkit.getPlayer(args[0]);
            if(target == null) {
                player.sendMessage(Utils.chat("&cThat player is not online!"));
                return true;
            }else if(!(target == null)){
                player.sendMessage(Utils.chat("&aPlease choose a punishment:"));
                mute.setClickEvent( new ClickEvent( ClickEvent.Action.RUN_COMMAND, "/punish mute" ) );
                mute.setHoverEvent(new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Utils.chat("&cClick here to mute " + target.getName())).create()) );
                player.spigot().sendMessage(mute);
                ban.setClickEvent( new ClickEvent( ClickEvent.Action.RUN_COMMAND, "/punishban" ) );
                ban.setHoverEvent(new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Utils.chat("&cClick here to ban " + target.getName())).create()) );
                player.spigot().sendMessage(ban);
             
                return true;
    //DEAD CODE HERE VVVVVV
            }else if(args[1].equalsIgnoreCase("ban")){
             
            }
    //DEAD CODE HERE ^^^^^^^^^^^
         
            }else {
                sender.sendMessage("Only players can use this command!");
            }
    
            }
            return false;
        }
    
    }
    
     
  2. Offline

    CraftCreeper6

    @BizeaxPvP
    Target is either null or not null, you've checked for both and have forgotten to put a closing bracket after return true;
     
  3. Offline

    BizeaxPvP

    No? I didn't forget any brackets i also dont have any errors. And I want to have a message if the target is not online so how can i do that
     
  4. Offline

    CraftCreeper6

    @BizeaxPvP
    You really ought to format your code.
    Code:
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    
    import me.Zxoir.OnlyzBan.Utils;
    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import net.md_5.bungee.api.chat.HoverEvent;
    import net.md_5.bungee.api.chat.TextComponent;
    
    public class commands implements CommandExecutor, Listener {
        public String punish = "punish";
        TextComponent mute = new TextComponent(Utils.chat("&cMute"));
        TextComponent ban = new TextComponent(Utils.chat("&cBan"));
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase(punish)) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    if (args.length == 0) {
                        player.sendMessage(Utils.chat("&cInvalid usage: &a/punish <player>"));
                        return true;
                    }
                    Player target = Bukkit.getPlayer(args[0]);
                    if (target == null) {
                        player.sendMessage(Utils.chat("&cThat player is not online!"));
                        return true;
                    } else if (!(target == null)) {
                        player.sendMessage(Utils.chat("&aPlease choose a punishment:"));
                        mute.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/punish mute"));
                        mute.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Utils.chat("&cClick here to mute " + target.getName())).create()));
                        player.spigot().sendMessage(mute);
                        ban.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/punishban"));
                        ban.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Utils.chat("&cClick here to ban " + target.getName())).create()));
                        player.spigot().sendMessage(ban);
    
                        return true;
                        //DEAD CODE HERE VVVVVV
                    } else if (args[1].equalsIgnoreCase("ban")) {
    
                    }
                    //DEAD CODE HERE ^^^^^^^^^^^
    
                } else {
                    sender.sendMessage("Only players can use this command!");
                }
    
            }
            return false;
        }
    
    }
    You haven't forgotten a bracket, you've put the else if statement in the wrong place.
     
  5. Offline

    BizeaxPvP

    still says the code is dead

    Edit: So I changed my code and i fixed the error but it wont send me the message when i do the command..
    Code:
    public class commands implements CommandExecutor, Listener {
        public String punish = "punish";
        TextComponent mute = new TextComponent(Utils.chat("&cMute"));
        TextComponent ban = new TextComponent(Utils.chat("&cBan"));
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase(punish)) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    if (args.length == 0) {
                        player.sendMessage(Utils.chat("&cInvalid usage: &a/punish <player>"));
                        return true;
                    }
                    Player target = Bukkit.getPlayer(args[0]);
                    Player target2 = Bukkit.getPlayer(args[0]);
                    if (target2 == null) {
                        player.sendMessage(Utils.chat("&cThat player is not online!"));
                        return true;
                    } else if (!(target == null)) {
                        player.sendMessage(Utils.chat("&aPlease choose a punishment:"));
                        mute.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/punish mute"));
                        mute.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Utils.chat("&cClick here to mute " + target.getName())).create()));
                        player.spigot().sendMessage(mute);
                        ban.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/punishban"));
                        ban.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Utils.chat("&cClick here to ban " + target.getName())).create()));
                        player.spigot().sendMessage(ban);
    
                        return true;
                    } else if (args[1].equalsIgnoreCase("ban")) {
                        player.sendMessage("test message");
                    }
    
                } else {
                    sender.sendMessage("Only players can use this command!");
                }
    
            }
            return false;
        }
    
    }
     
  6. Offline

    CraftCreeper6

    @BizeaxPvP
    I haven't corrected it for you, I just formatted it. You need to move else if (args[1].equalsIgnoreCase("ban"))
    somewhere else.
     
  7. Offline

    BizeaxPvP

    reload the page, i fixed the error but the command won't work.
     
  8. Offline

    CraftCreeper6

  9. Offline

    BizeaxPvP

    this is the new code
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase(punish)) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    if (args.length == 0) {
                        player.sendMessage(Utils.chat("&cInvalid usage: &a/punish <player>"));
                        return true;
                    }
                    if (args[1].equalsIgnoreCase("ban")) {
                        player.sendMessage("test message");
                        return true;
                    }
                    Player target = Bukkit.getPlayer(args[0]);
                    Player target2 = Bukkit.getPlayer(args[0]);
                    if (target2 == null) {
                        player.sendMessage(Utils.chat("&cThat player is not online!"));
                        return true;
                    } else if (!(target == null)) {
                        player.sendMessage(Utils.chat("&aPlease choose a punishment:"));
                        mute.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/punish mute"));
                        mute.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Utils.chat("&cClick here to mute " + target.getName())).create()));
                        player.spigot().sendMessage(mute);
                        ban.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/punishban"));
                        ban.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Utils.chat("&cClick here to ban " + target.getName())).create()));
                        player.spigot().sendMessage(ban);
    
                        return true;
                    }
    
                } else {
                    sender.sendMessage("Only players can use this command!");
                }
    
            }
            return false;
        }
    
    }
    but i get errors when i do /punish Zxoir (an online player) and /punish test (an offline player)
     
  10. Offline

    CraftCreeper6

  11. Offline

    BizeaxPvP

    An internal error occurred while attempting to perform this command.
    this is the log:
    Code:
    [13:52:21 INFO]: Zxoir issued server command: /punish Zxoir
    [13:52:21 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'punish' in plugin OnlyzBan v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_201]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_201]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at me.Zxoir.OnlyzBan.Commands.commands.onCommand(commands.java:31) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
     
  12. Offline

    CraftCreeper6

    @BizeaxPvP
    Why are you getting two target players?
     
  13. Offline

    BizeaxPvP

    For some reason if i only had 1 then any code under that would be dead and having 2 targets seem to have fixed it
    Edit: I tried having only 1 and it still is not working
     
  14. Offline

    CraftCreeper6

    @BizeaxPvP
    On the line else if (args[1].equalsIgnoreCase("ban"))
    args[1] refers not to the first argument but the second

    /punish arg0 arg1
     
  15. Offline

    BizeaxPvP

    Yea i know, im trying to make /punish (username) (ban/mute)
     
  16. Offline

    CraftCreeper6

    @BizeaxPvP
    The command that you're writing is
    /punish name

    That doesn't contain args[1] so it will throw an error, you need to first check if the command has an args[1]
     
  17. Offline

    BizeaxPvP

    but i put
    Code:
                        if (args[1].equalsIgnoreCase("ban")) {
                            player.sendMessage("test message");
                            return true;
                        }
     
  18. Offline

    CraftCreeper6

    @BizeaxPvP
    But args[1] doesn't exist, so you're trying to get the value of something that doesn't exist.

    You need to check that args[1] exists first by checking args.length >= 1
     
  19. Offline

    BizeaxPvP

    So what u said worked but there is 2 problems.
    1. When I do /punish Zxoir the text appears but under that it says internal error
    Log:
    Code:
    [14:22:15 INFO]: Zxoir issued server command: /punish Zxoir
    [14:22:15 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'punish' in plugin OnlyzBan v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_201]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_201]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at me.Zxoir.OnlyzBan.Commands.commands.onCommand(commands.java:44) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
    2nd problem is when i do /punish Zxoir ban it works but it also shows the same text from /punish Zxoir
    is there a way to remove that?
     
  20. Offline

    CraftCreeper6

  21. Offline

    BizeaxPvP

    there u go
    Code:
    public class commands implements CommandExecutor, Listener {
        public String punish = "punish";
        TextComponent mute = new TextComponent(Utils.chat("&cMute"));
        TextComponent ban = new TextComponent(Utils.chat("&cBan"));
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase(punish)) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    if (args.length == 0) {
                        player.sendMessage(Utils.chat("&cInvalid usage: &a/punish <player>"));
                        return true;
                    }
                    Player target = Bukkit.getPlayer(args[0]);
                    Player target2 = Bukkit.getPlayer(args[0]);
                    if (target2 == null) {
                        player.sendMessage(Utils.chat("&cThat player is not online!"));
                        return true;
                    } else if (!(target == null)) {
                        player.sendMessage(Utils.chat("&aPlease choose a punishment:"));
                        mute.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/punish mute"));
                        mute.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Utils.chat("&cClick here to mute " + target.getName())).create()));
                        player.spigot().sendMessage(mute);
                        ban.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/punishban"));
                        ban.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(Utils.chat("&cClick here to ban " + target.getName())).create()));
                        player.spigot().sendMessage(ban);
                        if (args[1].equalsIgnoreCase("ban")) {
                            if (args[1].length() == 0) {
                                player.sendMessage(Utils.chat("&cInvalid usage: &a/punish <player> <ban/mute>"));
                                return true;
                            }
                            player.sendMessage("test message");
                            return true;
                        }else {
                            return true;
                        }
                    }
    
                } else {
                    sender.sendMessage("Only players can use this command!");
                }
    
            }
            return false;
        }
    
    }
     
  22. Offline

    CraftCreeper6

    @BizeaxPvP
    Your command has to be /punish name ban otherwise you'll get an error, you need to check if args.length >= 1.
     
  23. Offline

    BizeaxPvP

    I still get the error
    Edit: I also tried this and it didnt work:
    Code:
                            if (args[1].length() == 0 || (!(args[1].equalsIgnoreCase("ban")))) {
                                player.sendMessage(Utils.chat("&cInvalid usage: &a/punish <player> <ban/mute>"));
                                return true;
                            }else if(args[1].length() >= 1) {
                            player.sendMessage("test message");
                            return true;
                            }
                        }
     
  24. Offline

    CraftCreeper6

    @BizeaxPvP
    That's really confusing.

    Here's the structure that you want:
    Code:
    if (command is equal to "punish")
        if (args.length >= 1)
            if (args[0] != null)
                target = getPlayer(args[0])
                if (target == null)
                    return false
                else
                    if (args[1] is equal to "ban")
                        // ban the player
                        return true
                    else if (args[1] is equal to "mute")
                        // mute the player
                        return true
            else
                // args[0] is null
                return false
        else
            // not enough arguments
            return false
    else
        // the command that was typed was not "punish"
        return false
    That's obviously not working code but it's the right track.
     
  25. Offline

    BizeaxPvP

    hmm this format is actually more confusing to me xD. but i did it and it still shows text from /punish <user> when i do /punish <user> ban. i would like to have separate text
     
  26. Offline

    CraftCreeper6

  27. Offline

    BizeaxPvP

    Ugh I reverted it back to my old code cause i found my old code better in my opinion but ill post a short version of the code so you could understand, if you would like the full code tell me.
    Code:
                        if (args[1].equalsIgnoreCase("ban")) {
                            if (args[1].length() == 0 || (!(args[1].equalsIgnoreCase("ban")))) {
                                player.sendMessage(Utils.chat("&cInvalid usage: &a/punish <player> <ban/mute>"));
                                return true;
                            }else if(args[1].length() >= 1) {
                            player.sendMessage("test message");
                            return true;
                            }
                        }
     
  28. Offline

    CraftCreeper6

    @BizeaxPvP
    That code is very inefficient and doesn't work as you like it to.

    If you are checking each and every single command to make sure it matches a certain type that's just inefficient.

    I'll try make the code more pseudo like such that it's easier to understand; I've written it in python.
    Code:
    if "punish" in command:
        if args.length >= 1:
            if (args[0] != null):
                target = Bukkit.getPlayer(args[0])
                if (target is null):
                    player.message("The target player is null")
                    return False
                else:
                    if ("ban" in args[1]):
                        # ban the player
                        return True
                    else if ("mute" in args[1]):
                        # mute the player
                        return True
            else:
                player.message("Args[0] is null")
                return False
        else:
            player.message("There are not enough arguments")
            return False
    else:
        player.message("The command that was run was not punish")
        return False
     
  29. Offline

    BizeaxPvP

    ive tried everything in my knowledge to do this and i dont have any clue, can you help? Basically I want it where if you do /punish <player> it says in chat choose a punishment which i will add. then when u click on the ban option it will execute /punish <player> ban, and then a text which i would like to put. for some reason everytime i try to make it, either it executes the text of /punish <player> with /punish <player> <ban> combines or it just doesn't work. Don't spoonfeed me just tell me what i need to do
     
  30. Offline

    CraftCreeper6

    @BizeaxPvP
    That changes a few things.

    So, in the punish command, check the length of the args. If args.length == 1 then the player has typed /punish [player name]
    If the args.length >= 1 then the player has clicked a chat button.

    So, I updated the pseudo code here:
    Code:
    if "punish" in command:
        if args.length == 1:
            player.message("the player has typed /punish [player]")
            sendChatMessage("Would you like to ban or mute the player") ## Make the ban and mute clickable
            ## If the buttons are clicked then run the command /punish [player] mute/ban
            return True
        elif args.length >= 1:
            if (args[0] != null):
                target = Bukkit.getPlayer(args[0])
                if (target is null):
                    player.message("The target player is null")
                    return False
                else:
                    if ("ban" in args[1]):
                        # ban the player
                        return True
                    else if ("mute" in args[1]):
                        # mute the player
                        return True
            else:
                player.message("Args[0] is null")
                return False
        else:
            player.message("There are not enough arguments")
            return False
    else:
        player.message("The command that was run was not punish")
        return False
    That is more like what you are wanting to do.
     

Share This Page