Using two events at the same time?

Discussion in 'Plugin Development' started by Sabersamus, Feb 23, 2012.

  1. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I know that you can have multiple events in the same class, but is it possible to use two events at the same time in that class?


    heres what i want (so its easier to explain)

    When a player drops an item, i want it to send the "dropper" a message saying who picked it up, like this
    Code:java
    1. player.sendMessage(ChatColor.YELLOW + "You gave " + <NameofPlayerWhoPickedUpItem> + " " +value + " " + item.getItemStack().getType().name().toLowerCase());




    and when a player picks up an item, like this
    Code:java
    1. player.sendMessage(ChatColor.YELLOW + "You got " + ChatColor.DARK_GREEN + item.getItemStack().getType().name().toLowerCase() + " from " + <NameOfPlayerWhoDroppedItem>);
  2. Offline

    Jogy34

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I don't see why you would need 2 events happening at the same time for something like this
  3. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    PlayerPickupItemEvent and PlayerDropItemEvent.... right? wouldnt i need both?
    or else how would i get who dropped the item AND who picked it up?
  4. Offline

    Jogy34

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Well yes you need both but why would you need them to function at the same time?
  5. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    so that i can get the name of who dropped something and the name of who picked something up at the same time

    like this
    Code:
    player.sendMessage(ChatColor.YELLOW + "You gave " +  <nameofplayrwhopickedup> + " " +value + " " + item.getItemStack().getType().name().toLowerCase());
    in order for that to work (this is player drop event btw) i need to be able to get the name of who picked up the item, and i have yet to find a way to do it, so if you have one please tell me
  6. Offline

    nisovin

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You're thinking about this all wrong. You can't make those events work at the same time, because they happen at different times. They could be separated by a second, or by several minutes. Instead of thinking about how to get those two events to work at the same time, you should be thinking about how to transfer some bits of information from one event to the other.
  7. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    and how would i do that?
  8. Offline

    Jogy34

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You could use a HashMap to store the item that a player dropped and then cross check it when a player picks it up like this:
    Code:
    HashMap<Item, Player> hashmap = new HashMap<Item, Player>();
    //on drop
    .....
    hashmap.put(item, player);
     
    //on pickup
    .......
    if(hashmap.contains(item)
    {
        player.sendMessage("Message");
        hashmap.get(item).sendMessage("Message");
        hashmap.remove(item);
    }
    
  9. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    i give up on this i dont understand it and im in a terrible mood. i wont get it to work i dont even know why i tried. sorry for wasting your time.
  10. Offline

    dillyg10

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    look at the code that @Jogy34 said :D. if you don't understand it, look @ bukkit tuts on wiki :)
  11. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    looking at his code isnt going to help if i dont understand it. i tried do it and it doesnt work. looking at a tutorial wont help. they are too general.
  12. Offline

    Jogy34

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Just ask about what you don't understand here. Many people will be happy to help unless it is something extremely basic like an if statement because you need to know at least the basics of java before you start trying to program for bukkit and even then people will typically point you in the direction of a good java tutorial. Anyways I'm getting a little off topic. What do you need explained.
  13. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    im not sure exactly how to use the hashmap in the "on pickup" part when i do if(hashmap.contains(item)){

    i get an error on contains (not sure how to fix that)
  14. Offline

    Jogy34

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Sorry that was a mistake on my part. It isn't .contains(); it's .containsKey();
  15. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    alright, how do i make it so that both players get a message? (the dropper of the item gets "you gave <playerwhopickedup> (itemname)!"

    and the player who picks it up gets "you got <item> from <droppingplayer>"

    ??
  16. Offline

    dillyg10

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    What I was saying is that the tutorial on the bukkit wiki has a great info on hashkeys! That is where I learned how to do mine... also the javadocs are amazing!! And as @Jogy34 said we are willing to help :D
  17. Offline

    dillyg10

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I would say on the event with the drop...
    hashkey.put(event.getPlayer())

    and the event with the pickup...
    hashkey2.put(event.getPlayer())
    Bukkit.braodcastMessage(hashkey.get()+" has picked up an item from " + hashkey2.get())
  18. Offline

    Jogy34

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    It's just this:
    Code:
    if(hashmap.containsKey(item) //this checks to see if the item picked up was dropped by a player
                               //assuming you stored it when the player dropped it
    {
        player.sendMessage("Message"); //player in this instance is the one you get from
                              //event.getPlayer();
        hashmap.get(item).sendMessage("Message"); //this gets the player that dropped the item
                              //that was stored in the HashMap
        hashmap.remove(item);//this gets rid of the item to save memory allocation for other things
    }
    
    A HashMap, by the way, stores a Key and a Value to go with the key <Key, Value>. You can then get the Value by looking for the Key and use it to do many other things. Don't worry it took me a week or two to understand HashMaps when I was first using them.

    This post has been edited 4 times. It was last edited by Jogy34 Feb 23, 2012.
  19. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    i dont get it.
    at all. i type "hashkep.put(event.getPlayer())" and nothing works
  20. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)

    but then how do i get
    "you got <item> from <player>" when a player picks it up?? :/

    it sends the message but i cant get the name of who dropped it :/

    This post has been edited 1 time. It was last edited by Sabersamus Feb 23, 2012.
  21. Offline

    Jogy34

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Are you using HashMap or HashKey?
  22. Offline

    Jogy34

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    It would be something like this:
    Code:
    player.sendMessage("You got " + event.getItem() + " from " + hashmap.get(event.getItem()).getName());
    

    This post has been edited 1 time. It was last edited by Jogy34 Feb 23, 2012.
  23. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    that seems to work :) but i cant test it on other people yet :3 only myself, thanks
  24. Offline

    Jogy34

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Any time.[cake]
  25. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    hmm, one last thing, how would i make it (i know how i just dont know EXACTLY what to put) so that if the player that picks it up is the player that drops it, it doesnt, send the message?

    Code:java
    1.  
    2. if(!(something not sure what).equals(somethingnotsure what :3 ))){
    3. //send the messages
    4. }
    5.  
  26. Offline

    dillyg10

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    A bit unrelated, how did you get the java highlighting :D..
  27. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
     [syntax=java] [/syntax] 
  28. Offline

    Jogy34

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    It would be:
    Code:java
    1. if(event.getPlayer() != hashmap.get(item))
    2. {
    3. //Stuff
    4. }

    This post has been edited 2 times. It was last edited by Jogy34 Feb 23, 2012.
  29. Offline

    dillyg10

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  30. Offline

    Sabersamus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)

    would that need to be
    Code:java
    1. if(event.getPlayer() != hashmap.get(item).getPlayer())
    2. {
    3. //Stuff
    4. }


    so that it gets the player?

Share This Page