get random String from List

Discussion in 'Plugin Development' started by guenniman, Jul 30, 2014.

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

    guenniman

    Hi guys,
    i want to get a random String from a StringList but it dont work.
    can anyone help me?

    and btw sry for my bad english.
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    3. Inventory inv=null;
    4.  
    5. if(command.getName().equalsIgnoreCase("test")){
    6. Player p = (Player) sender;
    7. File ZombieFile = new File("plugins/ProjectRPG/Lib/MobNames.yml");
    8. FileConfiguration ZombieConfig = YamlConfiguration.loadConfiguration(ZombieFile);
    9. List<String> namesArray = new ArrayList<String>();
    10. namesArray = ZombieConfig.getStringList("List.Zombies");
    11. Zombie z = (Zombie) p.getWorld().spawnEntity(p.getLocation(), EntityType.ZOMBIE);
    12. int random = new Random().nextInt(namesArray.size() -1);
    13. String ZombieName = namesArray.get(random);
    14. int randomInt = new Random().nextInt(100 -1);
    15. int ZombieLevel = randomInt;
    16.  
    17.  
    18. z.setCustomNameVisible(true);
    19. z.setTarget(p);
    20. z.setPassenger(p);
    21. z.getEquipment().setHelmet(new ItemStack(Material.LEATHER_HELMET));
    22. z.setCustomNameVisible(true);
    23.  
    24. z.setCustomName(ZombieName + " Lvl. " + ZombieLevel);
    25.  
    26.  
    27. return true;
    28. }
    29. return false;
    30. }
     
  2. Offline

    TeeePeee

    I'm sorry but your code was just too ugly looking not to format/fix for you.

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    3. if (command.getName().equalsIgnoreCase("test")) {
    4. if (!(sender instanceof Player)) {
    5. sender.sendMessage(ChatColor.RED + "You must be a player to use this command!");
    6. return true;
    7. }
    8. Random r = new Random();
    9.  
    10. Player p = (Player) sender;
    11. File zombieFile = new File(getDataFolder().getAbsolutePath() + File.separator + "Lib" + File.separator + "MobNames.yml");
    12. if (!zombieFile.exists()) {
    13. sender.sendMessage(ChatColor.RED + File.separator + "Lib" + File.separator + "MobNames.yml doesn't exist :(");
    14. return true;
    15. }
    16. FileConfiguration zombieConfig = YamlConfiguration.loadConfiguration(zombieFile);
    17. List<String> namesArray = zombieConfig.getStringList("List.Zombies");
    18.  
    19. if (namesArray.isEmpty()) {
    20. sender.sendMessage(ChatColor.RED + "There are no names in your name array :(");
    21. return true;
    22. }
    23.  
    24. Zombie z = (Zombie) p.getWorld().spawnEntity(p.getLocation(), EntityType.ZOMBIE);
    25. String zombieName = namesArray.get(r.nextInt(namesArray.size() - 1));
    26.  
    27. z.setCustomName(zombieName + " Lvl. " + r.nextInt(99)); // [name] Lvl. <0-98>
    28.  
    29. z.setTarget(p);
    30. z.setPassenger(p);
    31. z.getEquipment().setHelmet(new ItemStack(Material.LEATHER_HELMET));
    32. z.setCustomNameVisible(true);
    33.  
    34. return true;
    35. }
    36. return false;
    37. }
     
  3. Offline

    Esophose

    If it doesn't work try to do some debugging, if there is a stacktrace you should post that as well.


    Try setting zombieName with this.
    Code:
    Random random = new Random();
    String zombieName = namesArray.get(random.nextInt(namesArray.size())
     
  4. Offline

    guenniman

    Hi, I found a way to get a random String from the List.
    thx for your help guys :)

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    3. if (command.getName().equalsIgnoreCase("test")) {
    4. if (!(sender instanceof Player)) {
    5. sender.sendMessage(ChatColor.RED + "You must be a player to use this command!");
    6. return true;
    7. }
    8. Random r = new Random();
    9.  
    10. Player p = (Player) sender;
    11. File zombieFile = new File("plugins/ProjectRPG/Lib/MobNames.yml");
    12. if (!zombieFile.exists()) {
    13. sender.sendMessage(ChatColor.RED + File.separator + "Lib" + File.separator + "MobNames.yml doesn't exist :(");
    14. return true;
    15. }
    16. FileConfiguration zombieConfig = YamlConfiguration.loadConfiguration(zombieFile);
    17. List<String> namesArray = new ArrayList<>();
    18. for(String zName : zombieConfig.getStringList("Names.Zombies")){
    19. namesArray.add(zName);
    20. }
    21.  
    22. if (namesArray.isEmpty()) {
    23. sender.sendMessage(ChatColor.RED + "There are no names in your name array :(");
    24. return true;
    25. }
    26.  
    27. Zombie z = (Zombie) p.getWorld().spawnEntity(p.getLocation(), EntityType.ZOMBIE);
    28. //String zombieName = namesArray.get(r.nextInt(namesArray.size() - 1));
    29. String zombieName = namesArray.get(r.nextInt(namesArray.size()));
    30.  
    31. z.setCustomName(zombieName + " Lvl. " + r.nextInt(100)); // [name] Lvl. <0-99>
    32.  
    33. z.setTarget(p);
    34. z.setPassenger(p);
    35. z.getEquipment().setHelmet(new ItemStack(Material.LEATHER_HELMET));
    36. z.setCustomNameVisible(true);
    37.  
    38. return true;
    39. }
    40. return false;
    41. }
     
Thread Status:
Not open for further replies.

Share This Page