Some Coding Issues - Help Please!

Discussion in 'Plugin Development' started by Booshayy, Nov 14, 2013.

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

    Booshayy

    Hi there.
    I was there, just mindin' my own developer buisness making a plugin.
    Everything was going well until I tried to use the commands on my private server.
    The plugins starts up fine, and the help info all works, but the command will not execute, and it's quite annoying. It is a single class. Here is my code (and there are no code problems):
    Code:
    package me.pierce.test;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Test extends JavaPlugin {
     
       
        public void onEnable() {
            Bukkit.getServer().getLogger().info("The test plugin has been enabled.");
        }
       
        public void onDisable() {
            Bukkit.getServer().getLogger().info("The test plugin has been disabled.");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.GOLD + "The console ran the test command, congratulations.");
                return true;
               
            }
           
            Player player = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase(("test"))) {
                player.sendMessage(ChatColor.AQUA + "You ran the test command, congratulations!");
            if (player.getName().equalsIgnoreCase(("setspawn"))) {
                World world = player.getWorld();
                Location loc = player.getLocation();
                world.setSpawnLocation(loc.getBlockX(), loc.getBlockY() + 1, loc.getBlockZ());
                player.sendMessage(ChatColor.GREEN + "You successfully set the spawn  at your location.");
            if (player.getName().equalsIgnoreCase(("spawn"))) {
                player.teleport(player.getWorld().getSpawnLocation());
                player.sendMessage(ChatColor.DARK_GREEN + "You have been healed and teleported to the world's spawn.");
                player.setHealth(20F);
                player.setFoodLevel(20);
                player.setFireTicks(0);
            if (cmd.getName().equalsIgnoreCase(("kill"))) {
                if (args.length == 0) {
                    player.setHealth(0.0F);
                    player.sendMessage(ChatColor.RED + "You just got killed!");
                } else if
                    (player.getServer().getPlayer(args[0])!=null) {
                    Player targetplayer = player.getServer().getPlayer(args[0]);
                    targetplayer.setHealth(0.0F);
                            targetplayer.sendMessage(ChatColor.RED + "You have been killed! :O ");
                            player.sendMessage(ChatColor.RED + "You have succesfully killed a player.");
                }else{
                    player.sendMessage(ChatColor.DARK_RED + "Player not found!");
                }
        }
        }
        }
        return true;
    }
        return true;
       
    }
        }
     
    
     
  2. Offline

    The Fancy Whale

    Couple of things....
    1. Wrong forum (not a big deal)
    2. Do you get an error?
    3. You need to use more else statements
    4. You need to close your if statements.... how are you not getting tons of errors?
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3.  
    4. if (!(sender instanceof Player)) {
    5. sender.sendMessage(ChatColor.GOLD + "The console ran the test command, congratulations.");
    6. return true;
    7.  
    8. }
    9.  
    10. Player player = (Player) sender;
    11.  
    12. else if (cmd.getName().equalsIgnoreCase(("test"))) {
    13. player.sendMessage(ChatColor.AQUA + "You ran the test command, congratulations!");}
    14. else if (player.getName().equalsIgnoreCase(("setspawn"))) {
    15. World world = player.getWorld();
    16. Location loc = player.getLocation();
    17. world.setSpawnLocation(loc.getBlockX(), loc.getBlockY() + 1, loc.getBlockZ());
    18. player.sendMessage(ChatColor.GREEN + "You successfully set the spawn at your location.");}
    19. else if (player.getName().equalsIgnoreCase(("spawn"))) {
    20. player.teleport(player.getWorld().getSpawnLocation());
    21. player.sendMessage(ChatColor.DARK_GREEN + "You have been healed and teleported to the world's spawn.");
    22. player.setHealth(20F);
    23. player.setFoodLevel(20);
    24. player.setFireTicks(0);}
    25. if (cmd.getName().equalsIgnoreCase(("kill"))) {
    26. if (args.length == 0) {
    27. player.setHealth(0.0F);
    28. player.sendMessage(ChatColor.RED + "You just got killed!");
    29. } else if
    30. (player.getServer().getPlayer(args[0])!=null) {
    31. Player targetplayer = player.getServer().getPlayer(args[0]);
    32. targetplayer.setHealth(0.0F);
    33. targetplayer.sendMessage(ChatColor.RED + "You have been killed! :O ");
    34. player.sendMessage(ChatColor.RED + "You have succesfully killed a player.");
    35. }else{
    36. player.sendMessage(ChatColor.DARK_RED + "Player not found!");
    37. }
    38. }
    39. return true;
    40. }
    41. return true;
    42.  
    43. }
    44. }
    45.  


    Might not be exactly correct I didn't count all of the brackets but I think you get the idea
     
  3. Offline

    Booshayy

    The Fancy Whale
    I got no error messages, none in the console or on Eclipse...
    The command just wouldn't run when I entered it in-game.

    Okay so it turns out, the only commands that work are /kill and /test.
    /setspawn and /spawn are to no avail. I even tried changing my code around a bit, as shown below.
    http://prntscr.com/248tdh
    Code:
     Player player = (Player) sender;
     
            int LocX = (int) player.getLocation().getX() - 1;
     
            int LocY = (int) player.getLocation().getY() - 1;
     
            int LocZ = (int) player.getLocation().getZ();
     
     
     
            if (cmd.getName().equalsIgnoreCase(("test"))) {
     
                player.sendMessage(ChatColor.AQUA + "You ran the test command, congratulations!");}
     
            else if (player.getName().equalsIgnoreCase(("setspawn"))) {
     
                player.getServer().getWorld("world")
     
                .setSpawnLocation(LocX, LocY, LocZ);
     
                player.sendMessage(ChatColor.GREEN + "You successfully set the spawn  at your location.");}
     
            else if (player.getName().equalsIgnoreCase(("spawn"))) {
     
                player.teleport(player.getWorld().getSpawnLocation());
     
                player.sendMessage(ChatColor.DARK_GREEN + "You have been healed and teleported to the world's spawn.");
     
                player.setHealth(20F);
     
                player.setFoodLevel(20);
     
                player.setFireTicks(0); 
     
  4. Offline

    Syd

    1. You don't need enable/disable messages, they will be generated by Bukkit automaticly
    2. Your command Strings are in double braces (("test")), while only 1 pair of braces is needed ("test"), this should however not affect the functionality
    3. You should really format your code. Modern IDE like Eclipse have a feature that format your code automaticly. (In Eclipse Strg+Shift+F)
    4. Did you add the commands to your plugin.yml?
     
  5. Offline

    Necrodoom

    Moved to correct section.
     
  6. Offline

    1Rogue

    Format your code and you might see where you are having issue (eclipse is ctrl+a + ctrl+i). You have have nested if statements, and "player.getName()..equals("setarena")" is definitely not the right way to check for a command name.
     
Thread Status:
Not open for further replies.

Share This Page