Hunger Games Victory

Discussion in 'Plugin Development' started by djmaster329, Aug 22, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    djmaster329

    Hello,

    I am working on a Hunger Games plugin, it is almost finished.
    The last thing I have to do is add the winning-detection, as I like to call it.
    I have tried a few things but I have no idea how to actually make it work.

    Code:
    @EventHandler
        public void onEntityDeath(EntityDeathEvent event) {
          Entity ent = event.getEntity();
          if ((ent instanceof Player)) {
            if(playeramount == 2){ 
                Player player = (Player)event.getEntity();
                World pWorld = Bukkit.getWorld("world");
                pWorld.strikeLightningEffect(player.getLocation());
                //playeramount = playeramount +1;
                player.kickPlayer("You died :( - Players left: " + playeramount);
                String winningplayer = Bukkit.getOnlinePlayers().toString();
                Bukkit.getPlayer(winningplayer).kickPlayer("Congratulations, " + winningplayer + "! You win this round!");
                WriteWindb(winningplayer.toLowerCase());
            }else{
                Player player = (Player)event.getEntity();
                String playername = player.getName();
                World pWorld = Bukkit.getWorld("world");
                pWorld.strikeLightningEffect(player.getLocation());
                //playeramount = playeramount +1;
                player.kickPlayer("You died :( - Players left: " + playeramount);
                Bukkit.broadcastMessage(playername.toString() + " died. Players left: " + playeramount);
                WriteLosedb(playername.toLowerCase());
            }
          }
        }
    playeramount is a public static int.

    I hope you guys can help me :)

    Kind Regards,

    djmaster329
     
  2. Offline

    andf54

    That depends on what you want the plugin to do.

    I would maintain a "HashSet<String> contesters" with player names who are in the game. If a player dies then do "contesters.remove(player.getName())". The game ends if contesters.size() == 1.
    You need to figure out how "contesters" is initialised. One possibility is a /begin command that adds all online players to "contesters".
     
  3. Offline

    djmaster329

    Okay, and that means I can remove the playeramount int? And change it to add the playername to the list instead of adding +1 to the playeramount, right?
     
  4. Offline

    andf54

    That playeramount is a bad solution. What if someone logs off (normal or client crash) or if someone logs in during the game. Remove it.

    You need to figure out what the plugin should do in all those special cases.
     
  5. Offline

    djmaster329

    Well, the playeramount gets updated every time someone logs in or disconnects, and I have a public static boolean that gets changed to true when the game starts. When someone logs in, it'll check if the boolean is true/false and then kick the player if needed.
     
  6. Offline

    andf54

    The problem with that is when someone lags off. Also, if you decide to go with playeramount then you could just get all players that are online and count how many of them are online: getPlayers.length().

    Here is how I would do it:
    When a command /start is given then all players online get put to the contesters HashSet<String> list.
    When someone leaves the game he remains in the list.
    When someone logs in then he gets kicked if he is not on the list. Players on the list can still log in.
    When someone is killed he gets removed from the list and then kicked.
    Game is over when a single player remains on the server.
    I would also make a countdown when a player logs off. If the countdown for the player reaches 0, and he doesn't log in, he gets removed from the list. This way when players lag off, they can still log in, but if if they take too long, they are disqualified.
     
  7. Offline

    djmaster329

    Hi again,

    I tried the code you suggested, but I don't really like the way it works. So I changed it back to the original values.
    Code:
    @EventHandler
        public void onEntityDeath(EntityDeathEvent event) {
          Entity ent = event.getEntity();
          if ((ent instanceof Player)) {
            if(players == 2){
                Player player = (Player)event.getEntity();
                World pWorld = Bukkit.getWorld("world");
                pWorld.strikeLightningEffect(player.getLocation());
                String playername = player.getName().toLowerCase();
                WriteLosedb(playername);
                player.kickPlayer("You died :( - Players left: 1");
                String winningplayer = Bukkit.getServer().getOnlinePlayers().toString();
                Bukkit.getPlayer(winningplayer).kickPlayer("Congratulations, " + winningplayer + "! You win this round!");
                WriteWindb(winningplayer.toLowerCase());
                Bukkit.shutdown();
            }else{
                Player player = (Player)event.getEntity();
                String playername = player.getName();
                World pWorld = Bukkit.getWorld("world");
                pWorld.strikeLightningEffect(player.getLocation());
                player.kickPlayer("You died :( - Players left: " + players);
                Bukkit.broadcastMessage(playername.toString() + " died. Players left: " + players);
                WriteLosedb(playername.toLowerCase());
            }
          }
        }
    But when the code above gets executed, this error shows up:
    Code:
    12:27:48 [INFO] djmaster329 left the game. Players left: 1
    12:27:48 [SEVERE] Could not pass event PlayerDeathEvent to HungerGames
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:332)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
            at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:322)
            at net.minecraft.server.EntityPlayer.die(EntityPlayer.java:307)
            at net.minecraft.server.EntityLiving.damageEntity(EntityLiving.java:663)
     
            at net.minecraft.server.EntityHuman.damageEntity(EntityHuman.java:595)
            at net.minecraft.server.EntityPlayer.damageEntity(EntityPlayer.java:349)
     
            at net.minecraft.server.EntityHuman.attack(EntityHuman.java:762)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:1020)
            at net.minecraft.server.Packet7UseEntity.handle(SourceFile:36)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:281)
            at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
            at net.minecraft.server.ServerConnection.b(SourceFile:35)
            at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:583)
            at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
            at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:476)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
            at me.djmaster329.HungerGames.HungerGames.onEntityDeath(HungerGames.java:608)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:330)
            ... 20 more
     
Thread Status:
Not open for further replies.

Share This Page