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!!
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.
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)
tryy3 Ok thanks! tryy3 Is this right?? i mean i still cant get it to work. Idk why its not working tho. Code:java public class SignListener implements Listener{ public static ArrayList<List> worlds = new ArrayList<List>(); Random random = new Random(); int w = random.nextInt(); @EventHandler public void signchange(SignChangeEvent event) { if (event.getLine(0).equalsIgnoreCase("hg")) { final Sign s = (Sign) event.getBlock().getState(); event.getPlayer().sendMessage("HungerGames Sign Made!"); Bukkit.getServer() .getScheduler() .scheduleSyncRepeatingTask( Bukkit.getPluginManager().getPlugin("Hunger Games"), new Runnable() { public void run() { s.setLine(0, ChatColor.GREEN + "[JOIN]"); s.setLine(2, Bukkit.getWorld(s.getLine(3)).getPlayers() + "/30"); s.setLine(3, worlds.get(w) + ""); s.update(true); } }, 0L, 20L); } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.
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
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 if (worlds.length > 0){ Random random = new Random(); int w = random.nextInt(world.length);}
If that doesn't work, try this: PHP: World world = Bukkit.getWorlds()[new Random().nextInt(Bukkit.getWorlds().length)];
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.
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
tryy3 Yea i am doing this in the onEnable.. idk if it is right.. Code:java 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.
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())
tryy3 Its says this when i put that in.. The method length() is undefined for the type ArrayList<List>
tryy3 Idk why but it still says n must be positive and wont load the plugin.. This is my code. Code:java public class SignListener implements Listener{ @SuppressWarnings("rawtypes") public static ArrayList<List> worlds = new ArrayList<List>(); int w = Main.random.nextInt(SignListener.worlds.size()); @EventHandler public void signchange(SignChangeEvent event) { if (event.getLine(0).equalsIgnoreCase("hg")) { final Sign s = (Sign) event.getBlock().getState(); event.getPlayer().sendMessage("HungerGames Sign Made!"); Bukkit.getServer() .getScheduler() .scheduleSyncRepeatingTask( Bukkit.getPluginManager().getPlugin("Hunger Games"), new Runnable() { public void run() { s.setLine(0, ChatColor.GREEN + "[JOIN]"); //s.setLine(2, Bukkit.getWorld(s.getLine(3)).getPlayers() + "/30"); if(worlds.size() > 0){ s.setLine(3, worlds.get(w) + ""); } s.update(true); } }, 0L, 20L); } } }
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.
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?
remove line 6 completly on onEnable() add this lines Code:java Random random = new Random();SignListener.w = random.nextInt(SignListener.worlds.size()); on SignListener, add Code:java int w; after the variable worlds!
Wizehh I mean when you goto your plugin config you can set worlds and import them and teleport to them stuff like that.
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.
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?