Coding issues - Please help!

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

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

    Booshayy

    Hi! I'm trying to make my own version of Essentials. I know basics, but am not very well-versed in coding as of now.
    The commands I have are: /test, /kill, /setspawn and /spawn.
    The only command that works is /test.
    Here is the code, any assistance would be appreciated.
    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 (cmd.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 (cmd.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")) {
                } else if (args.length == 0) {
                    player.setHealth(0.0D);
                    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.0D);
                            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;
    }
    }
    
    Thanks again.
     
  2. Offline

    RealDope

    Do you even know Java? Your brackets for the if statements are seriously messed up. You need to close each if before the next one starts, not nest them.
     
    jacklin213 and The_Doctor_123 like this.
  3. Offline

    Booshayy

    RealDope I'm still learning. I'm very much a beginner. So you're saying I should close the if statement before putting the next one under it?
     
  4. Offline

    The_Doctor_123

    Booshayy
    Okay, these forums are for Bukkit API help, not Java. It is also very dangerous and a pain to be learning Java and the Bukkit API at the same time. I recommend you learn first.
     
  5. Offline

    Gater12

    Booshayy You should always know that to close your brackets or those nasty red lines you don't want to mess will start to appear! I really do not know why you put out a check if the sender is not an instance of Player when I don't know what's causing it to make it check. (Or I am missing something here) You should put a check when they execute the command.
     
    AoH_Ruthless likes this.
  6. Offline

    xTrollxDudex

    Booshayy
    He's implying that you should thouroughly learn java before even trying to make plugins
     
  7. Offline

    Booshayy

    xTrollxDudex I've made a simple hub plugin before, with /heal, /gm(c, s, and a), and /tp.
    I just haven't been practicing at all, and it's been at least a month since I've messed with it.
     
  8. Offline

    Gater12

    Booshayy Well silly you! I forget sometimes, too. You should go back and review, I mean when you forget something you can't just remember it so you need to go back and review everything and then probably something will click and automagically you will remeber again.
     
  9. Offline

    Booshayy

    Gater12 I was screwing with the bodies and what not, and I figured it out.
    The_Doctor_123 A problem though, whatever command I run, it does what I told it too, but also kills me, displays the kill message, and /kill no longer works? Here is my code now.
    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) {
       
     
            Player player = (Player) sender;
     
            if (cmd.getName().equalsIgnoreCase(("test"))) {
                player.sendMessage(ChatColor.AQUA + "You ran the test command, congratulations!");
            }
       
            if (cmd.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 (cmd.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")) {
            }
                else if (args.length == 0) {
                    player.setHealth(0.0D);
                    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.0D);
                            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;
    }
        }
    
     
  10. Offline

    The_Doctor_123

    Booshayy
    Because your bodies are still screwed up at the end.

    Also, what's with the double parenthesis?
    Code:java
    1. .equalsIgnoreCase(("setspawn")))
     
  11. Offline

    Gater12

    Booshayy Somebody has to cut the cheese. Like everyone else basically said, you don't know what you are doing anymore. Quick, go to the Java For Dummies book or thenewboston's channel for a refresh, and avoid some embarrassment.
     
    The_Doctor_123 likes this.
  12. Offline

    Booshayy

    The_Doctor_123 Can you elaborate on the issue? And, I'm not sure why I did that, I'll change it now.
    Gater12 Thank you :p
     
  13. Offline

    The_Doctor_123

  14. Offline

    Booshayy

    The_Doctor_123 I'm not understanding what part of my bodies are screwed up.
     
  15. Offline

    The_Doctor_123

    Booshayy
    I'm not exactly sure what to say if you can't see all those errors(Yes, with an "s").
     
  16. Offline

    Booshayy

    The_Doctor_123 The point of this thread is to ask for help, could you help? And I still don't know why I'm being killed when I run every other command.
     
  17. Offline

    jacklin213

    Code:
    return true;
    or
    Code:
    return false;
    once a check is done
     
  18. Offline

    The_Doctor_123

    The point of these forums is to help with the Bukkit API. I'm very sorry.
     
  19. Offline

    Booshayy

    This is the Bukkit API, is it not?
     
  20. Offline

    The_Doctor_123

    Booshayy
    Are bodies specific to Bukkit?
     
  21. Offline

    Compressions

    Booshayy When your problems aren't related to the Bukkit API and are examples of your Java inadequacies, but you lead yourself to believe you understand the language and that your API calls are the issue, we have no reason to teach you basic Java. We all started somewhere, but you simply can not learn Java thoroughly through Bukkit.
     
  22. Offline

    TeeePeee

    Because I strongly believe that code examples do help when learning Java. This is not to say that you don't need to practice, though.

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. if (!(sender instanceof Player)) {
    3. sender.sendMessage("You are not a player.");
    4. return true;
    5. }
    6.  
    7. Player player = (Player) sender;
    8.  
    9. if (cmd.getName().equalsIgnoreCase("test")) {
    10. player.sendMessage(ChatColor.AQUA + "You ran the test command, congratulations!");
    11. } else if (cmd.getName().equalsIgnoreCase("setspawn")) {
    12. World world = player.getWorld();
    13. Location loc = player.getLocation();
    14. world.setSpawnLocation(loc.getBlockX(), loc.getBlockY() + 1, loc.getBlockZ());
    15. player.sendMessage(ChatColor.GREEN + "You successfully set the spawn at your location.");
    16. } else if (cmd.getName().equalsIgnoreCase("spawn")) {
    17. player.teleport(player.getWorld().getSpawnLocation());
    18. player.sendMessage(ChatColor.DARK_GREEN + "You have been healed and teleported to the world's spawn.");
    19. player.setHealth(20F);
    20. player.setFoodLevel(20);
    21. player.setFireTicks(0);
    22. } else if (cmd.getName().equalsIgnoreCase("kill")) {
    23. if (args.length == 0) {
    24. player.setHealth(0.0D);
    25. player.sendMessage(ChatColor.RED + "You just got killed!");
    26. } else if (player.getServer().getPlayer(args[0]) != null) {
    27. Player targetplayer = player.getServer().getPlayer(args[0]);
    28. targetplayer.setHealth(0.0D);
    29. targetplayer.sendMessage(ChatColor.RED + "You have been killed! :O ");
    30. player.sendMessage(ChatColor.RED + "You have succesfully killed a player.");
    31. } else {
    32. player.sendMessage(ChatColor.DARK_RED + "Player not found!");
    33. }
    34. }
    35.  
    36. return true;
    37. }


    Compare the one you wrote vs. the one I revised for you.
     
Thread Status:
Not open for further replies.

Share This Page