Solved Randomly picking a person's name from a config StringList

Discussion in 'Plugin Development' started by ProSl3nderMan, Aug 28, 2015.

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

    ProSl3nderMan

    Hey guys, ran into a problem (again hehe). I can't get make it look through a config StringList and pick one name from the list to tp. I tried, and this is what I've got to:
    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(AccessControl.plugin, new Runnable() {
                       @Override
                       public void run() {
                          for (String myPlayerName : AccessControl.plugin.getConfig().getStringList("map1.players")) {
                                Player names2 = Bukkit.getPlayer(myPlayerName);
                                @SuppressWarnings("unchecked")
                                int random1 = new Random().nextInt(((List<String>) names2).size());
                              @SuppressWarnings("unchecked")
                                Player p1 = (Player) ((List<String>) names2).toArray()[random1];
                            
                                int x = AccessControl.plugin.getConfig().getInt("map1.cop.x");
                                int y = AccessControl.plugin.getConfig().getInt("map1.cop.y");
                                int z = AccessControl.plugin.getConfig().getInt("map1.cop.z");
                                p1.teleport(new Location(Bukkit.getWorld(AccessControl.plugin.getConfig().getString("map1.world")), x, y, z));
                          }
                        
                          Bukkit.broadcastMessage("Test complete!");
                       }
                   }, 1200); // 20 = 1 Second
    
    Can someone show me what I did wrong and how to correct myself? Thanks in advance.
     
  2. Offline

    finalblade1234

    "((List<String>) names2)" Don't cast Player to List<String>
     
  3. Offline

    ProSl3nderMan

    @finalblade1234
    If I take away List <String>, it says toArray is undefined for the type Player
     
  4. Offline

    mrgreen33gamer

    This code should return a random player from your desired list:

    Code:java
    1.  
    2. ArrayList<String> test = new ArrayList<String>();
    3.  
    4. public Player pickRandom(){
    5. Random randomM = new Random();
    6. String random = test.get(randomM.nextInt(test.size()));
    7. Player selected = Bukkit.getPlayer(random);
    8. return selected;
    9. }
    10.  
     
  5. Offline

    ProSl3nderMan

    Did I enter this right?:
    Code:
    ArrayList<String> test = new ArrayList<String>(AccessControl.plugin.getConfig().getStringList("map1.copjoinlist"));
         
        public Player pickRandom(){
            Random randomM = new Random();
            String random = test.get(randomM.nextInt(test.size()));
            Player selected = Bukkit.getPlayer(random);
            return selected;
        }
    
     
  6. Offline

    mrgreen33gamer

    @ProSl3nderMan

    I think you did it right there, if you didn't, try adding this somewhere before calling the pickRandom() method:

    Code:java
    1. test.addAll(AccessControl.plugin.getConfig().getStringList("map1.copjoinlist"));
     
  7. Offline

    sionzee

    What about
    Code:
    List<String> test = AccessControl.plugin.getConfig().getStringList("map1.copjoinlist");
    Yaml returning instance of List<String>

    And please, don't use 1200);// 20 = 1 Second
    But: 20L * 60); // It's more clear and all will know what it will do.
     
  8. Offline

    ProSl3nderMan

    @mrgreen33gamer
    Worked perfectly, thanks.
    @sionzee
    Hehe, sorry about that. Anyways, I haven't tested you thing!

    Solved, thanks ya'll.
     
Thread Status:
Not open for further replies.

Share This Page