Solved Help with simple problem

Discussion in 'Plugin Development' started by Strahan, Mar 26, 2016.

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

    Strahan

    Hi. I'm a newb to Java. Just decided to learn how to make plugins. I'm sure I'm making some dumb mistake, but I can't figure out what I'm doing wrong here. I figured I'd start with something simple as my first plugin; you type /chat and the server chats with you. I had it do a String.join on the args so I can just switch on the joined string and catch stuff like "/chat how are you" easily. I decided to add a "choose" command where you can pass it items and have it make a choice like "/chat choose bk kfc fiveguys bww" and it would randomly choose one. Joining strings is not helpful for that so I figured I'd throw an IF in there and check if args[0] is choose, otherwise go to the joined part. Seems simple. But for some reason, every time I every time I do the command I get the "uhh i have no idea" thing as it fails the if check for choose and falls through to the joined part where it doesn't match any known chat option. I had it tell me what it sees as arg[0] just before the if and it said in chat "args0 is |choose|". I put the | | to see if it was padding or something. So I cannot figure out why the if statement I bolded below fails.......?

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player)sender;
            switch (cmd.getName().toLowerCase()) {
            case "chat":
                Random rnd = new Random();
                if (args.length == 0) {
                    SendChat(player, "Not much to say, huh?");
                } else {
                    Boolean chatted = false;
                    SendChat(player, "args0 is |" + args[0] + "|");
                   if (args[0].toLowerCase() == "choose") {
                        int choice = rnd.nextInt(args.length-2)+1;
                        SendChat(player, "After careful deliberation, I choose " + args[choice] + "!");
                        chatted = true;
                    } else {
                      String chatTopic = String.join(" ", args).toLowerCase();
                      switch (chatTopic) {
                      case "hello":
                          SendChat(player, "Hello there, " + player.getName());
                          chatted = true;
                          break;
                      case "heads or tails":
                          if (rnd.nextInt(1) == 0) {
                              SendChat(player, "Heads!");
                          } else {
                              SendChat(player, "Tails!");
                          }
                          chatted = true;
                          break;
                      case "how are you":
                          int rndint = rnd.nextInt(5);
                          switch (rndint) {
                          case 0:
                              SendChat(player, "I'm feeling great, thank you for asking!");    break;
                          case 1:
                              SendChat(player, "Not too bad, thanks.");    break;
                          case 2:
                              SendChat(player, "Meh.  I've had better days."); break;
                          case 3:
                              SendChat(player, "All I can tell you is, today sucks."); break;
                          case 4:
                              SendChat(player, "Why do you wanna know??"); break;
                          case 5:
                              SendChat(player, "Just another day, I have no real opinion."); break;
                          }
                          chatted = true;
                          break;
                      }
                    }
                    if (!chatted) SendChat(player, "Uhhh I have no idea what you are saying.");
                }
                break;
            }
        }
        return false;
    }
    This seems like a simple thing, it's driving me nuts that it doesn't work. I'm probably doing something stupid, can someone enlighten me? :)
     
    Last edited: Mar 26, 2016
  2. Offline

    mcdorli

    That's not a good idea, please don't learn java trough bukkit

    Don't compare strings with ==, they may look like a primitive, but thy're an object

    Use boolean instead of Boolean, the second one is an object, the first one is a primitive

    Please, fromat your code.
     
    Zombie_Striker likes this.
  3. Offline

    Strahan

    Thanks. I Googled java string comparison, I switched to .equals and it works now. I've no idea what you mean by "primitive", but I'll go ahead and change that.

    As to formatting, I don't know why it got the spaces screwy when I pasted. In Eclipse, it's all properly spaced and lined up.
     
  4. Offline

    mcdorli

    primitive is all the things you write with lowercase first letter (int, boolean, double, float, char (void is considered one, but that is an odd one)), they represent basic information (numbers, characters and true or false), and have an exact amount of bytes they take up (int: 32, double: 16, byte: 8, char: 8, long: (I think) 64, float: 32, boolean: 8 (booleans can be different on different systems, for example OsX represents them as 10000001 and 10000000)), objects are everything else, there are java built-in objects (JFrame, BufferedReader, Listener, Object itself, etc.), there are API-related ones (JavaPlugin, CommandExecutor, etc.), the object version of primitives (listed above) (Integer, Double, Float., Array kind of...)

    Because you can instatiate a String with a bunch of characters between quotes, people often think they're primitives. Also, you don't need to import it, because it's in the java.swing package, and that gets automagically imported.
     
  5. Offline

    Strahan

    Thanks for the info. I suppose I should take your advice and go spend some time on just Java tutorials and learning all this before jumping into plugins. My ultimate goal is a plugin that can play a sound effect on the server PC when a player joins and/or send an email. Probably a good idea to have a solid foundation of understanding before getting into all that complicated stuff. Thanks again.
     
  6. Offline

    Zombie_Striker

    If you want, you can use my plugins (Either Music or EventSounds) which can accomplish what you want.
     
Thread Status:
Not open for further replies.

Share This Page