Hello,
I'm tyring to code a minecraft server plugin and want to spawn a player randomly on pre defined locations writen in a config file. So far so good, because it works when a player wants to join the mini game I made by simply right clicking on a special sign. When there is no spawn defined in the config file yet, the console shows the error: bound must be positive. Which is right because there is no number (spawn) saved in the config. But when a players dies I use the same method but the console says: bound must be postive. Which it is! There is a spawn defined in the config and it says 1, counting up.
Now to the code:
public Location getRandomSpawn() { if (!arenas.contains("Arenas." + getName() + ".Spawns.Counter")) { return null; } else { Random rand = new Random(); int other = this.arenas.getInt("Arenas." + getName() + ".Spawns.Counter") - 1; int num = rand.nextInt(other) + 1; Location loc = getSpawn(num); return loc; } }
I use this to get the x,y,z and direktion of looking with this:
public Location getSpawn(int id) { if (!arenas.contains("Arenas." + getName() + ".Spawns.Counter")) { return null; } else { Location loc = new Location(Bukkit.getWorld(this.arenas.getString("Arenas." + getName() + ".Spawns." + id + ".World")), this.arenas.getDouble("Arenas." + getName() + ".Spawns." + id + ".X"), this.arenas.getDouble("Arenas." + getName() + ".Spawns." + id + ".Y"), this.arenas.getDouble("Arenas." + getName() + ".Spawns." + id + ".Z")); loc.setPitch((float)this.arenas.getDouble("Arenas." + getName() + ".Spawns." + id + ".Pitch")); loc.setYaw((float)this.arenas.getDouble("Arenas." + getName() + ".Spawns." + id + ".Yaw")); return loc; } }
I call getRandomSpawn() in all cases. And yes I know that if there is no spawn wrtitten in the config it just returns null, i have to change that in the future but for now I want to know what's going on....
Now the code where I use getRandomSpawn().
SIGN:
@EventHandler public void onSignInteract(PlayerInteractEvent e) { Player player = e.getPlayer(); if ((e.getAction() == Action.RIGHT_CLICK_BLOCK) && e.hasBlock() && e.getClickedBlock().getState() instanceof Sign) { Sign sign = (Sign)e.getClickedBlock().getState(); if (sign.getLine(0).equalsIgnoreCase("[" + ChatColor.GOLD + "CrackShot" + ChatColor.BLACK + "]")) { sign.update(); for (Iterator<?> iterator = Arenas.getArenas().iterator(); iterator.hasNext(); ) { Arena arena = (Arena)iterator.next(); if (sign.getLine(1).equalsIgnoreCase(ChatColor.BOLD + arena.getName())) { if (!arena.hasPlayer(player)) { if (Methods.getLobby() != null) { if (arena.getPlayers().size() < arena.getMaxPlayers()) { if (!arena.isOn()) { arena.addPlayer(player); Arenas.addArena(player, arena); player.teleport(arena.getRandomSpawn()); Main.sendMessage(player, "You joined the Arena: " + ChatColor.GOLD + arena.getName()); arena.sendAll(ChatColor.GRAY + "[" + ChatColor.GOLD + "CrackShot" + ChatColor.GRAY + "] " + ChatColor.GOLD + player.getName().toString() + ChatColor.GRAY + " has joined the Arena!"); arena.updateSigns(); Main.setScoreboard(player); player.setGameMode(GameMode.SURVIVAL); continue; } Main.sendMessage(player, "Arena is disabled!"); continue; } Main.sendMessage(player, "Arena is full!"); continue; } Main.sendMessage(player, "No spawn set!"); continue; } Main.sendMessage(player, "You are already in the Arena!"); } } } } }
Please ignore all the rest because important is only "player.teleport(arena.getRandomSpawn());"
DEATH:
@EventHandler public void playerRespawnTeleport(PlayerRespawnEvent e) { Player player = e.getPlayer(); if(Arenas.isInArena(player)){ e.setRespawnLocation(getRandomSpawn()); } }
My impression of the situation is that "teleport()" and "setRespawnLoaction()" maybe work differently? But I just can be stupid and don't see the mistake.