Code not working..? Not sure why

Discussion in 'Plugin Development' started by HyrulesLegend, Nov 1, 2013.

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

    HyrulesLegend

    I was wondering, how would I go about checking if args 1 is a certain text? example: if args 1 is "test" then give the player an apple. How would I go about checking for what args 1 is

    When I do /lms set, it works, but /lms join and /lms host do not work. Any ideas?

    Code:
    Code:java
    1. public class Main extends JavaPlugin {
    2. SettingsManager settings = SettingsManager.getInstance();
    3. public ArrayList<String> lms = new ArrayList<String>();
    4.  
    5. public void onEnable() {
    6. settings.setup(this);
    7. }
    8. public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, String[] args){
    9. if(cmd.getName().equalsIgnoreCase("lms")){
    10. if(args.length <= 0){
    11. sender.sendMessage(ChatColor.GOLD + "bLMS Commands!");
    12. sender.sendMessage(ChatColor.GOLD + "/lms host - Host an LMS!");
    13. sender.sendMessage(ChatColor.GOLD + "/lms set - Set LMS Location!");
    14. sender.sendMessage(ChatColor.GOLD + "/lms join - Join LMS!");
    15. return true;
    16. }
    17.  
    18. if(args.length > 0){
    19. if(args[0].equalsIgnoreCase("set")){
    20. if(sender.hasPermission("lms.set")){
    21. Player p = (Player) sender;
    22. settings.getData().set("world", p.getLocation().getWorld().getName());
    23. settings.getData().set("x", p.getLocation().getX());
    24. settings.getData().set("y", p.getLocation().getY());
    25. settings.getData().set("z", p.getLocation().getZ());
    26. settings.saveData();
    27. p.sendMessage(ChatColor.RED + "Set loc!");
    28. return true;
    29. }
    30.  
    31. if(args.length > 0){
    32. if(args[0].equalsIgnoreCase("host")){
    33. if(sender.hasPermission("lms.host")){
    34. Bukkit.broadcastMessage(ChatColor.GOLD + "An LMS is being hosted by " + sender.getName() + "!");
    35. // not done yet
    36. return true;
    37. }
    38.  
    39. if(args.length > 0){
    40. if(args[0].equalsIgnoreCase("join")){
    41. sender.sendMessage(ChatColor.RED + "You have joined the LMS!");
    42. lms.add(sender.getName());
    43.  
    44. World w = Bukkit.getServer().getWorld(settings.getData().getString("world"));
    45. double x = settings.getData().getDouble("x");
    46. double y = settings.getData().getDouble("y");
    47. double z = settings.getData().getDouble("z");
    48. Player p = (Player) sender;
    49. p.teleport(new Location(w, x, y, z));
    50. p.sendMessage(ChatColor.RED + "LMS has began!");
    51. return true;
    52. }
    53. }
    54.  
    55.  
    56. }
    57. }
    58. }
    59.  
    60.  
    61. }
    62. }
    63. return false;
    64. }
    65. }
     
  2. Offline

    JPG2000

  3. Max_The_Link_Fan
    To add on to what JPG2000 said, you just do this for example:

    Code:java
    1. if (args[0].equalsIgnoreCase("happy")){
    2. //checks if the first arg is equal to the word happy, no matter
    3. //what capitalization it has
    4. }
    5.  
    6. //you can also do this to check if it exactly matches a specific text
    7. if (args[0].equals("Halloween")){
    8. }
     
  4. Offline

    HyrulesLegend

    For some reason, this isn't working:
    Code:java
    1. public boolean onCommand(final CommandSender sender, Command smd, String commandLabel, String[] args){
    2. if(commandLabel.equalsIgnoreCase("lms")){
    3. if(args.length == 0){
    4. sender.sendMessage(ChatColor.RED + "Invalid arguements!");
    5. return true;
    6. }
    7.  
    8. if(args.length == 1){
    9. if(args[1].equalsIgnoreCase("join")){
    10. sender.sendMessage(ChatColor.RED + "You have joined the LMS!");
    11. lms.add(sender.getName());

    JPG2000

    Plugin.yml:
    Code:
    name:  bLMS
    main:  me.hydrations.LMS.Main
    version:  1.0
    commands:
      lms:
        description: ajfd
     
  5. Offline

    JPG2000

    Max_The_Link_Fan change args[1] to args[2]. The number in the array directly reffers to the argument number. args[1] will be the FIRST argument.
     
  6. Offline

    HyrulesLegend

    JPG2000 The cmd should be /lms join, wouldn't args[1] be /lms {args1}?
     
  7. Offline

    johnnywoof

    JPG2000
    0 is actually the first one.
    I always got confused when I first started learning java.
    Max_The_Link_Fan
    Your plugin.yml is perfect, but try this code.
    Code:
        public boolean onCommand(final CommandSender sender, Command smd, String commandLabel, String[] args){
        if(commandLabel.equalsIgnoreCase("lms")){
        if(args.length <= 0){
        sender.sendMessage(ChatColor.RED + "Invalid arguements!");
        return true;
        }
     
        if(args.length > 0){
        if(args[0].equalsIgnoreCase("join")){
        sender.sendMessage(ChatColor.RED + "You have joined the LMS!");
        lms.add(sender.getName());
    Edit: That is not an "o"
    Blame the font xD
     
  8. Offline

    MayoDwarf

    Think of an argument as another word. When I say "Hey" that is the command bc it has no arguments to it. If I say "Hey there" there is args[0] bc it is my first argument. (My first word spaced after my command)
     
  9. Offline

    HyrulesLegend

    johnnywoof JPG2000
    Everything is now working except these lines:
    Code:java
    1. if(args.length > 0){
    2. if(args[0].equalsIgnoreCase("set")){
    3. if(sender.hasPermission("lms.set")){
    4. Player p = (Player) sender;
    5. settings.getData().set("world", p.getLocation().getWorld().getName());
    6. settings.getData().set("x", p.getLocation().getX());
    7. settings.getData().set("y", p.getLocation().getY());
    8. settings.getData().set("z", p.getLocation().getZ());
    9. settings.saveData();
    10. p.sendMessage(ChatColor.RED + "Set loc!");
    11. return false;
     
  10. Offline

    johnnywoof

    Max_The_Link_Fan
    Error? The most powerful debugger is the error in the console/log.
    Also what is the variable settings? I assume it is a config but I want to confirm that.
     
  11. Offline

    HyrulesLegend

    johnnywoof Variable settings? Also, there are no errors.
     
  12. Offline

    johnnywoof

    Max_The_Link_Fan
    What type is the variable "settings". Example, "settings" is type String.
    Sorry I made it confusing.
    Anyways you'll need to post your entire class, as I can not detect anything wrong with the current snippet.
     
  13. Offline

    HyrulesLegend

    Code:java
    1. package me.hydrations.LMS;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.World;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13. import org.bukkit.scheduler.BukkitRunnable;
    14.  
    15.  
    16. public class Main extends JavaPlugin {
    17. SettingsManager settings = SettingsManager.getInstance();
    18. public ArrayList<String> lms = new ArrayList<String>();
    19.  
    20. public void onEnable() {
    21. settings.setup(this);
    22. }
    23. public boolean onCommand(final CommandSender sender, Command smd, String commandLabel, String[] args){
    24. if(commandLabel.equalsIgnoreCase("lms")){
    25. if(args.length <= 0){
    26. sender.sendMessage(ChatColor.GOLD + "bLMS Commands!");
    27. sender.sendMessage(ChatColor.GOLD + "/lms host - Host an LMS!");
    28. sender.sendMessage(ChatColor.GOLD + "/lms set - Set LMS Location!");
    29. sender.sendMessage(ChatColor.GOLD + "/lms join - Join LMS!");
    30. return true;
    31. }
    32.  
    33. if(args.length > 0){
    34. if(args[0].equalsIgnoreCase("join")){
    35. sender.sendMessage(ChatColor.RED + "You have joined the LMS!");
    36. lms.add(sender.getName());
    37.  
    38. new BukkitRunnable() {
    39. @Override
    40. public void run() {
    41. World w = Bukkit.getServer().getWorld(settings.getData().getString("world"));
    42. double x = settings.getData().getDouble("x");
    43. double y = settings.getData().getDouble("y");
    44. double z = settings.getData().getDouble("z");
    45. Player p = (Player) sender;
    46. p.teleport(new Location(w, x, y, z));
    47. p.sendMessage(ChatColor.RED + "LMS has began!");
    48.  
    49. }
    50. }.runTaskLater(this, 100);
    51. return true;
    52. }
    53. if(args.length > 0){
    54. if(args[0].equalsIgnoreCase("host")){
    55. if(sender.hasPermission("lms.host")){
    56. Bukkit.broadcastMessage(ChatColor.GOLD + "An LMS is being hosted by " + sender.getName() + "!");
    57. // not done yet
    58. return true;
    59. }
    60. if(args.length > 0){
    61. if(args[0].equalsIgnoreCase("set")){
    62. if(sender.hasPermission("lms.set")){
    63. Player p = (Player) sender;
    64. settings.getData().set("world", p.getLocation().getWorld().getName());
    65. settings.getData().set("x", p.getLocation().getX());
    66. settings.getData().set("y", p.getLocation().getY());
    67. settings.getData().set("z", p.getLocation().getZ());
    68. settings.saveData();
    69. p.sendMessage(ChatColor.RED + "Set loc!");
    70. return false;
    71. }
    72. }
    73.  
    74.  
    75. }
    76. }
    77. }
    78.  
    79.  
    80. }
    81. }
    82. return false;
    83. }
    84. }

    johnnywoof There's my full code, if you want the SettingsManager code, here:
    Code:java
    1. package me.hydrations.LMS;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.configuration.file.YamlConfiguration;
    10. import org.bukkit.plugin.Plugin;
    11. import org.bukkit.plugin.PluginDescriptionFile;
    12.  
    13. public class SettingsManager {
    14.  
    15. private SettingsManager() { }
    16.  
    17. static SettingsManager instance = new SettingsManager();
    18.  
    19. public static SettingsManager getInstance() {
    20. return instance;
    21. }
    22.  
    23. Plugin p;
    24.  
    25. FileConfiguration config;
    26. File cfile;
    27.  
    28. FileConfiguration data;
    29. File dfile;
    30.  
    31. public void setup(Plugin p) {
    32. cfile = new File(p.getDataFolder(), "config.yml");
    33. config = p.getConfig();
    34. //config.options().copyDefaults(true);
    35. //saveConfig();
    36.  
    37. if (!p.getDataFolder().exists()) {
    38. p.getDataFolder().mkdir();
    39. }
    40.  
    41. dfile = new File(p.getDataFolder(), "data.yml");
    42.  
    43. if (!dfile.exists()) {
    44. try {
    45. dfile.createNewFile();
    46. }
    47. catch (IOException e) {
    48. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create data.yml!");
    49. }
    50. }
    51.  
    52. data = YamlConfiguration.loadConfiguration(dfile);
    53. }
    54.  
    55. public FileConfiguration getData() {
    56. return data;
    57. }
    58.  
    59. public void saveData() {
    60. try {
    61. data.save(dfile);
    62. }
    63. catch (IOException e) {
    64. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save data.yml!");
    65. }
    66. }
    67.  
    68. public void reloadData() {
    69. data = YamlConfiguration.loadConfiguration(dfile);
    70. }
    71.  
    72. public FileConfiguration getConfig() {
    73. return config;
    74. }
    75.  
    76. public void saveConfig() {
    77. try {
    78. config.save(cfile);
    79. }
    80. catch (IOException e) {
    81. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml!");
    82. }
    83. }
    84.  
    85. public void reloadConfig() {
    86. config = YamlConfiguration.loadConfiguration(cfile);
    87. }
    88.  
    89. public PluginDescriptionFile getDesc() {
    90. return p.getDescription();
    91. }
    92. }
     
  14. Offline

    johnnywoof

    Max_The_Link_Fan
    Change this​

    if(commandLabel.equalsIgnoreCase("lms")){

    to this

    if(smd.getName().equalsIgnoreCase("lms")){
     
  15. Offline

    HyrulesLegend

    johnnywoof That still doesn't fix the error with /lms set not working. D:

    johnnywoof Found error in console!
    Code:
    01.11 21:40:29 [Server] INFO at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    01.11 21:40:29 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    01.11 21:40:29 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    01.11 21:40:29 [Server] INFO at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    01.11 21:40:29 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:524)
    01.11 21:40:29 [Server] INFO at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
    01.11 21:40:29 [Server] INFO at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftTask.run(CraftTask.java:53)
    01.11 21:40:29 [Server] INFO at me.hydrations.LMS.Main$1.run(Main.java:41)
    01.11 21:40:29 [Server] INFO at org.bukkit.craftbukkit.v1_6_R3.CraftServer.getWorld(CraftServer.java:823)
    01.11 21:40:29 [Server] INFO at org.apache.commons.lang.Validate.notNull(Validate.java:203)
    01.11 21:40:29 [Server] INFO java.lang.IllegalArgumentException: Name cannot be null
    01.11 21:40:29 [Server] WARNING Task #698 for bLMS v1.0 generated an exception
    johnnywoof Line 41 is:
    Code:java
    1. World w = Bukkit.getServer().getWorld(settings.getData().getString("world"));


    Updated OP.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  16. the getString is returning a null value
     
  17. Offline

    HyrulesLegend

  18. Offline

    Goblom

    Max_The_Link_Fan Line 10... try "if (args.length > 1) {"

    Edit: You are also checking args.length WAAYY too many times...
     
  19. Offline

    HyrulesLegend

    Goblom Line 10? It's (the args) are supposed to be 0.
     
  20. Code:java
    1.  
    2. public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, String[] args){
    3. if(cmd.getName().equalsIgnoreCase("lms")){
    4. if(args.length <= 0){
    5. sender.sendMessage(ChatColor.GOLD + "bLMS Commands!");
    6. sender.sendMessage(ChatColor.GOLD + "/lms host - Host an LMS!");
    7. sender.sendMessage(ChatColor.GOLD + "/lms set - Set LMS Location!");
    8. sender.sendMessage(ChatColor.GOLD + "/lms join - Join LMS!");
    9. return true;
    10. }
    11.  
    12. if(args.length > 0){
    13. if(args[0].equalsIgnoreCase("set")){
    14. if(sender.hasPermission("lms.set")){
    15. Player p = (Player) sender;
    16. settings.getData().set("world", p.getLocation().getWorld().getName());
    17. settings.getData().set("x", p.getLocation().getX());
    18. settings.getData().set("y", p.getLocation().getY());
    19. settings.getData().set("z", p.getLocation().getZ());
    20. settings.saveData();
    21. p.sendMessage(ChatColor.RED + "Set loc!");
    22. return true;
    23. }
    24. }else if(args[0].equalsIgnoreCase("host")){
    25. if(sender.hasPermission("lms.host")){
    26. Bukkit.broadcastMessage(ChatColor.GOLD + "An LMS is being hosted by " + sender.getName() + "!");
    27. // not done yet
    28. return true;
    29. }
    30. }else if(args[0].equalsIgnoreCase("join")){
    31. sender.sendMessage(ChatColor.RED + "You have joined the LMS!");
    32. lms.add(sender.getName());
    33.  
    34. World w = Bukkit.getServer().getWorld(settings.getData().getString("world"));
    35. double x = settings.getData().getDouble("x");
    36. double y = settings.getData().getDouble("y");
    37. double z = settings.getData().getDouble("z");
    38. Player p = (Player) sender;
    39. p.teleport(new Location(w, x, y, z));
    40. p.sendMessage(ChatColor.RED + "LMS has began!");
    41. return true;
    42. }
    43. }
    44. }
    45. return false;
    46. }
    47.  
     
  21. Offline

    HyrulesLegend

  22. bracket errors. you did a check to see if it was set, then inside of that you checked if it was host or join,
     
Thread Status:
Not open for further replies.

Share This Page