ArrayList help

Discussion in 'Plugin Development' started by XFarwar, Sep 28, 2016.

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

    XFarwar

    Hi, i'm trying to create am admin chat.. so i created an arraylist witch registers players who do the command /achat. So, how do I get the players in the arraylist? I did:
    Code:
    ...code...
    ArrayList<Player> staff = new ArrayList<Player>();
    for(Player s : Staff){
    S.sendMessage("balbla");
    }
    ...code...
    I think it's wrong because it doesn't work..
    Have you got any ideas??
     
  2. Online

    timtower Administrator Administrator Moderator

    @XFarwar You never add players to the list.
     
  3. Offline

    Evonoucono

    @XFarwar
    First of all your ArrayList of players should be an ArrayList of strings, the strings being the names/uuid of the players.
    To add something into the ArrayList just use:
    Code:
    staff.add(player.getName());
    That should run right after a player types the command /achat. The player variable obviously being the one who sent the command.
    Also your for loop is iterating over all players in "Staff", but your ArrayList's name is "staff" with a lower case s, as well you're sending messages to "S" which should be "s" (java is case sensitive...)
     
  4. Offline

    henrikF95

    you need to add the player who performs the command /adminchat to the arraylist and everytime someone chats
    you need to get all online Players and check if they have the permission or if they are op with a for loop
     
  5. Offline

    XFarwar

    Ok, here's my method:
    Code:
    @EventHandler
        public void onChat(PlayerChatEvent e){
            Player p = e.getPlayer();
            String msg = e.getMessage();
            if(Main.staffChat.contains(p.getName())){
                e.setCancelled(true);          
                for(Player on : Bukkit.getOnlinePlayers()){
                      if(on.hasPermission("Fun.staffchat")){
                       String name = p.getName();
                        on.sendMessage("§b(" + name + ")§7>" + msg);
                        }
                   }
            }
        }
    When i talk, the event doesn't cancel me the chat, even if im in the arraylist 'staffChat'.
    Console says no errors.. I think this is becouse i've another plugin who controls the chat (essentials chat and factions)..
    How can i solve this problem??
    @timtower
     
  6. Online

    timtower Administrator Administrator Moderator

    @XFarwar And what is staffChat for type?
     
  7. Offline

    XFarwar

    @timtower it is:
    public static ArrayList<String> staffChat = new ArrayList<String>();

    EDIT: I saw that the chat clears my messages when im in 'Chat Staff' only in console.. not in-game.

    2^ EDIT: Solved using AsyncPlayerChatEvent, :)
     
    Last edited: Oct 1, 2016
  8. Offline

    Zombie_Striker

    STOP ABUSING STATIC. If you want to access the arraylist, pass the instance of the main class through this class's constructor.

    Don't use Colorcodes. Use ChatColor instead.

    The reason why this is happening is because either arraylist does not contain the player's name, or you have not registered this class's listener.
     
  9. Offline

    XFarwar

    1) why not using static? It works fine
    2) why not using color codes? It works as chatcolor
    3) I solved the problem, i should use asyncplayerchat. Ive registered the class and arraylist contains players...
     
  10. Offline

    Zombie_Striker

    1. Static objects can cause memory leaks if not disposed of correctly.
    2. Server reloads do not account for static objects, so you may have multiple instance of the same arraylist.
    3. Static objects are used to create one instance for multiple objects. Since there can only be one instance of the main class, this feature is not used.
    4. There is no need for it to be static. It's in the main class. You (should) always have access to the main class, so you should always be able to access the array through the main class's instance.

    1. Minecraft/bukkit can change color codes any time they want (mojang has already changed numerous things seemingly just because they can). ChatColor will always be equal to their designated color if something like this happens (meaning your plugin will always be backwards compatible)
    2. Unless you remember all the color codes (which, kudos if you do), you have to look up/ experiment with the color codes.

    If this is the case, then you will need to debug. Figure out which line is "broken" or which if statement is not true.
     
  11. @Zombie_Striker @XFarwar
    Something to note on the ChatColor thing, is that if you think typing "ChatColor.BLUE" is too unwieldy, you can use a little handy thing called static imports. Just put "import static org.bukkit.ChatColor.*" at the top of your class, and then you can simply do "BLUE", instead of "ChatColor.BLUE".
     
  12. Offline

    I Al Istannen

    @AlvinB @Zombie_Striker @XFarwar
    To extend this, you don't need to add it by yourself obviously.
    Just write "ChatColor.BLUE" one time, then move your caret to "BLUE".

    IntelliJ:
    Alt + Enter -> "Add static import for"

    Eclipse:
    CTRL + SHIFT + M

    Won't do a star import as far as I know, but schould be okay too.
     
Thread Status:
Not open for further replies.

Share This Page