Solved Plugin Half Working... Need help.

Discussion in 'Plugin Development' started by Axanite, Jul 23, 2013.

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

    Axanite

    So I'm developing a PVP type plugin where you have to use commands to do stuff (e.g. join the lobby, choose your class, etc) and the join portion works as for now but what is bugging me is the class choosing. I've got it set up so it uses command arguments so you do /class (classname) and if you just do /class, it provides you with a list of the available classes to you. Now, thats all fine.... When I go to type /class and then the class name, it says what its meant to (telling you that you've successfully chosen the class) but before hand, it shows me the list of the classes. I am not sure why and I cannot figure out how to prevent this. Any help with be greatly appreciated!

    Source Code So Far:
    Code:java
    1. package com.omegagamers.rf2minecraft.PVPBattle;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public final class PVPBattle extends JavaPlugin {
    8. public void onEnable() {
    9. getLogger().info("PVPBattle by RobloxianFire2 has been enabled!");
    10. }
    11. public void onDisable() {
    12. getLogger().info("PVPBattle by RobloxianFire2 has been disabled!");
    13. }
    14.  
    15. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    16. if(cmd.getName().equalsIgnoreCase("join")) {
    17. sender.sendMessage(ChatColor.RED + "" + ChatColor.ITALIC + "" + "Please choose your class.");
    18. }
    19. if(cmd.getName().equalsIgnoreCase("class")) {
    20. sender.sendMessage(ChatColor.RED + "" + "Please choose your class:");
    21. sender.sendMessage(ChatColor.RED + "" + "Warrior: /class warrior");
    22. sender.sendMessage(ChatColor.RED + "" + "Archer: /class archer");
    23. if (args[0].equalsIgnoreCase("warrior")) {
    24. sender.sendMessage(ChatColor.GREEN + "You have chosen:" + "" + ChatColor.GOLD + "Warrior.");
    25. } else
    26. if(args[0].equalsIgnoreCase("archer")) {
    27. sender.sendMessage(ChatColor.GREEN + "You have chosen:" + "" + ChatColor.GOLD + "Archer.");
    28. }
    29. return true;
    30. }
    31. return false;
    32. }
    33. }
     
  2. Offline

    Dpasi314

    It's because you don't have a closing bracket after your class if statement.

    Code:
    if(cmd.getName().equalsIgnoreCase("class")) { <---
                sender.sendMessage(ChatColor.RED + "" + "Please choose your class:");
                sender.sendMessage(ChatColor.RED + "" + "Warrior: /class warrior");
                sender.sendMessage(ChatColor.RED + "" + "Archer: /class archer");
                if (args[0].equalsIgnoreCase("warrior")) { <---
                    sender.sendMessage(ChatColor.GREEN + "You have chosen:" + "" + ChatColor.GOLD + "Warrior.");
                } else
                if(args[0].equalsIgnoreCase("archer")) {
                    sender.sendMessage(ChatColor.GREEN + "You have chosen:" + "" + ChatColor.GOLD + "Archer.");
                }
    So the code will go all the way through.
    Try something like:
    Code:
    if(cmd.getName().equalsIgnoreCase("class")) {
                sender.sendMessage(ChatColor.RED + "" + "Please choose your class:");
                sender.sendMessage(ChatColor.RED + "" + "Warrior: /class warrior");
                sender.sendMessage(ChatColor.RED + "" + "Archer: /class archer");
    }
                if (args[0].equalsIgnoreCase("warrior")) {
                    sender.sendMessage(ChatColor.GREEN + "You have chosen:" + "" + ChatColor.GOLD + "Warrior.");
                } else
                if(args[0].equalsIgnoreCase("archer")) {
                    sender.sendMessage(ChatColor.GREEN + "You have chosen:" + "" + ChatColor.GOLD + "Archer.");
                }
     
  3. Offline

    Zewlzor

    You're not checking how many arguments you recieve. With your code, it'll display the arguments and then move onto the class if statements. Fixed/Prettied up a bit:

    Code:java
    1. if(cmd.getName().equalsIgnoreCase("class")) {
    2. if (args.length==0) {
    3. sender.sendMessage(ChatColor.RED + "" + "Please choose your class:");
    4. sender.sendMessage(ChatColor.RED + "" + "Warrior: /class warrior");
    5. sender.sendMessage(ChatColor.RED + "" + "Archer: /class archer");
    6. }else if (args.length==1){
    7. if (args[0].equalsIgnoreCase("warrior")) {
    8. sender.sendMessage(ChatColor.GREEN + "You have chosen:" + "" + ChatColor.GOLD + "Warrior.");
    9. } else
    10. if(args[0].equalsIgnoreCase("archer")) {
    11. sender.sendMessage(ChatColor.GREEN + "You have chosen:" + "" + ChatColor.GOLD + "Archer.");
    12. }else{
    13. sender.sendMessage(ChatColor.RED+"That is not a valid class!");
    14. }
    15. }else{
    16. sender.sendMessage(ChatColor.RED+"Correct usage: /class [class]");
    17. }
    18. return true;
    19. }
     
  4. Offline

    Axanite

    Cheers guys. Plugin is working fine now. :)
     
Thread Status:
Not open for further replies.

Share This Page