onFinish Method-Need help

Discussion in 'Plugin Development' started by CrazymanJR, Sep 6, 2014.

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

    CrazymanJR

    I need help fixing my onFinish method when there is only 1 player on the team it should broadcast a msg and kick all players from the server, but for some odd reason, its not working. It would be great if somebody could help me out.

    Code:
    Code:java
    1. public void onFinish() {
    2. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    3. public void run() {
    4. if (GameState.isState(GameState.IN_GAME)) {
    5. if (AnimalHunt.getPlayers().getPlaying().size() == 1) {
    6. Player winner = (Player) AnimalHunt.getPlayers().getPlaying();
    7. ChatUtilities.Broadcast(ChatColor.YELLOW + "=======================");
    8. ChatUtilities.Broadcast(ChatColor.GREEN + "Game Over!");
    9. ChatUtilities.Broadcast(ChatColor.GOLD + winner.getName() + ChatColor.GREEN + "has won!");
    10. ChatUtilities.Broadcast(ChatColor.YELLOW + "=======================");
    11. for (Player players : Bukkit.getServer().getOnlinePlayers()) {
    12. players.kickPlayer(ChatColor.RED + "Game Is Restarting!");
    13. }
    14. }
    15. }
    16. }
    17. }, 0L, 100L);
    18. }
     
  2. Offline

    teej107

    Assuming you are calling the method:
    • Code:java
      1. if (GameState.isState(GameState.IN_GAME))
      returns false
    • OR......
    • Code:java
      1. if (AnimalHunt.getPlayers().getPlaying().size() == 1)
      returns false
    Also, this
    Code:java
    1. AnimalHunt.getPlayers().getPlaying().size() == 1 {
    2. Player winner = (Player) AnimalHunt.getPlayers().getPlaying();

    It appears that you are trying to cast a collection to a Player.
     
  3. Offline

    CrazymanJR

    teej107
    Umm, i don't get what you mean but i'm trying a new way to execute this. But this doesn't work either...
    Code:
    Code:java
    1. public void onFinish(final Player player) {
    2. if (GameState.isState(GameState.IN_GAME)) {
    3. if (AnimalHunt.getPlayers().getPlaying().size() == 1) {
    4. onWin(player);
    5. }
    6. }
    7.  
    8. }
    9.  
    10. private void onWin (Player player) {
    11. Player winner = (Player) AnimalHunt.getPlayers().getPlaying();
    12. ChatUtilities.Broadcast(ChatColor.YELLOW + "=======================");
    13. ChatUtilities.Broadcast(ChatColor.GREEN + "Game Over!");
    14. ChatUtilities.Broadcast(ChatColor.GOLD + winner.getName() + ChatColor.GREEN + "has won!");
    15. ChatUtilities.Broadcast(ChatColor.YELLOW + "=======================");
    16.  
    17. player.kickPlayer(ChatColor.RED + "Game Is Restarting!");
    18.  
    19. }
    20.  
     
  4. Offline

    teej107

    What does this return?
    Code:java
    1. AnimalHunt.getPlayers().getPlaying()
    I assume it returns a collection of some sort.
    Code:java
    1. Player winner = (Player) AnimalHunt.getPlayers().getPlaying()
    You are using the same method but trying to cast it to a Player. I'm surprised your console hasn't thrown any errors yet. Unless you just haven't checked?
     
  5. Offline

    CrazymanJR

    teej107
    The AnimalHunt.getPlayers()... gets how many players are in the players team
     
  6. Offline

    firecombat4

    What does "Not working" mean? Is there a stack trace or simply nothing happens or???. If there is an error, posting a stack trace can help us pin point your error.
     
  7. Offline

    coldandtired

    You'll need to show your whole class.

    From the first look it seems you're not using events correctly. In order to do what you want to do (listen to an event) you first have to create the event somewhere else. Using the entitydeathevent it looks like this in pseudocode
    Code:java
    1. Mob class
    2. {
    3. mobGotDamaged()
    4. {
    5. if (mob.hp <= 0)
    6. {
    7. //mob died
    8. deathEvent = new deathEvent(deadMob);
    9. raise deathEvent;
    10. }
    11. }
    12. }
    13.  
    14. YourListener
    15. {
    16. onDeath(DeathEvent event)
    17. {
    18. / Do stuff
    19. }
    20. }

    In other words you can't just listen for something happened without someone else (either the Bukkit team or you) making that event happen.
     
  8. Offline

    CrazymanJR

    firecombat4
    What i mean by "not working" is when nothing happens, no errors, nothing.
    coldandtired
    Wrong thread...this thread is about my onFinish method and has nothing to do with mobs and such
     
  9. Offline

    coldandtired

    CrazymanJR I know that, but you're trying to use events in the wrong way which leads me to believe you're not raising them anywhere.
     
  10. Offline

    CrazymanJR

    coldandtired
    Well, i don't know any other way to put these events, i tried twice and thats all i can think of.
     
  11. Offline

    coldandtired

    That's what I was saying. Before you can listen to an event it needs to be raised somewhere. You can listen to the EntityDeathEvent in your other case because the Bukkit team raises this event whenever a mob dies.

    Here's some of my old code you look at. This is a custom event. It gets raised whenever the Minecraft time changes the hour. You can see how it gets raised here (pm is the PluginManager class).
    Now, elsewhere in the plugin or in any other plugin I can listen to the HourChangeEvent like normal.

    In your case you would need to create a new event class called FinishEvent (or something) and make it similar to the first link. Then you would need to raise it at the appropriate time (whenever you decide the game has finished) and finally you can listen to it like with Bukkit events.
     
  12. Offline

    CrazymanJR

    coldandtired
    So i just need to put it in a new class, but how would it actually work?
     
  13. Offline

    coldandtired

    It works like I described above. First you create a new class that extends Event. This class contains the values relevant to your event (who, when, where, etc.).

    Then you create a new instance of this event and tell the PluginManager to call it at the right time.

    Lastly you listen for it in another part of your plugin.

    Post the rest of your code, though. It's likely you don't need to mess with events at all.
     
  14. Offline

    teej107

    I feel like you are over complicating things. As I have said, the reason that his code isn't executing is because his if statements aren't true or the fact that he is trying to cast a collection to a Player.
     
  15. Offline

    coldandtired

    teej107 Possibly, but I assumed the method is never getting called.
     
  16. Offline

    CrazymanJR

    Well, a friend helped me and i got everything resolved, thanks for all the support
     
  17. Offline

    teej107

    CrazymanJR Out of curiosity, what was the problem you solved to get it working?
     
Thread Status:
Not open for further replies.

Share This Page