Solved Get same event players from another event?

Discussion in 'Plugin Development' started by MrPowWow, Apr 14, 2017.

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

    MrPowWow

    Hello, I'm trying to get the same player from event A to event B. Here's some code I have to make it more clear.

    @EventHandler
    public void onEntityDamageEvent(EntityDamageByEntityEvent evt) // EVENT A
    {
    Entity e = evt.getEntity();
    Entity damager = evt.getDamager();
    if ((e instanceof Player)) {
    final Player PLAYERA = (Player)e;
    final Player PLAYERB = (Player)damager;
    }
    }

    -- > I now want to put the players I used in Event A and use it for Event B.

    @EventHandler
    public void onCloseEvent(InventoryCloseEvent evt) // EVENT B
    {

    // somehow get Event A players to this and send them a message

    EventAplayers.sendMessage("test");

    }

    Thank you so much for your help and time <3
     
  2. Offline

    yPedx

    @MrPowWow
    You could use HashMaps to store them.
     
  3. Offline

    MrPowWow

    Are you able to provide an example?
     
  4. Offline

    yPedx

    @MrPowWow
    Code:
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("A", PLAYERA.getName()); // Assigns PLAYERA to "A" key
    map.put("B", PLAYERB.getName()); // Assigns PLAYERB to "B" key
         
    map.get("PLAYERB"); // Gets a value associated with "PLAYERB" key, which is B
    map.get("PLAYERA"); // Gets a value associated with "PLAYERA" key, which is A
     
    Last edited: Apr 15, 2017
  5. Offline

    MrPowWow

    Thank you! How would I check if one of those players are in the hash map and how would I remove them?

    Also, If I wanted to send them a message what would I do?

    Sorry for the confusion. Thank you again
     
    Last edited: Apr 14, 2017
  6. Offline

    yPedx

    @MrPowWow
    I just noticed I wrote something wrong in the hashmap example, it would be like this instead;
    Code:
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    map.put("A", PLAYERA.getName()); // Assigns PLAYERA's name to "A" key
    map.put("B", PLAYERB.getName()); // Assigns PLAYERB's name to "B" key
    
    map.get("A"); // Gets a value associated with "A" key, which is PLAYERA's name
    map.get("B"); // Gets a value associated with"B" key, which is PLAYERB's name
    
    Anyway, you could do this in the second event;
    Code:
    Player playerA = map.get("A");
    Player playerB = map.get("B");
    
    // Then send them the message
    
    playerA.sendMessage();
    PlayerB.sendMessage();
     
  7. Offline

    MrPowWow

    You're truely a life safer. Thank you

    Just tried it but it won't work with a int or string.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 15, 2017
  8. Offline

    yPedx

    @MrPowWow
    Where did you try int and string?
     
  9. Offline

    MrPowWow

    In the events. I'm using a CloseInventoryEvnet and enitydamgebyentityevent. What I'm trying to do is add the hash map to the player in entitydamagedbteneityevnrt and check if they have the hashmap in closeinventoyevent and remove and send message.
     
  10. Offline

    yPedx

    @MrPowWow
    It's quiet hard to understand what you're trying to say. Are you saying you are saving the player from the first event, then check if the hashmap contains the names, then use it in the second event?

    ( By the way, again I failed a bit on the hashmap example, It's not supposed to be Integer but String xD )
     
  11. Offline

    TheHadDad

    @yPedx
    I've never worked with hashmaps before but when you failed a bit on the hashmap example do you mean:
    Code:
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    map.put("A", PLAYERA.getName()); // Assigns PLAYERA's name to "A" key
    map.put("B", PLAYERB.getName()); // Assigns PLAYERB's name to "B" key
    
    map.get("A"); // Gets a value associated with "A" key, which is PLAYERA's name
    map.get("B"); // Gets a value associated with"B" key, which is PLAYERB's name
    Code:
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    Change to

    Code:
    HashMap<String, String> map = new HashMap<String, String>();
    Also, is it possible to make a HashMap arraylist? ArrayList<HashMap> hm = new ArrayList<HashMap>
    ????
     
  12. Offline

    yPedx

    @TheHadDad
    Yes that's what I meant.
    I'm not sure about that, but google is your friend.
     
  13. Offline

    MrPowWow

    If I were to change it to a string, You can't cast a player with a string it would have to be <Player, Player>. But if I were to to that, How would I put the player onto the hash map and see if they contain the hash map?

    Code:
     
    @EventHandler
    
      public void onEntityDamagsadasdasdelasdsadawl(EntityDamageByEntityEvent evt)
    
      {
    
        Entity e = evt.getEntity();
    
        Entity damager = evt.getDamager();
    
        if ((e instanceof Player))
    
        {
    
          final Player PLAYERA = (Player)e;
    
          final Player PLAYERB = (Player)damager;
    
    
    
            evt.setCancelled(true);
    
            if ((damager instanceof Player)) {
    
    
    
                map.get("A"); //
    
                map.get("B");
    
         
    
           
    
                  map.put("A", PLAYERA.getName()); // ERROR
    
                  map.put("B", PLAYERB.getName()); // ERROR
    
    
    
            }
    
        }
    
      }
    
    
    
    @EventHandler
    
    public void onECloseEvent(InventoryCloseEvent event) {    
    
          HumanEntity p = event.getPlayer();
    
    
    
    // Check somehow to see if p is in the hash map, if so do the following
    
                        Player d = map.get("B"); 
    
    
    
                        Player b = map.get("A");  
    
    
    
        // remove the hash map from both players      
    
    
    
                        d.sendMessage(ChatColor.RED + "test");
    
    
    
                          b.sendMessage(ChatColor.RED + "test");
    
    
    
    
    
    
      }
    
    
            }
     
    Last edited: Apr 16, 2017
  14. Offline

    yPedx

    @MrPowWow
    Is that your whole code? It's only one event now.
    If you could post the whole code, if there is more I'll try it out and find a solution.

    EDIT: Sorry! I didn't notice I could scroll down.
     
  15. Offline

    MrPowWow

    There's two events. Try Scrolling down some.
     
  16. Offline

    yPedx

    @MrPowWow
    I've come up with a solution! I apologize for giving you wrong information, but it should work now :)
    Code:
    niuby plugin = this;
      
        HashMap<String, Player> map = new HashMap<String, Player>();
      
        public void onEnable() {
          
            plugin.getServer().getPluginManager().registerEvents(this, this);
        }
      
       
        @EventHandler
    
          public void onEntityDamage(EntityDamageByEntityEvent evt)
          {
            Entity e = evt.getEntity();
            Entity damager = evt.getDamager();
    
            if (!(e instanceof Player))
            {
                evt.setCancelled(true);
            } else {
              final Player PLAYERA = (Player)e;
              final Player PLAYERB = (Player)damager;
              
                if ((damager instanceof Player)) {
    
                      map.put("A", PLAYERA.getPlayer());
                      map.put("B", PLAYERB.getPlayer());
                      Bukkit.broadcastMessage("This works :P map put.");
            }}
          }
        @EventHandler
        public void onInventoryClose(InventoryCloseEvent event) {  
              HumanEntity p = event.getPlayer();
              if(p.equals(event.getPlayer()))
              if(map.containsKey("A") && map.containsKey("B")) {
                            Player d = map.get("B");
                            Player b = map.get("A");
                        
                            d.sendMessage(ChatColor.RED + "test for B");
                              b.sendMessage(ChatColor.RED + "test for A");
              }}}
    If you look over the code, you can see what I've changed :)
    Copy the code and change the messages & actions, and you're good to go!
    Bukkit.broadcastMessage was for debugging purposes, as well as "test for b" and "test for a"
     
  17. Offline

    MrPowWow

    Dude. You're amazing. Thank you so much,
     
  18. Offline

    yPedx

    @MrPowWow
    By the way!
    Code:
    niuby plugin = this;
    Change "niuby" to whatever your main is called. My main was called that xD

    If your problem has been solved, you can mark the thread as solved :)
     
  19. Offline

    ipodtouch0218

    Don't use a player inside of a map. Use UUIDs instead.
    Also, that won't work if there are multiple people.
    Map<UUID, UUID>, first UUID being damager and second UUID being the damaged player.
     
  20. Offline

    MrPowWow

    Okay, yPedx, I tried your way and worked until in game when I closed the inventory. It crashed the server.

    ipodtouch0218, I can't send a message to a player using the UUID. May you provide an example? Thank you sir. :)
     
  21. Offline

    yPedx

    @MrPowWow
    Never thought about it, but you might need to clear the hashmap after use?
     
  22. Offline

    MrPowWow

    With <String, Player>? I used map.remove("A"); & map.remove("B");
     
  23. Offline

    yPedx

  24. Offline

    MrPowWow

    I've tried. Nothing seems to change anything. ;/
     
  25. @MrPowWow
    Scrap the whole storing player objects themselves in the HashMap. As @ipodtouch0218 correctly pointed out, you should instead use UUIDs. This is because player objects contain references to a bunch of other stuff which means they can't be properly unloaded if you're still holding onto that player reference.

    To get around the problem of "not being able to send messages to a UUID", you can simply use Bukkit.getPlayer(UUID) to get the player back from the UUID object.
     
    ipodtouch0218 likes this.
  26. Offline

    MrPowWow

    Oh, Sweet. One more question, When I add them into the hashmap i'm using
    Code:
     map.put(PLAYERB.getUniqueId(), PLAYERA.getUniqueId());
       map.put(PLAYERA.getUniqueId(), PLAYERB.getUniqueId());
    But then when I want to send them a message in the other event, How would I get those players? Sorry, I haven't really messed around with hashmaps before. Thank you so much.

    This is the second event:
    Code:
     @EventHandler
      public void onInventoryClose(InventoryCloseEvent event) {
            HumanEntity p = event.getPlayer();
            if(p.equals(event.getPlayer()))
            if(map.containsKey() && map.containsKey()) {// not sure what to put here
                          UUID d = map.get(); // or here
                          UUID b = map.get(); // or here.
                          Bukkit.getPlayer(b).closeInventory();
                          Bukkit.getPlayer(d).closeInventory();
                          Bukkit.getPlayer(b).sendMessage(ChatColor.RED + "test");
                            Bukkit.getPlayer(d).sendMessage(ChatColor.RED + "test");
                            map.remove(d);
                            map.remove(b);
                       
            }}
     
  27. Offline

    Denziz

    Hello, I am kinda new to Bukkit but I think you need to loop through the HashMap, here's how I did it. Hope it works :D

    Code:
            public HashMap<String, Integer> map = new HashMap<String, Integer>();
        
            public static void main(String[] args) {
                Main main = new Main();
                main.map.put("PlayerA", 1);
                main.map.put("PlayerB", 2);
                main.sendMessageToPlayersInsideHashMap("are both nubs :D");
            }
    
            public void sendMessageToPlayersInsideHashMap(String message) {
                for (Map.Entry<String, Integer> m : map.entrySet()) {
                    System.out.println(m.getKey() + " " + message + " ");
                }
            }
     
  28. Offline

    MrPowWow

    Thank you for your help and time but are you able to explain to me how this will work? I want it as if a player hits a player, both damager and damaged players will be added into the hashmap and creates an inventory. If they contain the hashmap and close there inventory, it sends them a message and closes both of there inventory.
     
  29. Offline

    ipodtouch0218

    @MrPowWow
    Since you want both the damager and the damaged player to be able to close their inventory, you need to store both players as keys (left part of the map), with the value (right part of the map) of the other player.
    To do that, do this (using UUIDS):
    Code:
    map.put(attacker, damaged);
    map.put(damaged, attacker);
    Why put them like that instead of using PlayerA and PlayerB as said before? Because this whole system would break down if you have multiple players who attacked each other, unlike this one.

    Now that we have both players inside of the map, we move onto the InventoryCloseEvent. Cast the HumanEntity e.getPlayer to a player. If the map .contains() the player's UUID, then use map.get() on the player's UUID and save it to a variable, so we can also close the inventory of the OTHER player. Next, for both UUID's, use map.remove() to remove them from the map.
    Code:
    if (map.contains(closedUUID)) {
          UUID otherPlayerUUID = map.get(closedUUID);
          Bukkit.getPlayer(otherPlayerUUID).closeInventory();
          map.remove(closedUUID);
          map.remove(otherPlayerUUID);
    }
    One more change to the EntityDamageByEntityEvent, check if either the attacker, or the damager, is already contained in the map. If so, .get() the UUID value that's already inside the map (since they reference each other, remember) and .remove() both of the UUIDs. Do this for both players.
     
  30. Offline

    MrPowWow

    Okay, I got it to work. Thank you but now it times out my server. Any ideas?

    EDIT: Found other solution. Thank you all for the help and time.
     
    Last edited: Apr 17, 2017
Thread Status:
Not open for further replies.

Share This Page