Error & (How do you force a player to say a message) ?

Discussion in 'Plugin Development' started by TheLittleBigBoss, Jun 3, 2014.

Thread Status:
Not open for further replies.
  1. Code:
     public void onPlayerLogin(final PlayerLoginEvent e) {
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    private Object welcome;
     
    public void run() {
    if (e.getPlayer().hasPlayedBefore()) return;
    for (String message : this.welcome ) {
    CommandSender sender;
    sender.sendMessage(ReplaceColours(message));
    }
    Error: (at for (String message : this.welcome ) { Line)
    Can only iterate over an array or an instance of java.lang.Iterable



    Also can someone tell me how do you force a player to say a message ?

    I am quite beginner in these plugins so help is much appreciated
     
  2. Offline

    theguynextdoor

    1. Eclipse is telling you exactly what the error is. Can you even logically think in your head how you can iterate over an 'Object'?. Not only that, but you don't even initialise it to anything... You don't initialise sender to anything. So that is null and would throw an NPE. If you can't fix this, then you need to hit the java tutorials again.

    2. http://jd.bukkit.org/rb/apidocs/org/bukkit/entity/Player.html
     
  3. Offline

    Konkz


    Looks like he needs to hit Java tutorials for the first time.
     
  4.  
  5. Offline

    theguynextdoor

    I know, I read that, but this isn't a matter of being new at plugins but Java itself. A sound knowledge of the foundation (I.e Java) is vital and you are evidently lacking in this. How can you expect to do a drawing if you don't know how to hold a pencil?
     
  6. Offline

    theguynextdoor

    You have your for loop set up like
    Code:
    for(String message : this.welcome) {
    The first error here is the 'this.welcome' because you can't just iterate over an object. The thing you are iterating over has to be an array or something which is iterable (These include things such as ArrayLists). Now this should make sense because an array is pretty much a list, since I can define an array like
    Code:
    String[] strings = {"hello", "world"};
    As you can see in that definition, I have a number of elements, which means that I can go through this array and view each element (through the process of iteration). Now if you have your object, there is no way via code nor logic that I can iterate (go through each element) of an Object.

    You also don't actually initialise your welcome to anything either. This means that you are trying to iterate over a null instance (even if you could hypothetically iterate over an Object). Your welcome should be a String, and contain the message you want. I don't exactly know where you thought your message would come from with your current code.

    You then put a random CommandSender inside of your for loop. Now I know why you did this. You most likely did this because everyone knows from making commands that you can send a message to a command sender (which in most cases (especially those of noobs) is a player). However you have an object, which is not assigned to anything, and is therefore null. Therefore whenever you try to use that object you will get an NPE. You should use the player in the event.

    I also see you have a replace colours method, may I recommend you take a look at the Java docs for the ChatColor class, more specifically at the translateAlternateColorCodes method.
     
  7. theguynextdoor I am planning on sending a custom message in the config and making a random person chosen on the server to say it
     
  8. Offline

    JeremyPark

    I would recommend to you that, although that isn't very complex, you should probably start with more basic stuff.
     
  9. Offline

    Deleted user

    TheLittleBigBoss
    Look, knowledge in Java is required here. Even though you are new to this, doesn't mean that we can come in and give you code in hopes that you'll get it. It's like the old Chinese proverb.

    We can't just keep on giving you fish. You need to go and learn Java basics first.
     
    xYourFreindx likes this.
  10. TheLittleBigBoss That's a nice plan. However, as theguynextdoor is pointing out, you won't be able to do this unless you learn Java. Would you learn to draw before learning to hold a pencil? No. Would you learn to ride a bike before learning to walk? No. Would you learn scuba dive before learning to swim? No. So why would you learn to create plugins before learning Java? It may sound harsh, and I'm sorry about that, but it's just not possible.

    If you insist on not following that piece of advice, the only other advice I will give you is to give up. Like a lot of things, programming requires patience to have any measurable success in it. Impatience is the only reason people want to create plugins before learning Java.

    zombiekiller753 Don't follow the "I don't know the origin of that proverb, so it's Chinese" rule. It's not Chinese, and a simple Google search would clear that up. :)
     
  11. Offline

    Deleted user

    AdamQpzm
    Actually, I said it's Chinese because those are the first few results that come up, but when I add a few more keywords like 'history', I do get some results who say that it's from Oxford.
    I'm not following the 'I don't know the origin, so it's Chinese' rule, because I had actually thought it was Chinese :p
     
  12. TheLittleBigBoss Yes, but you haven't learned how to hold things yet. Please learn Java first. :)
     
  13. AdamQpzm I have but can't grasp it properly yet.
     
  14. Offline

    xJeremyCx

    To force a player send a message use player.chat(you msg here);
     
  15. Thanks!

    What if i need to choose a random person and then run it?

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

    Necrodoom

    Then you haven't learnt Java yet.
     
    AdamQpzm likes this.
  17. Offline

    xJeremyCx

    Learn Java first.

    Use Random.nextInt and then Bukkit.getOnlinePlayers()[randomInt]
     
  18. Since all of you are advising me to learn java, could you please tell me on good sources where i can learn java that will help me in bukkit development?
     
  19. Offline

    Konkz

    How do you like to learn, in school for example?
    Video (Watching someone do it.)
    Reading about it (Web based / book based)
     
  20. I like to watch someone do it, but even what i like more is 1v1 so i can get straight back replies if i have any doubts.
     
  21. Offline

    Konkz

    As a Developer you got to get used to waiting and patience, if you want a tutor you will have to pay unless you have contacts. You will have to have patience if you mock something up and spend 30 minutes trying to find that nasty NPE.

    EDIT: I suggest watching TheNewBoston's Java Basic Tutorials - they helped me a lot. Ahh, will never forget the apple and tuna class'
     
    es359 likes this.
  22. Konkz is there also screenshots+writing for java tutorials?

    For a book, i have Java for dummies.
     
  23. Offline

    Konkz

    Google it. Buy a book, don't know what you prefer.

    Took me one second to find this:
    http://www.homeandlearn.co.uk/java/java.html
     
  24. Konkz
    Code:java
    1. Player chatter3 = Bukkit.getOnlinePlayers()[random];
    2. chatter3.chat(str + joinmessage + " " + event.getPlayer().getName());


    Getting a error at the last bracket: syntax error, enter "}" to complete the statement
     
  25. Offline

    es359

    I always go so damn hungry watching his videos. Lol.


    You could try this site. I'm doing my computer science course with this program.

    http://www.bluepelicanjava.com/sampleLessons.htm

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
    Konkz likes this.



  26. Anyone?
     
  27. Offline

    Kobting

    If you really don't know what that error is, go learn more Java like everyone is suggesting.
     
  28. Kobting Want to see the rest of the code?
     
  29. Offline

    theguynextdoor

    You clearly have little intention of learning, and therefore I have little intention of helping. But what I shall say is that your IDE is telling you exactly how to fix it. It's telling you to enter a } for heaven's sake. You couldn't have more help if it hit you in the face.

    Somewhere you have opened a block, using a {, and have yet to close it.
     
Thread Status:
Not open for further replies.

Share This Page