Issue with variable scope in Bukkit

Discussion in 'Plugin Development' started by HackerTyper, Aug 5, 2013.

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


    I am not sure whether this is a Java or a Bukkit question, but i will just ask it here:

    I am developing a simple plugin right now. It works like this: You enter the command /TRmimic [name of target]. After that, when you say something in the chat, it will look like the target player said that. For this to work properly, I need to stop the target player from saying anything. For that, I wrote this code:

    My listener code

    1. public class TRmimic implements Listener{
    3. /**Variable to store the name of the player who should be stopped
    4.   * from saying anything in the chat;
    5.   */
    6. String listed;
    8. @EventHandler
    9. public void onPlayerChat(AsyncPlayerChatEvent event){
    10. String senderName = event.getPlayer().getName(); //Gets the sender of the chat message
    11. if( senderName.equals(listed) ){ //IF the name of the sender equals the listed variable...
    12. event.setCancelled(true); //The chatEvent is cancelled, muting the player
    13. }
    14. }
    15. /**
    16.   * This method is invoked by the /trmimic command, it is an usual set method
    17.   * used to assign the name of the muted player to the listed variable
    18.   */
    19. public void setBlackListed(String playerNameToList){
    20. listed = playerNameToList;
    21. Bukkit.getServer().getPlayer(listed).sendMessage("You are listed!");
    22. //Sends a message to the listed player, indicating he should be muted
    23. }
    25. /**
    26.   * Sets the listed variable to null, allowing the muted player to talk again
    27.   */
    29. public void setNull(){
    30. listed = null;
    31. }
    34. }

    Command code:

    1. /**
    2. * This block of code is executed by the command
    3. */
    4. String targetName = args[0]; //Args[0] should be the player name
    5. mim.setBlackListed(targetName); //Invokes the setBlackListed method in the TRmimic class
    6. return true;

    I did some debugging, and when the ''listed'' variable is set to the player name in the setBlackListed method, the onChatEvent method does not recognize that, and still thinks the variable has no value yet. How can I solve this problem?
  2. Offline


    First, your code may actually be working, but your chat message might be calling the Chat event (perhaps but a logger message to see if the event is firing).

    Second, would it perhaps be easier/better to use either a Hashtable or a database to store the muted players? So instead of requiring an instance per player (expensive if you have a lot of players!), you could have one public static instance of a Hashtable or a database.
  3. What collielimabean said about using a storage variable is true. I think you should use a List<String> to hold the muted players because you can simply add/remove them from the list and use if (list.contains(event.getPlayer().getName()) event.setCancelled(true);

    Also, it is of course firing the AsyncPlayerChatEvent and your code is fine except:
    Unless you're using Bukkit.getPlayerExact(), you're forgetting that players sometimes type the first few letters of the player and Bukkit recognizes the player. You must use:
    If you want players to be able to black list offline players you can do:
    Player target = Bukkit.getPlayer(targetName);
    if (target != null) mim.setBlackListed(target.getName());
    else mim.setBlackListed(targetName);
  4. Offline


    First, i would store Player objects instead of strings, but in the FAQ i found that using the name solves 50 problems, so I switched over to string. Now i use an ArrayList, but it still does not work for some reason, when I store the string or player in a method, another method does not recognize it. Is there any way to circumvent this?
Thread Status:
Not open for further replies.

Share This Page