Help with Player Chat (Please)

Discussion in 'Plugin Development' started by nateracecar5, Jan 24, 2014.

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

    nateracecar5

    Okay, so I have some code, but I won't show it because it is a secret project that I think you guys will like.

    So I have a command that is used like "/pcgui <player name>". The command opens an inventory, but I have it so that the plugin stores the player name for control later (It's kind of an in-game control panel). I have a button in the inventory that says "Message the player", and when I click it, it sends the player a message saying "Please type your message into chat". I have two questions on this:

    1.) Will the inventory automatically close when I click the button, because I don't know how to close it if it doesn't.
    2.) How do I make it record AND cancel the player chat event right here, at this EXACT point?

    I really need help on this subject. I'm kind of new to this inventory manipulation thing.
     
  2. nateracecar5
    1. player.closeInventory()

    2. You can do something like this (not tested, but should work):

    Code:java
    1. HashMap<Player, Player> msgMap = new HashMap<Player, Player>();
    2. //make sure to add players to map when the inventory is clicked
    3. @EventHandler
    4. public void onChat(AsyncPlayerChatEvent e){
    5. Player sender = e.getPlayer();
    6. Player receiver = msgMap.get(sender);
    7.  
    8. if (msgMap.containsKey(sender)){
    9. e.setCancelled(true);
    10. receiver.sendMessage(e.getMessage());
    11. msgMap.remove(sender);
    12. }
    13. }


    Hope this helps :)
     
  3. Offline

    nateracecar5

    Thanks. I am about to export the plugin, so when everything checks, I will implement this. :)

    The Gaming Grunts One thing: How do I add them to the message map? msgMap.add(player);? I've never used hashmaps before, kind of a noob XD

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

    Garris0n

    Map.put(key, value);
     
  5. Offline

    Garris0n

    The Gaming Grunts
    You really shouldn't be storing the actual players in HashMaps...
     
  6. Garris0n
    It really doesn't matter all that much, as long as you remember to remove them. We're only talking about a few bytes of data. In this case, he needs to remember to remove them after the message and, incase one of the players leaves, check the map and remove the values when a player disconnects.
     
  7. Offline

    Garris0n

    1. It's not necessarily only a small bit of data, but I suppose fine.
    2. More importantly, the OP is clearly a new coder and would not know when it is and isn't okay to save the instances, and it's probably best to say "just save the names". In the end, there are better ways than that to save them, but it's hard to explain to somebody who's just getting started how it works.
     
  8. Garris0n
    You're right :p I should have shown him better examples, but at least he's learning something new :)
     
  9. Offline

    nateracecar5

    Either I didn't implement the methods correctly, or this isn't working. The events aren't cancelling, I'm guessing it's because the HashMap isn't working right, or the events are working, and the message isn't being sent. I don't know.
    :(

    The Gaming Grunts Needed to tag you for you to see this

    Never Mind, I did something and it worked. Not sure what, I think it was because as a precautionary measure, I put msgMap.remove(sender) in ALL my methods XD

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

    Jake6177

    nateracecar5 Please don't double or even triple post. Just edit your post. He would still receive the tag and thread would be much cleaner :)
     
  11. Offline

    nateracecar5

    Okay thanks. I'm not used to the forums either. I'm done with this thread now. :)
     
Thread Status:
Not open for further replies.

Share This Page