Help with Custom Bukkit Plugin

Discussion in 'Plugin Development' started by CyberDork34, Feb 23, 2013.

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

    CyberDork34

    Hello all,

    I am making a bukkit plugin called golden commands. It is supposed to add more server commands to the server. While the commands work fine, most of them dump everything from the code into the code into the chat screen. For example, one of my commands, /gc location, is supposed to give you you're location, but instead gives you the help screen, and the usage as shown below:

    Bug.png
    This is my code for my main class:

    Code:
    package com.emc2vsa22.gmail;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class GoldCommandsPlugin extends JavaPlugin{
        public static GoldCommandsPlugin plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
       
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " ,the official CyberCraft Plugin, has been disabled.");
            getServer().getPluginManager().removePermission(new Permisssions().canPerformCommand);
        }
       
        @Override
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            PluginManager pm = this.getServer().getPluginManager();
            this.logger.info(pdfFile.getName() +  " ,the official CyberCraft Plugin, has been enabled." + " Version " + pdfFile.getVersion());
           
            pm.addPermission(new Permisssions().canPerformCommand);
           
        }
       
        float slow = 0.2f;
        float fast = 0.5f;
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("goldcommands") || commandLabel.equalsIgnoreCase("gc")){
                        player.sendMessage(ChatColor.WHITE + "----- " + ChatColor.GOLD + "GoldCommands Help " + ChatColor.WHITE + "-----");
                        player.sendMessage( ChatColor.GREEN + "/gc location" + ChatColor.GOLD + " Gives you you're location");
                        player.sendMessage( ChatColor.GREEN + "/gc bed" + ChatColor.GOLD + " Gives you you're  bed location");
                        player.sendMessage( ChatColor.GREEN + "/gc speed " + ChatColor.DARK_AQUA + "[0:1]" + ChatColor.GOLD + "Allows you to set walking speed");
                        player.sendMessage( ChatColor.GREEN + "/gc time " + ChatColor.DARK_AQUA + "[morning:afternoon:night] " + ChatColor.GOLD + "Sets the time (requires permissions!)");
                        player.sendMessage( ChatColor.GREEN + "/gc fly " + ChatColor.DARK_AQUA + "[on:off] " + ChatColor.GOLD + "Toggles flight (requires permissions!");
                        player.sendMessage( ChatColor.GREEN + "/gc vanish " + ChatColor.GOLD + "Toggles invisibility (requires permissions");
                        player.sendMessage( ChatColor.GREEN + "/gc juggernuat " + ChatColor.GOLD + "Toggles super-stregnth (requires permissions");
                        player.sendMessage( ChatColor.GREEN + "/gc powder " + ChatColor.GOLD + "Go home, you're drunk!");
                    }if(args[0].equalsIgnoreCase("powder")){
                        player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 1200, 1));
                        player.sendMessage(ChatColor.GOLD + "Go HOme " + player.getDisplayName() + " YoU'Re DRunK!!!!!!!!");
                    }else if(args[0].equalsIgnoreCase("location")){
                        player.sendMessage(ChatColor.GOLD + "You are located at " + ChatColor.GREEN + player.getLocation());
                    }else if(sender.hasPermission(new Permisssions().canPerformCommand)){
                        if(args[0].equalsIgnoreCase("vanish")){
                            player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 12000, 2));
                            player.sendMessage(ChatColor.GOLD + "You are invisible for 10 minutes!");
                        }else if(args[0].equalsIgnoreCase("juggernaut")){
                            player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 12000, 2));
                            player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 12000, 2));
                            player.sendMessage(ChatColor.DARK_BLUE + "JUGGERNAUT........");
                        }else{
                            player.sendMessage(ChatColor.DARK_RED + "Unknown Command or not enough permissions!");
                        }
                    }if(args.length == 2){
                        if(args[0].equalsIgnoreCase("speed")){
                            if(args[1].equalsIgnoreCase("1")){
                                player.setWalkSpeed(slow);
                                player.sendMessage("You're the flash!");
                            }else if(args[1].equalsIgnoreCase("0")){
                                player.setWalkSpeed(fast);
                                player.sendMessage("You're as slow as a turtle.");
                            }                   
                        }else if(args[0].equalsIgnoreCase("fly")){
                            if(sender.hasPermission(new Permisssions().canPerformCommand)){
                                if(args[1].equalsIgnoreCase("on")){
                                    player.sendMessage(ChatColor.GOLD + "It's a bird! It's a plane! It's " + player.getDisplayName());
                                    player.setAllowFlight(true);
                                }else if(args[1].equalsIgnoreCase("off")){
                                    player.sendMessage(ChatColor.GOLD + "Prepare for landing!");
                                    player.setAllowFlight(false);
                                }
                            }
                        }
                    }
            return false;
        }
    }
                           
                            
    I'm new to java, bukkit plugin making, and bukkit forums so please help me out. A step by step or something to replace my old code would be helpful.
     
  2. Offline

    iTechRemix

    If you want to display the location in a more "friendly" way, you could replace
    PHP:
    player.sendMessage(ChatColor.GOLD "You are located at " ChatColor.GREEN player.getLocation());
    with
    PHP:
    player.sendMessage(ChatColor.GOLD "You are located at: " ChatColor.GREEN player.getLocation().getWorld().getName() + ", " player.getLocation().getX() + ", " player.getLocation().getY() + ", " player.getLocation().getZ());
    Is this what you were trying to do?
     
    EvilKanoa likes this.
  3. Offline

    ZachBora

    CyberDork34

    At the moment you are showing help no matter if there has been any args. After your check on the commandname, add an if before the help that checks if args.length is 0.

    and change this for an else if
    }if(args[0].equalsIgnoreCase("powder")){
     
  4. Offline

    CyberDork34

    Not really, but this helps immensely! Thanks!!

    nah, now all the commands with one argument just show the help screen and nothing else. Any other ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  5. Offline

    ZachBora

    I think you misunderstood what I said. Let me show you an example.


    Code:java
    1.  
    2. public boolean onCommand(CommandSender s, Command c, String l, String[] args)
    3. {
    4. if(l.equalsIgnoreCase("goldcommands") || l.equalsIgnoreCase("gc"))
    5. {
    6. if(args.length == 0)
    7. {
    8. //show help here
    9. return true;
    10. }
    11. else
    12. {
    13. String a0 = args[0].toString();
    14.  
    15. if (a0.equalsIgnoreCase("help"))
    16. {
    17. //show help here
    18. return true;
    19. }
    20. else if (a0.equalsIgnoreCase("powder"))
    21. {
    22. //do stuff
    23. return true;
    24. }
    25. else if (a0.equalsIgnoreCase("location"))
    26. {
    27. //do stuff
    28. return true;
    29. }
    30. //etc...
    31. }
    32. }
    33. }
    34.  
     
  6. Offline

    CyberDork34

    Ohhh..... I see now. Thank you so much :D
     
Thread Status:
Not open for further replies.

Share This Page