Problem with return false

Discussion in 'Plugin Development' started by Stjubit, Jan 28, 2014.

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

    Stjubit

    Hey ;)

    When I make /fragen <Message> then the Usage from the plugins.yml appears. Why? Normally, the Usage of the Command just appears, when the Plugins comes to the return false. But it doesn't and do it too. When I use the /fragen command and a player is on the server and in the pList, it funcs. I dont know why

    Here's the Code from the Main:
    Code:java
    1. package tk.stjubit.main;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.ArrayList;
    6. import java.util.Vector;
    7.  
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class Main extends JavaPlugin {
    17.  
    18. File file = new File("plugins/SimpleHelp","playerlist.yml");
    19. FileConfiguration list = YamlConfiguration.loadConfiguration(file);
    20.  
    21. public static ArrayList<String> pList=new ArrayList<>();
    22.  
    23. @Override
    24. public void onEnable() {
    25. System.out.println("[SimpleHelp] Vollstaendig geladen!");
    26.  
    27. if(!file.exists())
    28. {
    29. try {
    30. file.createNewFile();
    31. list.set("players","");
    32. list.save(file);
    33.  
    34. } catch (IOException e) {
    35. System.out.println("[SimpleHelp] Der Ordner ist nicht gefunden worden!");
    36. }
    37. }
    38. else
    39. {
    40. pList=(ArrayList<String>) list.getList("players");
    41. }
    42.  
    43. this.getCommand("fragen").setExecutor(new Fragen());
    44. this.getCommand("fragenadd").setExecutor(new Fragenadd());
    45. }
    46.  
    47. @Override
    48. public void onDisable() {
    49. System.out.println("[SimpleHelp] Wird geschlossen!");
    50. }
    51.  
    52. }
    53.  


    Heres the Code from the Fragen Command:
    Code:java
    1. package tk.stjubit.main;
    2.  
    3. import java.io.File;
    4. import java.util.ArrayList;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.configuration.file.YamlConfiguration;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Fragen extends JavaPlugin implements CommandExecutor {
    16.  
    17. File file = new File("plugins/SimpleHelp","playerlist.yml");
    18. FileConfiguration list = YamlConfiguration.loadConfiguration(file);
    19.  
    20. ArrayList<String> pList=Main.pList;
    21.  
    22. @Override
    23. public boolean onCommand(CommandSender sender, Command cmd, String label,
    24. String[] args) {
    25.  
    26. if(cmd.getName().equalsIgnoreCase("fragen"))
    27. {
    28. System.out.println("asd");
    29. if(!(sender instanceof Player))
    30. {
    31. System.out.println("[SimpleHelp] Dieses Command ist nur InGame verfuegbar!");
    32. return true;
    33. }
    34.  
    35. Player p=(Player) sender;
    36. sender.sendMessage("asd");
    37.  
    38. if(args.length>=1)
    39. {
    40. String frage="";
    41. for(int i=0;i<args.length-1;i++)
    42. {
    43. frage+=args[i]+" ";
    44. }
    45.  
    46. for(int i=0;i<pList.size();i++)
    47. {
    48. Player ziel=this.getServer().getPlayer(pList.get(i));
    49. if(ziel!=null)
    50. {
    51. if(ziel.isOnline())
    52. {
    53. ziel.sendMessage(ChatColor.YELLOW+"[SimpleHelp]"+ChatColor.RED+" "+p.getDisplayName()+" : "+frage);
    54. }
    55. }
    56. }
    57. p.sendMessage(ChatColor.YELLOW+"[SimpleHelp]"+ChatColor.RED+" Frage erfolgreich verschickt!");
    58. return true;
    59. }
    60. else
    61. {
    62. p.sendMessage(ChatColor.YELLOW+"[SimpleHelp]"+ChatColor.RED+" /fragen <Frage>");
    63. return true;
    64. }
    65. }
    66.  
    67. return false;
    68. }
    69.  
    70. }
    71. [/i]


    And heres the Code from the Fragenadd Command:
    Code:java
    1. package tk.stjubit.main;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.ArrayList;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13. import org.bukkit.entity.Player;
    14.  
    15. public class Fragenadd implements CommandExecutor {
    16.  
    17. File file = new File("plugins/SimpleHelp","playerlist.yml");
    18. FileConfiguration list = YamlConfiguration.loadConfiguration(file);
    19.  
    20. ArrayList<String> pList=Main.pList;
    21.  
    22. @Override
    23. public boolean onCommand(CommandSender sender, Command cmd, String label,
    24. String[] args) {
    25.  
    26. if(cmd.getName().equalsIgnoreCase("fragenadd"))
    27. {
    28. if(!(sender instanceof Player))
    29. {
    30. System.out.println("[SimpleHelp] Dieses Command ist nur InGame verfuegbar!");
    31. return true;
    32. }
    33.  
    34. Player p=(Player) sender;
    35.  
    36. if(p.hasPermission("simplehelp.add"))
    37. {
    38. if(args.length==1)
    39. {
    40. String pName=args[0].toLowerCase();
    41. if(pList.contains(pName))
    42. {
    43. p.sendMessage(ChatColor.YELLOW+"[SimpleHelp]"+ChatColor.RED+" Dieser Spieler ist bereits in der Liste!");
    44. return true;
    45. }
    46. pList.add(pName);
    47. list.set("players", pList);
    48. try {
    49. list.save(file);
    50. } catch (IOException e) {
    51. }
    52. p.sendMessage(ChatColor.YELLOW+"[SimpleHelp]"+ChatColor.RED+" Spieler "+pName+" wurde erfolgreich hinzugefĆ¼gt!");
    53. return true;
    54. }
    55. else
    56. {
    57. p.sendMessage(ChatColor.YELLOW+"[SimpleHelp]"+ChatColor.RED+" /fragenadd <Name: Spieler>");
    58. return true;
    59. }
    60. }
    61. else
    62. {
    63. p.sendMessage(ChatColor.YELLOW+"[SimpleHelp]"+ChatColor.RED+" Du hast keine Rechte!");
    64. return true;
    65. }
    66. }
    67.  
    68. return false;
    69.  
    70. }
    71.  
    72. }
    73.  


    Thanks for the help, you guys are awesome!

    Mfg,
    Stjubit
     
  2. Stjubit
    Problem 1: Only your Main class should extend JavaPlugin

    Also, try getting rid of all the return trues/falses except for the one at the end of the onCommand() and see what happens.
     
    Stjubit likes this.
  3. Offline

    Stjubit

    The Gaming Grunts - Thanks

    I have tried like you said, but it doesn't make any sense.

    The only thing i couldn't change was, that i can't delete the extends JavaPlugin, because when I delete it, I cannot make the this.getServer().getPlayer(); - Command.!

    Do you or anyone have another suggestion?
     
  4. Stjubit
    Why not do Bukkit.getServer().getPlayer() ?
     
  5. Offline

    Stjubit

  6. Offline

    Stjubit

  7. Offline

    Th3Br1x

    Stjubit Can you show us your plugin.yml please?
     
  8. Offline

    Stjubit

    Th3Br1x
    sure, here it is:
    Code:
    name: SimpleHelp
    author: Stjubit
    version: 1.0
    main: tk.stjubit.main.Main
    commands:
      fragenadd:
        usage: /fragenadd <Spieler>
        description: Fuegt einen Spieler der Fragenliste hinzu
      fragen:
        usage: /fragen <frage>
        description: Sendet allen Admins Mods und Supps deine Frage
     
  9. Offline

    Shayana

    I notice you haven't registered the command. I'm not sure it will help because JavaPlugin also has an onCommand method that you can override, but for a CommandExecutor to execute a command, you need to add

    Code:java
    1. public class MyPlugin extends JavaPlugin {
    2.  
    3. //Method called when your plugin starts
    4. @Override
    5. public void onEnable() {
    6. //Then register each command of the plugin.yml using it's own CommandExecutor
    7. getCommand("fragen").setExecutor(new MyCommandExecutor());
    8.  



    [Edit] If your main class is also a command executor, you'll need to replace "new MyCommandExecutor() by "this" by the way
     
  10. Offline

    Th3Br1x

    Read the starting topic again please, he had this already implemented.

    Stjubit Do you have any errors in the console?
    Or is it only the usage message in the ingame-chat?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  11. Offline

    Stjubit

    Th3Br1x
    There are no errors, it is the usage ingame!

    It don't work anyways.

    Th3Br1x Can you add me in Skype? I am from Austria too! (german)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  12. Offline

    Th3Br1x

    I can (i'm not even from austria XD). I pm you my name.
     
    Stjubit likes this.
  13. Offline

    Alshain01

    Your pList array has a chance of being empty.

    Code:java
    1. if(!file.exists()) {
    2. // pList is empty here
    3. } else {
    4. // pList gets populated here
    5. }


    and while I'm here, don't ever String concatenate in a loop. Every time through the loop it creates a new string and copies the other two to it, very inefficient. Use a StringBuilder. And try to avoid for loops when iterators are available, also less efficient.

    Code:java
    1. StringBuilder frage= new StringBuilder();
    2. for(String i : args)
    3. {
    4. frage.append(args[i]);
    5. frage.append(" ");
    6. }
    7.  
    8. // String allArgs = frage.toString();
    9. [/i]
     
    Stjubit likes this.
  14. Offline

    Th3Br1x

    It's not. He just did a mistake extending the JavaPlugin class, but this was already mentioned and corrected.
     
    Stjubit likes this.
Thread Status:
Not open for further replies.

Share This Page