Unmute problems

Discussion in 'Plugin Development' started by BeastCraft3, May 26, 2014.

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

    BeastCraft3

    Greetings, I made a mute and unmute plugin. The mute is working but the unmute isnt working. If we use /unmute on a player he will still be muted. Please help. My code so far:
    Code:java
    1. package me.BeastCraft3.inventorygui;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.AsyncPlayerChatEvent;
    14.  
    15. public class Mute implements CommandExecutor, Listener{
    16.  
    17. public Main plugin;
    18.  
    19. public Mute(Main main) {
    20. main.getServer().getPluginManager().registerEvents(this, main);
    21. }
    22.  
    23.  
    24. public ArrayList<String> mute = new ArrayList<String>();
    25.  
    26.  
    27. @EventHandler
    28. public void onChat(AsyncPlayerChatEvent e) {
    29. Player p = e.getPlayer();
    30. if (mute.contains(p.getName())) {
    31. e.setCancelled(true);
    32. p.sendMessage(ChatColor.DARK_RED + "You are muted and have to wait until someone unmute you!");
    33. }
    34. }
    35.  
    36.  
    37. @SuppressWarnings("deprecation")
    38. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    39. if (!(sender instanceof Player)) {
    40. sender.sendMessage(ChatColor.GREEN + "Only Players can preform this command");
    41. return true;
    42. }
    43. Player player = (Player) sender;
    44. if(label.equalsIgnoreCase("mute")) {
    45. if (player.hasPermission("Hub.mute")) {
    46. if (args.length == 0) {
    47. player.sendMessage(ChatColor.RED + "Invalid args. Try /mute <Player Name>");
    48. return true;
    49. }
    50. if (args.length == 1) {
    51. Player online = Bukkit.getPlayer(args[0]);
    52. if (online == null || !online.isOnline()) {
    53. player.sendMessage(ChatColor.RED + "Player " + args[0] + " was not found ");
    54. } else {
    55. mute.add(online.getName());
    56. player.sendMessage(ChatColor.GREEN + "You have muted " + online.getName());
    57. online.sendMessage(ChatColor.DARK_RED + "You have been muted");
    58. }
    59. }
    60. }
    61. }
    62. if (label.equalsIgnoreCase("unmute")) {
    63. if (player.hasPermission("Hub.unmute")) {
    64. if (args.length == 0) {
    65. player.sendMessage(ChatColor.RED + "Invalid args. Try /unmute <Player Name>");
    66. return true;
    67. }
    68. if (args.length == 1) {
    69. Player online = Bukkit.getPlayer(args[0]);
    70. if (online == null || !online.isOnline()) {
    71. player.sendMessage(ChatColor.RED + "Player " + args[0] + " was not found ");
    72. } else {
    73. mute.remove(online.getName());
    74. player.sendMessage(ChatColor.GREEN + "You have unmuted " + online.getName());
    75. online.sendMessage(ChatColor.DARK_RED + "You have been unmuted");
    76. }
    77. }
    78. }
    79. }
    80. return false;
    81. }
    82.  
    83. }
    84.  
     
  2. Offline

    minoneer

    Not sure if this is causing your issues, but you should never use an asynch event like this. Either use the PlayerChatEvent (the synchronised one) or implement proper concurrency handling.
     
  3. Offline

    rsod

    Instead of
    ArrayList<String>
    useHashSet<String>
    and it should work fine then.
     
  4. Offline

    minoneer

    Nope, HashSet is not thread save either. Quote from the Oracle docs:

     
  5. Offline

    BeastCraft3

    minoneer
    rsod
    Everyone on youtube did this and they manage to make it work
     
  6. Offline

    coasterman10

    BeastCraft3 Youtube is a very poor source for learning to program well. You need to consider the possibility that the mute command is run at the same exact time the asynchronous chat event is called, which would likely cause some issues. The easiest way is to use Collections.synchronizedList() to create a synchronized list.
     
  7. Offline

    BeastCraft3

    coasterman10
    Im new to java and your talking to advanced ;3
     
  8. Offline

    Synapz

    Try
    Code:java
    1. Player targetPlayer = player.getServer().getPlayer(args[0]);


    instead of the

    Code:java
    1. Player online = Bukkit.getPlayer(args[0]);


    Also, because of the UUID change, if this is 1.7.9 then I would replace the targetPlayer.getName() with targetPlayer.getUniqueId()
     
  9. Offline

    Rocoty

    BeastCraft3 Since you haven't posted your main class, I can make a guess. You might be calling new Mute(this) twice, thus you are working with two completely separate Lists. But again, this is just a guess.
     
  10. Offline

    coasterman10

    BeastCraft3 Instead of using new ArrayList<String>, use Collections.synchronizedList(). If you can't figure this out then I suggest you learn more Java before continuing with Bukkit.
     
  11. Offline

    BeastCraft3

  12. Offline

    Rocoty

    BeastCraft3 Alright. Seeing your code confirmed my concerns. So don't instantiate the class more than once. In fact, looking at your code, it doesn't seem like you have the faintest idea of what you're doing, so you might as well read some Java tutorials.
     
  13. Offline

    BeastCraft3

    Rocoty
    omg.. I've tried to many java tutorials but this is the only place I actually learn. all the other is just watching a video without thinking.
     
  14. Offline

    Rocoty

  15. Offline

    BeastCraft3

    Rocoty
    I'll just buy a lesson from bcbroz. He has cheap prices
     
  16. Offline

    minoneer

    If you're not opposed to books (yes, those things made of paper ;) ), "Head first Java" is an awesome book to learn Java with. It teaches a lot of background, while being very entertaining and easy to read and understand.
     
  17. Offline

    Code0

    Getting back to the original question of this thread,

    Synapz 's answer should fix your problem.
     
  18. Offline

    werter318

    Synapz When will people learn that Bukkit.getPlayer(String name) is the same as Bukkit.getServer().getPlayer(String name). It's a wrapper...
     
  19. Offline

    Rocoty

    Code0 I must disagree. The problem is that OP is working with three different instances of a class of which he should have only been working with one instance. If you look at the code from the pastebin link he posted, you'll see that this is a recurring toxic practice in his code.
     
  20. Offline

    fireblast709

    BeastCraft3
    1. Don't use Logger.getLogger("Minecraft"), use the Logger object you get from JavaPlugin#getLogger()
    2. For your own safety, remove that static instance of your main class.
    3. Use a (Hash)Set instead of any List implementation, due to the fact that Sets are way faster than Lists when it comes to contains().
    4. As mentioned before, learn to synchonize or use the non async events. While async is preferred, ignoring thread safety like in your current implementation is no better. The easiest way to synchronise in this case is by using Collections.synchronisedSet(new HashSet<String>());
    5. When the player is offline, Bukkit.getPlayer(String) returns null, which throws an NullPointerException is you use .isOnline(), thus check if(player == null)
     
    coasterman10 and Gnat008 like this.
  21. Offline

    BeastCraft3

    fireblast709
    Could you give me the code for both of em? Please im close to give up cause I never manage to get this to work.
     
  22. Offline

    fireblast709

    BeastCraft3 I don't spoonfeed code. You can find the actual answer to the issue in Rocoty 's last post, and additional pointers in my last post which will prevent some future issues.
     
    Rocoty likes this.
  23. Offline

    coasterman10

    It would be best to get a good Java book and become more familiar with the concepts behind the language. It will help you greatly both now and later on when you get into more complex plugins. Once you are familiar then all the advice here will be easy to implement into your program.
     
    Rocoty likes this.
Thread Status:
Not open for further replies.

Share This Page