List of worlds?

Discussion in 'Plugin Development' started by Ty Cleere, May 10, 2014.

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

    Ty Cleere

    Hey!

    Is there a way to go through a list of worlds? like i want my code to pick a random world out of a list and use that for the match? Could you guys guide me on this? Havent done something like this before lol

    Thanks!!
     
  2. Offline

    xTigerRebornx

  3. Offline

    Ty Cleere

    xTigerRebornx Well, im more looking for like a place that my code goes through all of the custom worlds i have loaded in the plugin. Like if i have the worlds 1, 2, 3, 4, and 5 i want my code to pick a random world out of these worlds and use it.
     
  4. Offline

    tryy3

    Save the custom made worlds in a list, or use getWorlds() and save it into a list
    then use the Random java class, and get a random number between 0 and the world list's length, then use list.get(random)
     
  5. Offline

    Ty Cleere

    tryy3 Ok thanks!

    tryy3

    Is this right?? i mean i still cant get it to work. Idk why its not working tho.

    Code:java
    1. public class SignListener implements Listener{
    2.  
    3. public static ArrayList<List> worlds = new ArrayList<List>();
    4. Random random = new Random();
    5. int w = random.nextInt();
    6.  
    7.  
    8.  
    9. @EventHandler
    10. public void signchange(SignChangeEvent event) {
    11.  
    12. if (event.getLine(0).equalsIgnoreCase("hg")) {
    13. final Sign s = (Sign) event.getBlock().getState();
    14.  
    15.  
    16. event.getPlayer().sendMessage("HungerGames Sign Made!");
    17. Bukkit.getServer()
    18. .getScheduler()
    19. .scheduleSyncRepeatingTask(
    20. Bukkit.getPluginManager().getPlugin("Hunger Games"),
    21. new Runnable() {
    22.  
    23. public void run() {
    24. s.setLine(0, ChatColor.GREEN + "[JOIN]");
    25. s.setLine(2, Bukkit.getWorld(s.getLine(3)).getPlayers() + "/30");
    26. s.setLine(3, worlds.get(w) + "");
    27. s.update(true);
    28. }
    29. }, 0L, 20L);
    30. }
    31. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  6. Offline

    tryy3

    Code:java
    1. int w = random.nextInt(worlds.length());


    that should do it
     
  7. Offline

    Ty Cleere

    tryy3 I changed it to that and i got this error in the code.. I tried this earlier and didnt know how to fix it.

    Code:
    3:16 ERROR]: Could not load 'plugins\HungerGames.jar' in folder 'plugins'
    ukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: n must be positive
      at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[craftbukk
    r:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[craftbuk
    ar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [craftbuk
    ar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at org.bukkit.craftbukkit.v1_7_R2.CraftServer.loadPlugins(CraftServer.java:350) [craftbukkit
    git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at org.bukkit.craftbukkit.v1_7_R2.CraftServer.reload(CraftServer.java:784) [craftbukkit.jar:
    ukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at org.bukkit.Bukkit.reload(Bukkit.java:288) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f87
    3042jnks]
      at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [craftbukkit.jar
    Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) [craftbukkit.jar:
    ukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at org.bukkit.craftbukkit.v1_7_R2.CraftServer.dispatchCommand(CraftServer.java:696) [craftbu
    jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at org.bukkit.craftbukkit.v1_7_R2.CraftServer.dispatchServerCommand(CraftServer.java:683) [c
    ukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at net.minecraft.server.v1_7_R2.DedicatedServer.ax(DedicatedServer.java:286) [craftbukkit.ja
    -Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at net.minecraft.server.v1_7_R2.DedicatedServer.v(DedicatedServer.java:251) [craftbukkit.jar
    Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at net.minecraft.server.v1_7_R2.MinecraftServer.u(MinecraftServer.java:548) [craftbukkit.jar
    Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:459) [craftbukkit.j
    t-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [craftbukkit.jar
    Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
    d by: java.lang.IllegalArgumentException: n must be positive
      at java.util.Random.nextInt(Unknown Source) ~[?:1.7.0_45]
      at me.SimpleAce.hungergames.SignListener.<init>(SignListener.java:19) ~[?:?]
      at me.SimpleAce.hungergames.Main.<init>(Main.java:26) ~[?:?]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0_45]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.7.0_45]
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.7.0_45]
      at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.7.0_45]
      at java.lang.Class.newInstance(Unknown Source) ~[?:1.7.0_45]
      at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit.
    it-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:127) ~[craftbukk
    r:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
      ... 14 more
     
  8. Offline

    tryy3

    Because the variable worlds doesn't contain anything, so it will return 0.
    Add stuff to the variable worlds.
    You can also do
    Code:java
    1. if (worlds.length > 0)
    2. {
    3. Random random = new Random();
    4. int w = random.nextInt(world.length);
    5. }
     
  9. Offline

    Ty Cleere

    tryy3 Ok i will try this!
     
  10. Offline

    Wizehh

    If that doesn't work, try this:
    PHP:
    World world Bukkit.getWorlds()[new Random().nextInt(Bukkit.getWorlds().length)];
     
    Ty Cleere likes this.
  11. Offline

    Ty Cleere

    Wizehh Yea that other one didnt work. and ill try this!

    Wizehh Yea it doesnt like that either... Im trying to use this in a sign and in some other spots but im trying to update the sign so that it gets a random world in the config and uses it in the third line in my sign.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  12. Offline

    tryy3

    Ty Cleere
    Are you even adding anything to the worlds ArrayList before you use the Random class?
    If not then, basically you are telling the Random class to get a random number from 0, aka nothing
     
  13. Offline

    Ty Cleere

    tryy3 Yea i am doing this in the onEnable.. idk if it is right..

    Code:java
    1. SignListener.worlds.add(Bukkit.getWorlds());


    but it says n must be positive when i reload the plugin. so it wont load the plugin at all.
     
  14. Offline

    tryy3

    remove the Random variable from your class SignListener, change the variable w to
    int w;
    on onEnable, after you added the worlds, add the Random variable, and then under set the variable w in SignListener to random.nextInt(SignListener.worlds.length())
     
  15. Offline

    Ty Cleere

    tryy3

    Its says this when i put that in..

    The method length() is undefined for the type ArrayList<List>
     
  16. Offline

    tryy3

    Sorry its, size() not length()
     
  17. Offline

    Ty Cleere

    tryy3 Idk why but it still says n must be positive and wont load the plugin..

    This is my code.

    Code:java
    1. public class SignListener implements Listener{
    2.  
    3. @SuppressWarnings("rawtypes")
    4. public static ArrayList<List> worlds = new ArrayList<List>();
    5.  
    6. int w = Main.random.nextInt(SignListener.worlds.size());
    7.  
    8.  
    9.  
    10. @EventHandler
    11. public void signchange(SignChangeEvent event) {
    12.  
    13. if (event.getLine(0).equalsIgnoreCase("hg")) {
    14. final Sign s = (Sign) event.getBlock().getState();
    15.  
    16.  
    17. event.getPlayer().sendMessage("HungerGames Sign Made!");
    18. Bukkit.getServer()
    19. .getScheduler()
    20. .scheduleSyncRepeatingTask(
    21. Bukkit.getPluginManager().getPlugin("Hunger Games"),
    22. new Runnable() {
    23.  
    24. public void run() {
    25. s.setLine(0, ChatColor.GREEN + "[JOIN]");
    26. //s.setLine(2, Bukkit.getWorld(s.getLine(3)).getPlayers() + "/30");
    27. if(worlds.size() > 0){
    28. s.setLine(3, worlds.get(w) + ""); }
    29. s.update(true);
    30. }
    31. }, 0L, 20L);
    32. }
    33. }
    34.  
    35. }
    36.  
     
  18. Offline

    Wizehh

    Use the code I provided.
     
  19. Offline

    Ty Cleere

    Wizehh It didnt work. im trying to set this in a sign..
     
  20. Offline

    Wizehh

    sign.setLine world.getName()

    PHP:
    ArrayList<List>
    What. Did you just copy & paste that from somewhere? Because that means the ArrayList can only 'hold' type List, not World.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  21. Offline

    Ty Cleere

    Wizehh But im trying to get a random world out of my config of preset worlds and use it in my sign. So how would that work?
     
  22. Offline

    tryy3

    remove line 6 completly

    on onEnable()
    add this lines
    Code:java
    1. Random random = new Random();
    2. SignListener.w = random.nextInt(SignListener.worlds.size());


    on SignListener, add
    Code:java
    1. int w;

    after the variable worlds!
     
  23. Offline

    Wizehh

    By config, do you mean FileConfiguration? If so, that's completely different.
     
  24. Offline

    Ty Cleere

    Wizehh I mean when you goto your plugin config you can set worlds and import them and teleport to them stuff like that.
     
  25. Offline

    Wizehh

    Ty Cleere
    (can't edit posts - 'invisible')
    Again, why are you using an ArrayList<List>? You're going to want to use an ArrayList<World>.

    huh? Can you show us your configuration file then?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  26. Offline

    Ty Cleere

    Wizehh Idk why i didnt set it to Worlds. and i havent set that up i was just trying to get this random to work and then use the config after i get it working. But this still with working tryy3... It loaded the plugin but gave me an error and said n must be positive. so im pron not puttin in the worlds right?
     
Thread Status:
Not open for further replies.

Share This Page