Solved Arraylists

Discussion in 'Plugin Development' started by FlareLine, Sep 23, 2013.

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

    FlareLine

    I have this class here: BBGame.java
    Code:
    package pack.flareline.blockbash;
     
    import java.util.ArrayList;
     
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class BBGame implements Listener {
     
        /* Activating Arrays */
        private ArrayList<Player> playerlist = new ArrayList<Player>();
        private ArrayList<Player> gteamlist = new ArrayList<Player>();
        private ArrayList<Player> pteamlist = new ArrayList<Player>();
     
        /* Activating Variables */
        private int ArenaID;
        private int players = playerlist.size();
        private int teams = 2;
        private int gteam = gteamlist.size();
        private int pteam = pteamlist.size();
     
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerJoin(PlayerJoinEvent event) {
     
            playerlist.add(event.getPlayer());
     
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String[] args) {
     
            String[] playerarray = new String[playerlist.size()];
            playerarray = playerlist.toArray(playerarray);
     
            if (cmd.getName().equalsIgnoreCase("getplayers")) {
                sender.sendMessage(playerarray);
                sender.sendMessage("Sent you the Player Array!");
                return true;
            }
     
            return false;
        }
    }
    
    Would this work to add players that join to the Arraylist playerlist, and then print all entries out to the player when they execute /getplayers ?
     
  2. Offline

    mydeblob

    FlareLine
    I don't believe so, I think you would use
    Code:java
    1. playerarray.toString()

    You could do something like
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("getplayers")){
    2. sender.sendMessage("Players in the game: " + playerarray.toString());
    3. return true;
    4. }
     
  3. Offline

    nisovin

    You can't just print out an array or a list. You'll need to iterate through it and build the string of player names yourself.
     
  4. Offline

    FlareLine

    Is there a reason this does not work anymore?
    BBMain.java (Main Class)
    Code:java
    1. package pack.flareline.blockbash;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class BBMain extends JavaPlugin {
    8.  
    9. @Override
    10. public void onEnable() {
    11.  
    12. getServer().getPluginManager().registerEvents(new BBGame(), this);
    13.  
    14. // Start Datafolder
    15.  
    16. File PluginDirectory = new File("plugins/BlockBash");
    17. if (!PluginDirectory.exists()) {
    18. PluginDirectory.mkdir();
    19. getLogger().info("Data Folder Created!");
    20. }
    21.  
    22. // End Datafolder
    23.  
    24. // Start Config
    25.  
    26. File ConfigFile = new File(this.getDataFolder() + "/config.yml");
    27. if (!ConfigFile.exists()) {
    28. this.saveDefaultConfig();
    29. getLogger().info("Default Config Created!");
    30. }
    31.  
    32. // End Config
    33.  
    34. }
    35.  
    36. @Override
    37. public void onDisable() {
    38.  
    39. }
    40. }
    41.  


    BBGame.java (Game and Listener Class)
    Code:java
    1. package pack.flareline.blockbash;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.EventPriority;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    12.  
    13. public class BBGame implements Listener {
    14.  
    15. /* Activating Arrays */
    16. private ArrayList<Player> playerlist = new ArrayList<Player>();
    17. private ArrayList<Player> gteamlist = new ArrayList<Player>();
    18. private ArrayList<Player> pteamlist = new ArrayList<Player>();
    19.  
    20. /* Activating Variables */
    21. private int ArenaID;
    22. private int players = playerlist.size();
    23. private int teams = 2;
    24. private int gteam = gteamlist.size();
    25. private int pteam = pteamlist.size();
    26.  
    27. @EventHandler(priority = EventPriority.NORMAL)
    28. public void normalLogin(PlayerJoinEvent event) {
    29.  
    30. playerlist.add(event.getPlayer());
    31. }
    32.  
    33. public boolean onCommand(CommandSender sender, Command cmd, String[] args) {
    34.  
    35. if(cmd.getName().equalsIgnoreCase("getplayers")){
    36. String[] playerarray = new String[playerlist.size()];
    37. playerarray.toString();
    38. sender.sendMessage("Players in the Array: " + playerarray.toString());
    39. return true;
    40. }
    41.  
    42. return false;
    43. }
    44. }
    45.  

    I have registered the commands via plugin.yml.
     
  5. Offline

    Compressions

    Create a StringBuilder, loop through the String[], and append each String to the StringBuilder with a space or comma.
     
  6. Offline

    XvBaseballkidvX

    I was able to do something like this for a little Battle Minigame that I made:
    Code:java
    1. static ArrayList<String> isPlaying = new ArrayList<String>();
    2.  
    3. //Your command stuff (to lazy to write it down :P)
    4. String s = isPlaying.toString();
    5. player.sendMessage(ChatColor.GRAY + s);


    It would send you a message like [playerName, playerName, etc]

    The way I added the players to the arraylist (When they join the game):
    Code:java
    1. isPlaying.add(player.getName());
     
  7. Offline

    FlareLine

    Sounds like a plan XvBaseballkidvX I'll give it a try and refer you if it helps alot :) Thanks
    Is there a reason my plugin is not executing?
     
  8. Offline

    Coelho

    To be noted:

    You should do List<String> list = new ArrayList<String>();
     
  9. Offline

    Rocoty

    You haven't set the executor for the command.

    1. Make the BBGame class implement CommandExecutor
    2. In your main class, in the onEnable method, change following
    Code:java
    1. @Override
    2. public void onEnable() {
    3. BBGame bbgame = new BBGame();
    4. getCommand("getplayers").setExecutor(bbgame);
    5. getServer().getPluginManager().registerEvents(bbgame, this);
    6.  
    7. //getServer().getPluginManager().registerEvents(new BBGame(), this); //Remove this line!
    8.  
    9. ...
     
  10. Offline

    FlareLine

    @Rocoty nah duh. Went through both ears and came out the other side! :p
     
  11. Offline

    Rocoty

    Meaning?
     
  12. Offline

    FlareLine

    I totally forgot how to do this. You had reminded me.
     
Thread Status:
Not open for further replies.

Share This Page