Getting Amount Of Players In Survival Mode - Spigot

  • Home
    • Recent Posts
    • Recent Activity
  • Forums
    • Search Forums
    • Recent Posts
  • Resources
    • Search Resources
    • Most Resources
    • Latest Reviews
  • Wiki
    • Wiki Index
    • Page List
    • Recent Activity
  • Team
    • Administrator
    • Moderator
    • Sponsor
    • Developer
    • Wiki Team
    • Services Moderator
    • Junior Moderator
    • Resources Moderator
  • Downloads
    • Spigot / BuildTools
    • BungeeCord
  • Discord
  • Hub
  • Issues
  • Members
    • Notable Members
    • Current Visitors
    • Recent Activity
    • New Profile Posts
  • Donate
Your username or email address: Password: Forgot your password? Stay logged in SpigotMC - High Performance Minecraft Software Home Forums Spigot Spigot Plugin Development Getting Amount of Players in Survival Mode

Discussion in 'Spigot Plugin Development' started by HexLazer205, Jun 19, 2015.

Thread Status: Not open for further replies. Page 1 of 2 1 2 Next >
  1. HexLazer205

    HexLazer205

    Hi, I'm trying to develop a PvP plugin where it teleports all the players to a PvP arena and when a player dies, it sets their game mode to creative so they can spectate the game. In doing so, I need to test for the amount of players (in a for loop, probably) that are in survival mode. I keep getting an error in eclipse that states "Can only iterate over an array or an instance of java.lang.Iterable". I need to test for the amount of players in survival mode, so I can see if there's just 1 player in survival mode, and if so, it ends the game and states that the player has won. Here is my code so far: Code (Text): import java.util.ArrayList; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.plugin.java.JavaPlugin; public class Manager extends JavaPlugin implements Listener { privatebooleanpvpIsEnabled = false; privatebooleanpvpIsStarting = false; privateinttaskid = 0; privateinttaskid2 = 1; @Override public void onEnable() { Bukkit.getPluginManager().registerEvents(this, this); } @Override public void onDisable() { } @EventHandler public void onBreak(BlockBreakEvent event) { if(pvpIsEnabled || pvpIsStarting) { event.setCancelled(true); } } @EventHandler public void onPlace(BlockPlaceEvent event) { if(pvpIsEnabled || pvpIsStarting) { event.setCancelled(true); } } @EventHandler public void onMove(PlayerMoveEvent event) { if(pvpIsStarting) { event.getPlayer().teleport(event.getPlayer().getLocation()); } } @EventHandler public void onDeath(PlayerDeathEvent event) { if(pvpIsEnabled) { Bukkit.broadcastMessage("§6§l" + event.getEntity().getName() + " §b§lwas slain by §6§l" + event.getEntity().getKiller().getName() + "§b§l!"); event.getEntity().setGameMode(GameMode.CREATIVE); } } @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { Player player = (Player) sender; if(cmd.getName().equalsIgnoreCase("pvp")) { if(pvpIsEnabled) { sender.sendMessage("§c§lA PvP game is already in progress."); returntrue; } for(Player players : Bukkit.getOnlinePlayers()) { players.teleport(new Location(Bukkit.getWorld("world"), 0, 4, 0)); players.setGameMode(GameMode.SURVIVAL); } pvpIsEnabled = true; pvpIsStarting = true; taskid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { int seconds = 10; @Override public void run() { if(seconds < 0) { Bukkit.broadcastMessage("§b§lThe PvP match will begin in " + seconds); seconds--; } else { Bukkit.getScheduler().cancelTask(taskid); Bukkit.broadcastMessage("§b§lThe PvP match has started! Good luck!"); pvpIsStarting = false; } } }, 0, 20); taskid2 = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { @Override public void run() { for(Player players : Bukkit.getOnlinePlayers()) { for(Player alive : players.getGameMode().SURVIVAL) { //more code } } } }, 0, 20); returntrue; } returnfalse; } } I have it so it checks for the amount of players in survival mode every second. Also, with the for loop I have above, Code (Text): for(Player alive : players.getGameMode().SURVIVAL) { eclipse tells me "the static field GameMode.SURVIVAL should be accessed in a static way".
    #1 HexLazer205, Jun 19, 2015
  2. Serializator

    Serializator Supporter

    Learn about for-loops please, but this should work: Code (Text): for(Player alive : Bukkit.getServer().getOnlinePlayers()) { if(alive.getGameMode().equals(GameMode.SURVIVAL)) { // Do something... } }
    #2 Serializator, Jun 19, 2015
  3. HexLazer205

    HexLazer205

    Hi, this will not work. I want to get the amount of players in survival mode, not checking if any of the players are in survival mode. Also, I already know about for loops, as you can see, so don't say I need to learn them. There's going to be at least 1 player in survival mode, which at that time, I need to end the game. If I just used your check, it would end the game immediately as all the players start in survival mode.
    #3 HexLazer205, Jun 19, 2015
  4. Serializator

    Serializator Supporter

    HexLazer205 said: ↑
    Hi, this will not work. I want to get the amount of players in survival mode, not checking if any of the players are in survival mode. Also, I already know about for loops, as you can see, so don't say I need to learn them. There's going to be at least 1 player in survival mode, which at that time, I need to end the game. If I just used your check, it would end the game immediately as all the players start in survival mode.Click to expand...
    I highly recommend you to learn Java properly and after that come back, because nobody is going to help you with such a simple question.
    #4 Serializator, Jun 19, 2015
  5. SubSide

    SubSide

    Dude, do you even Java? Code (Text): x = 0; for(player : onlineplayers) { if(player gamemode is survival) { x++; } } if (x > 0) stahp teh game
    #5 SubSide, Jun 19, 2015
  6. Serializator

    Serializator Supporter

    SubSide said: ↑
    Dude, do you even Java? Code (Text): x = 0; for(player : onlineplayers) { if(player gamemode is survival) { x++; } } if (x > 0) stahp teh game Click to expand...
    Dude, do you even spoon feed? x-) <3
    #6 Serializator, Jun 19, 2015
  7. HexLazer205

    HexLazer205

    I highly recommend you stop telling me to learn Java. This forum is for helping other developers with their code, not going around random posts and telling everyone to learn Java. I wrote all of my code by hand, take a look at it. Here's my question: How do I check for the amount of players that are in survival mode? Also, saying it is a simple question makes me feel bad about myself, and saying no one is going to help is also hurtful, so be nice and if you're going to post on people's topics in this forum, give people help. Also, I highly doubt you even read my code.
    #7 HexLazer205, Jun 19, 2015
  8. HexLazer205

    HexLazer205

    SubSide, I didn't think of that, *facepalm*. Thanks! I should be able to make X a variable to the whole class and decrement it when a player dies, right?
    #8 HexLazer205, Jun 19, 2015
  9. Serializator

    Serializator Supporter

    HexLazer205 said: ↑
    SubSide, I didn't think of that, *facepalm*. Thanks! I should be able to make X a variable to the whole class and decrement it when a player dies, right?Click to expand...
    Instead of doing the check in a runnable you can do it in the PlayerDeathEvent after setting the gamemode from the player who died to Creative.
    #9 Serializator, Jun 19, 2015
  10. SubSide

    SubSide

    Serializator said: ↑
    Dude, do you even spoon feed? x-) <3Click to expand...
    I used pseudo code, no spoonfeeding involved. Serializator said: ↑
    Instead of doing the check in a runnable you can do it in the PlayerDeathEvent after setting the gamemode from the player who died to Creative.Click to expand...
    It's not like he said that in the post you quoted... Anyway HexLazer205 said: ↑
    SubSide, I didn't think of that, *facepalm*. Thanks! I should be able to make X a variable to the whole class and decrement it when a player dies, right?Click to expand...
    The reason why we are saying that you should learn Java, is because a coder with a fair amount of experience dreams this loop. And how I did it should work just fine. People can still die, even in creative.
    #10 SubSide, Jun 19, 2015
  11. HexLazer205

    HexLazer205

    Ah, yes, good idea Serializator. That will make the code a lot more efficient. And SubSide, there's a difference between knowing Java and getting good at it. I know a fair amount of java, and I do definitely favor for loops. This is why I'm honing my skills into it to become a good developer. And you are correct, people can die in creative, so I can check if the player is in survival mode when he died.
    #11 HexLazer205, Jun 19, 2015
  12. SubSide

    SubSide

    HexLazer205 said: ↑
    Ah, yes, good idea Serializator. That will make the code a lot more efficient. And SubSide, there's a difference between knowing Java and getting good at it. I know a fair amount of java, and I do definitely favor for loops. This is why I'm honing my skills into it to become a good developer. And you are correct, people can die in creative, so I can check if the player is in survival mode when he died.Click to expand...
    You shouldn't care about people talking like that on the forums, I bet some of them have "Learn Java" on a macro. But most of us (including me) know just by the question asked, if someone is familiar with Spigot or Bukkit in general. No need to respond defensively, yes some of them can be demotivating, but we are doing this voluntary, so don't just expect it, like it's our duty.
    #12 SubSide, Jun 19, 2015
    • Funny Funny x 1
    • Friendly Friendly x 1
  13. Serializator

    Serializator Supporter

    SubSide said: ↑
    You shouldn't care about people talking like that on the forums, I bet some of them have "Learn Java" on a macro. But most of us (including me) know just by the question asked, if someone is familiar with Spigot or Bukkit in general. No need to respond defensively, yes some of them can be demotivating, but we are doing this voluntary, so don't just expect it, like it's our duty.Click to expand...
    Also, I didn't want to be rude or something, but I interpreted the situation as someone who was saying "Just give me the code and go on" if you know what I mean, not really the right thing to do.
    #13 Serializator, Jun 19, 2015
  14. HexLazer205

    HexLazer205

    Ok, well I was asking for help, not the code. After testing out my game with a friend, I've come across bugs I cannot fix. If I stop and start the server and do /pvp, it says an internal error occurred. However, if I do /reload, it works fine. It's like /reload is better than stopping and starting a server, which is weird. Also, I have it so if there is 1 or less players in the pvp game, it stops it and says there needs to be more players to play. However, if I start the game, and my friend leaves while the 10 second countdown is in progress, then it does not quit the game when it starts. I'm sure there are plenty more bugs I can't fix. I looked through my code over and over, and it seems everything is supposed to work fine, I don't even know why it gives an internal error when I do /pvp when I didn't do /reload. Here is my code: Code (Text): import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.java.JavaPlugin; public class Manager extends JavaPlugin implements Listener { Location arena = new Location(Bukkit.getWorld("world"), 0, 4, 0); Location home = new Location(Bukkit.getWorld("world"), -1196, 4, -1207); privatebooleanpvpIsEnabled = false; privatebooleanpvpIsStarting = false; privateinttaskid = 0; privateintamountAlive = 0; @Override public void onEnable() { Bukkit.getPluginManager().registerEvents(this, this); } @Override public void onDisable() { } @EventHandler public void onBreak(BlockBreakEvent event) { if(pvpIsEnabled || pvpIsStarting) { event.setCancelled(true); } } @EventHandler public void onPlace(BlockPlaceEvent event) { if(pvpIsEnabled || pvpIsStarting) { event.setCancelled(true); } } @EventHandler public void onMove(PlayerMoveEvent event) { if(pvpIsStarting) { event.getPlayer().teleport(event.getPlayer().getLocation()); } } @EventHandler public void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); if(pvpIsEnabled || pvpIsStarting) { event.getPlayer().sendMessage("§b§lThere is currently a PvP match in progress."); player.teleport(arena); } event.setJoinMessage("§8§l[§a+§8§l] §6" + player.getName() + " §ajoined the game."); } @EventHandler public void onQuit(PlayerQuitEvent event) { Player player = event.getPlayer(); if(pvpIsEnabled || pvpIsStarting) { amountAlive--; } event.setQuitMessage("§8§l[§c-§8§l] §6" + player.getName() + " §aleft the game."); } @EventHandler public void onDeath(PlayerDeathEvent event) { Player player = event.getEntity(); if(pvpIsEnabled) { Bukkit.broadcastMessage("§6§l" + player.getName() + " §b§lwas slain by §6§l" + player.getKiller().getName() + "§b§l!"); event.getEntity().setGameMode(GameMode.CREATIVE); amountAlive--; if(amountAlive == 1) { Bukkit.broadcastMessage("§6§l" + player.getName() + " §b§lwon the game! Congratulations!"); pvpIsEnabled = false; for(Player players : Bukkit.getOnlinePlayers()) { players.teleport(home); } } } } @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if(cmd.getName().equalsIgnoreCase("pvp")) { if(pvpIsEnabled) { sender.sendMessage("§c§lA PvP game is already in progress."); returntrue; } pvpIsEnabled = true; pvpIsStarting = true; for(Player players : Bukkit.getOnlinePlayers()) { players.teleport(arena); players.setGameMode(GameMode.SURVIVAL); amountAlive++; } taskid = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { int seconds = 10; @Override public void run() { if(!(seconds < 1)) { Bukkit.broadcastMessage("§b§lThe PvP match will begin in " + seconds); seconds--; } else { Bukkit.getScheduler().cancelTask(taskid); Bukkit.broadcastMessage("§b§lThe PvP match has started! Good luck!"); pvpIsStarting = false; if(amountAlive <= 1) { Bukkit.broadcastMessage("§b§lThere must be at least 2 players in order to play."); pvpIsEnabled = false; amountAlive = 0; for(Player players : Bukkit.getOnlinePlayers()) { players.teleport(home); players.setGameMode(GameMode.CREATIVE); } } } } }, 0, 20); returntrue; } returnfalse; } } And here is the stack trace: Code (Text): [17:55:50 ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'pvp' in plugin Plugin205 v1.0 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642) ~[spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1135) [spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:970) [spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_45] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_45] at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45] Caused by: java.lang.NullPointerException at org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer.teleport(CraftPlayer.java:464) ~[spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity.teleport(CraftEntity.java:223) ~[spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] at me.edragon205.plugin.Manager.onCommand(Manager.java:107) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.6.jar:git-Spigot-044d928-e8c6403] ... 15 more
    #14 HexLazer205, Jun 19, 2015
  15. bo0tzz

    bo0tzz

    HexLazer205 said: ↑
    if I start the game, and my friend leaves while the 10 second countdown is in progress, then it does not quit the game when it starts.Click to expand...
    When a player quits the server (event), check if the countdown is running and if so check the amount of players, if insufficient then stop the game etc. Unfortunately I don't know about the reloading issue.
    #15 bo0tzz, Jun 19, 2015
  16. SubSide

    SubSide

    Try creating the arena and home variables in onEnable If that's the issue and fixes it, I'll tell you why. I don't know 100% if that's the problem. (I'm not going to explain something that afterwards doesn't seem to be true ;P)
    #16 SubSide, Jun 19, 2015
  17. HexLazer205

    HexLazer205

    Thanks bo0tzz, that fixed my problem. And SubSide, that doesn't work. It will give me errors saying unknown variable "arena" and "home" when i use them, and in-game it just pops in an internal error.
    #17 HexLazer205, Jun 19, 2015
  18. SubSide

    SubSide

    You know what I mean with initializing them in onEnable right?
    #18 SubSide, Jun 19, 2015
  19. HexLazer205

    HexLazer205

    Ah, so I would have them created in onEnable and outside onEnable is what you're saying, right? Code (Text): Location arena = new Location(Bukkit.getWorld("world"), 0, 4, 0); Location home = new Location(Bukkit.getWorld("world"), -1196, 4, -1207); privatebooleanpvpIsEnabled = false; privatebooleanpvpIsStarting = false; privateinttaskid = 0; privateintamountAlive = 0; @Override public void onEnable() { Bukkit.getPluginManager().registerEvents(this, this); arena = new Location(Bukkit.getWorld("world"), 0, 4, 0); home = new Location(Bukkit.getWorld("world"), -1196, 4, -1207); }
    #19 HexLazer205, Jun 19, 2015
  20. SubSide

    SubSide

    Only inside.
    #20 SubSide, Jun 19, 2015
Show Ignored Content Page 1 of 2 1 2 Next > Thread Status: Not open for further replies. Your username or email address: Do you already have an account?
  • No, create an account now.
  • Yes, my password is:
  • Forgot your password?
Stay logged in SpigotMC - High Performance Minecraft Software Home Forums Spigot Spigot Plugin Development SpigotMC - High Performance Minecraft Software Home Forums Spigot Spigot Plugin Development

Tag » How To Put A Player In Survival Mode