Recently I've been working on a mini-game plugin for someone and I had to get random locations and if possible make each player spawn at a different location. I came up with this method, basically you pass in 2 lists, the first list contains all the possible java objects and the second list contains all the used java objects. Code:java public static <T> T getUnusedObject(List<T> objects, List<T> usedObjects){Random random = new Random(); // If all objects have been used, pick a random oneif (usedObjects.containsAll(objects)){return objects.get(random.nextInt(objects.size()));} // Keep running this code until we find an object we can useT randomObject = objects.get(random.nextInt(objects.size()));while (usedObjects.contains(randomObject)){randomObject = objects.get(random.nextInt(objects.size()));} return randomObject;} Again this works for any type of java object (Strings, Integers, Locations, ItemStacks etc) Example Usage: The below example usage would work for a mini-game type plugin. Code:java List<Location> usedLocations = new ArrayList<Location>();for (Player player : Bukkit.getOnlinePlayers()){ if (getPlayers().contains(player)){ Location spawn = Utils.getUnusedObject(getSpawnLocations(), usedSpawns); usedLocations.add(spawn); player.teleport(spawn, TeleportCause.PLUGIN);}} Enjoy Keir (Also first post in ages, I hope to become more active here again)
The way I used was more convenient for what I was using it for. However that would be a better option in most other cases.
Maybe an example would go good with this thread? I'm in the process of making 3 mini-game plugins, and I want to see if this would be a good thing to use. Thanks