Hello bukkit, I have a few questions/problems I need help with, If you can answer any of them please help! ^.^ 1) I am trying to make a game that cycles through all the payers. So when the game starts 2 players fight, and then when 1 of the players dies it starts again with 2 other players, and keeps going through all the players until the arraylist is down to 1 player/element. 2) When my game starts, i have a horse spawned at a specific location. Sometimes however, instead of spawning just one, it spawns 2 or 3 horses and I have no idea why. Thank you! ^.^ EDIT: Question one was a little screwed up, fixed now.
Datdenkikniet here is the code that involves the horse spawning: Code: Random rand = new Random(); int r1; int r2; r1 = rand.nextInt(Main.alive.size()); Main.alive.remove(r1); r2 = rand.nextInt(Main.alive.size()); String r1n = Main.alive.get(r1); Main.alive.add(r1n); String r2n = Main.alive.get(r2); Player p1 = Bukkit.getServer().getPlayerExact(r1n); Player p2 = Bukkit.getServer().getPlayerExact(r2n); String p1n = p1.getName(); String p2n = p2.getName(); Main.ingame.add(p1n); Main.ingame.add(p2n); Horse horse1 = (Horse) p1.getWorld().spawnEntity(this.getHorse1Spawn(), EntityType.HORSE); Horse horse2 = (Horse) p2.getWorld().spawnEntity(this.getHorse2Spawn(), EntityType.HORSE); horse1.setAdult(); horse2.setAdult(); horse1.setColor(Color.BLACK); horse2.setColor(Color.WHITE); the methods getHorsespawn1 and 2 are getting a location from my config file for where the horses spawn
For number 1: Get all the players in the game into a HashMap with a key whose index starts from 0 to however many people are in your game, and value of player name. Create an empty HashSet into which you can add players' names after the round is over. Use random integers to grab the key of the HashMap. If that key's value isn't already in the HashSet, then add the name to the HashSet. Do this until the HashSet's size is equal to that of the HashMap.
That's actually a better option. Good thinking. I assume ArrayList has a better benchmark too so yes.
flaaghara thanks I was originally using an arraylist for it. the thing is though, I made the method game() which holds the picking of the players and I have it set so it is called when a player dies, but it doesn't work. How I get the player: Code:java Random rand = new Random(); int r1; int r2; r1 = rand.nextInt(Main.alive.size()); Main.alive.remove(r1); r2 = rand.nextInt(Main.alive.size()); String r1n = Main.alive.get(r1); Main.alive.add(r1n); String r2n = Main.alive.get(r2); Player p1 = Bukkit.getServer().getPlayerExact(r1n); Player p2 = Bukkit.getServer().getPlayerExact(r2n); The death listener: Code:java @EventHandler public void onPlayerDeath(PlayerDeathEvent e) { Entity entity = e.getEntity(); if (entity instanceof Player) { Player player = (Player) entity; String playern = player.getName(); if (Main.ingame.contains(playern)) { e.setDeathMessage(ChatColor.RED + "Player " + playern + " has been slain!"); Main.ingame.remove(player.getName()); plugin.game(); } } } the ArrayList is called ingame bump, help plz D: EDIT by Moderator: merged posts, please use the edit button instead of double posting.
IceBurgers No need to bump after OP has bumped himself, just watch the thread. ShadowLAX I'm confused. What's the current question?