Holding back chat for a player.

Discussion in 'Plugin Development' started by gjossep, Jul 6, 2012.

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

    gjossep

    How would hold back chat for a player for a while, and then latter on a command show it again?

    Like how buy craft does it when you are buying something


    Thanks
    Gjosse
     
  2. Offline

    gjosse

    Anyone?
     
  3. Offline

    gjossep

    Bump!
     
  4. simple. Paste this in a class called ChatLock.java
    Code:
            public static Vector<String> chatLocked_Users;
     
    public static void toggleUserChat(final Player player)
            {
                // make sure the player is real
                if(player == null) { return; }
     
                // initialize vector if not already intialized
                if(chatLocked_Users == null) { chatLocked_Users = new Vector<String>(); }
     
                // see if the user is already disabled
                final int indexOfPlayer = chatLocked_Users.indexOf(player.getName());
                if(indexOfPlayer > -1)
                {
                    // remove the player from list
                    chatLocked_Users.remove(indexOfPlayer);
                }
                else
                {
                    // add the player to the list
                    chatLocked_Users.add(player.getName());
                }
            }
    Call that with
    Code:
    ChatLock.toggleUserChat((Player) sender);
    then in a listener make the priority High
    then something like this *DON'T COPY ME... I'Z NOT WORKLS*
    Code:
        if(chatLocked_Users.contains(event.getPlayer().getName()))
        {
            // set the chat message event to false here.
        }
     
  5. Offline

    gjossep


    Thanks for your response, but this lis like muting the player, i want them to stop Reciving chat, and then latter then can view the chat that they missed.

    Bump

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

    EnvisionRed

    I'm pretty sure that this should work:
    Code:
    @EventHandler
        public void chat(PlayerChatEvent event){
            Set<Player> recipients = event.getRecipients();
            for (Player p : recipients){
                String name = p.getName();
                if (name.equalsIgnoreCase("some-player-name")) {
                    recipients.remove(p);
                }
            }
        }
     
  7. Offline

    stelar7

    if you want to stop the user from reciving a message then give it to them later, then store the chat and send it back later.
     
  8. Offline

    gjossep

    Thanks man, only one problem when i try to test it out. It works but also gives out a error:

    Code:
    Could not pass event PlayerChatEvent to FunStuff
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:460)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:786)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
        at java.util.HashMap$KeyIterator.next(HashMap.java:828)
        at nl.gjosse.Chat.BlockPlace(Chat.java:22)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302)
        ... 11 more
    
    It says the error is at line 22, wich is:
    Code:
            for (Player p : recipients){
    
    Whats wrong?
    Thanks

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  9. Offline

    EnvisionRed

    Odd, I'm pretty sure an enhanced for loop works with any collection or array, and getRecipients returns an set of players.
     
  10. Offline

    gjosse

    Ye that's why I'm confussed,
     
  11. you modinfing the list while you looping over it, you need to use iterators functions to modife them whitin the loop
     
  12. Offline

    gjosse

    How to do this? Thanks
     
  13. exampole ho to do this, now only modify to fit what you want:
    long code (open)
    Code:java
    1. /*
    2.   Remove an element from Collection using Java Iterator Example
    3.   This Java Example shows how to remove an element from underlying Collection using
    4.   Java Iterator's remove method.
    5. */
    6.  
    7. import java.util.Iterator;
    8. import java.util.ArrayList;
    9.  
    10. public class RemoveElementThroughIteratorExample {
    11.  
    12. public static void main(String[] args) {
    13.  
    14. //create an ArrayList object
    15. ArrayList aList = new ArrayList();
    16.  
    17. //populate ArrayList object
    18. aList.add("1");
    19. aList.add("2");
    20. aList.add("3");
    21. aList.add("4");
    22. aList.add("5");
    23.  
    24.  
    25. System.out.println("ArrayList before removal : ");
    26. for(int i=0; i< aList.size(); i++)
    27. System.out.println(aList.get(i));
    28.  
    29. //get an Iterator
    30. Iterator itr = aList.iterator();
    31.  
    32. //remove 2 from ArrayList using Iterator's remove method.
    33. String strElement = "";
    34. while(itr.hasNext()){
    35.  
    36. /*
    37.   Iterator's next method returns an Object so we need to cast it into
    38.   appropriate class before using it.
    39.   */
    40. strElement = (String)itr.next();
    41. if(strElement.equals("2"))
    42. {
    43. /*
    44.   Remove an element using remove() method of Iterator
    45.   Remove method removes an element from underlying collection and
    46.   it may throw a UnsupportedOperationException if the remove
    47.   operation is not supported.
    48.   */
    49. itr.remove();
    50. break;
    51. }
    52.  
    53.  
    54.  
    55. }
    56.  
    57. System.out.println("ArrayList after removal : ");
    58. for(int i=0; i< aList.size(); i++)
    59. System.out.println(aList.get(i));
    60.  
    61. }
    62. }
    63.  
    64. /*
    65. Output would be
    66. ArrayList before removal :
    67. 1
    68. 2
    69. 3
    70. 4
    71. 5
    72. ArrayList after removal :
    73. 1
    74. 3
    75. 4
    76. 5
    77. */
     
Thread Status:
Not open for further replies.

Share This Page