Solved Explain in here!

Discussion in 'Plugin Development' started by TechCommandCraft, Dec 16, 2016.

Thread Status:
Not open for further replies.
  1. I am currently working with an rpg plugin like a little chat plugin if you are in a certain group only the players in that group can see your messages and the others not! But I have a little problem with an getStringList It looks like the first person in the list gets taken 2 times! I will post my code players.yml and groups.yml and what it says on the player in the first place and the other person!




    Code:
    package me.techcommandcraft.rpgchat.events;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    import me.techcommandcraft.rpgchat.Core;
    import me.techcommandcraft.rpgchat.SettingsManager;
    
    public class Chat implements Listener {
    
        Core plugin;
    
        public Chat(Core instance) {
            plugin = instance;
        }
    
        SettingsManager settings = SettingsManager.getInstance();
    
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            Player player = (Player) e.getPlayer();
            String msg = e.getMessage();
            e.setCancelled(true);
    
            String senderGetGroup = settings.getPlayers().getString("players." + player.getName());
         
            ArrayList<String> messageRecieversArrayList = new ArrayList<String>();
    
            for (String playerFromGroup : settings.getGroups().getConfigurationSection("groups." + senderGetGroup)
                    .getStringList("players")) {
                messageRecieversArrayList.add(playerFromGroup);
    
                for (Player searchPlayers : Bukkit.getOnlinePlayers()) {
                    if (messageRecieversArrayList.contains(searchPlayers.getName())) {
                        searchPlayers.sendMessage(player.getName() + ": " + msg);
                    }
                }
    
            }
    
        }
    }
    
    Code:
    players:
      TechCommandCraft: elf
      karingijs: elf
    

    Code:
    groups:
      elf:
        prefix: ''
        players:
        - TechCommandCraft
        - karingijs
    

    Code:
    If "karingijs" talks:
    karingijs screen:
    
    karingijs: test
    
    TechCommandCraft screen:
    
    karingijs: test
    karingijs: test
    
    If "TechCommandCraft" talks:
    karingijs screen:
    
    TechCommandCraft: test
    
    TechCommandCraft screen:
    
    TechCommandCraft: test
    TechCommandCraft: test
    


    I already fixed it ^^ i putted the second "for" loop in the first loop! I was had this problem for like 1 hour that's why I posted it if an admin see's this you can delet it!
     
    Last edited: Dec 16, 2016
  2. Offline

    CeramicTitan

    You might experience performance issues with nested for loops
     
Thread Status:
Not open for further replies.

Share This Page