Help me fix this piece of code

Discussion in 'Plugin Development' started by beatcomet, Oct 1, 2011.

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

    beatcomet

    Hi guys :)
    I need some help here :
    I made a plugin for private use but there is a problem. The plgin allows you to hear message within
    15 block radius, and all the code is written but after sending a message it's being duplicated and shown multiple times.

    here is the code (Player listener)

    Code:java
    1.  
    2. package com.beatcomet.specialchat;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.player.PlayerChatEvent;
    7. import org.bukkit.event.player.PlayerListener;
    8.  
    9. public class playerListener extends PlayerListener {
    10. Main plugin;
    11. public playerListener(Main instance){
    12. plugin = instance;
    13. }
    14.  
    15. public void onPlayerChat(PlayerChatEvent event){
    16. Player player = event.getPlayer();
    17. String[] msg = event.getMessage().split(" ");
    18. String msg1 = "";
    19. if(msg[0].equals("**")){
    20. event.setCancelled(true);
    21. for(int x = 1; x < msg.length;x++){
    22. msg1 = msg[1] + msg[x];
    23. }
    24. for(Player p : plugin.getServer().getOnlinePlayers()){
    25. if(player.getLocation().distance(p.getLocation()) <= 15){
    26. p.sendMessage(ChatColor.WHITE + "<" + ChatColor.RED + player.getName() + ChatColor.WHITE + ">" + ChatColor.DARK_PURPLE + msg1);
    27. }
    28. else{
    29. event.setCancelled(true);
    30. }
    31. }
    32. player.sendMessage(ChatColor.WHITE + "<" + ChatColor.RED + player.getName() + ChatColor.WHITE + ">" + msg1);
    33. }
    34. else{
    35. String massage = event.getMessage();
    36. for(Player p : plugin.getServer().getOnlinePlayers()){
    37. if(player.getLocation().distance(p.getLocation()) <= 15){
    38. event.setCancelled(true);
    39. p.sendMessage(ChatColor.WHITE + "<" + ChatColor.RED + player.getName() + ChatColor.WHITE + ">" + massage);
    40. }
    41. else{
    42. event.setCancelled(true);
    43. }
    44. }
    45. player.sendMessage(ChatColor.WHITE + "<" + ChatColor.RED + player.getName() + ChatColor.WHITE + ">" + massage);
    46. }
    47. }
    48. }
    49.  
    50.  


    thanks for helping :)
     
  2. Offline

    Ahniolator

    I believe your problem is on line 32 and 45. I see each message coming up after each for loop is executed.
     
  3. Offline

    Lolmewn

    --^ What he said. (or she, dunno :p)
     
  4. Offline

    beatcomet

    well the idea is that every player within the radius of 15 blocks will get the message, can I just move the message above?
     
  5. Offline

    Ahniolator

    The messages are already sent in the for loop. Trust me and delete them. If I'm wrong then you can just as easily put it back ;)

    Hey, ho, it's the story of the internet!
     
    tips48 likes this.
  6. Offline

    Lolmewn

    IKR! Everyone should see it ;)
     
Thread Status:
Not open for further replies.

Share This Page