Urgent Player Join Detector With Player Death Detector

Discussion in 'Plugin Development' started by polo2673, Jan 25, 2013.

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

    polo2673

    I just wanted to know how would I combine a player join detector with a player death detector? heres the code im using
    Code:
    package me.polo2673.starwars;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    public class MyPlayerListener implements Listener{
    public static Starwars plugin;
    public int test;
    public int playercounter = 0;
    //Player Join Detector!
    //@EventHandler
    //public void onPlayerJoin(PlayerJoinEvent event){
    // Player player = event.getPlayer();
    // event.setJoinMessage(ChatColor.AQUA + "[Test]Hello " + player.getName() + " And Welcome To " + player.getWorld().getName() + " It Is " + player.getWorld().getTime());
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event){
    Player player = event.getPlayer();
    playercounter++;
    event.setJoinMessage(ChatColor.AQUA + player.getName() + " Has joined the game.There are now " + playercounter + " players online!");
    }
    public void onDeath(PlayerDeathEvent event){
    Player player = event.getEntity();
    playercounter--;
    event.setDeathMessage(ChatColor.RED + player.getName() + " Has died.Now " + playercounter + " players remain!");
     
    }
    }
    
    and all that does is it does the player join detector but not the death detector :O
     
  2. Offline

    DjTamo

    Try: onPlayerDeath(PlayerDeathEvent event) instead of onDeath(PlayerDeathEvent event) then change Player player = event.getEntity(); to Player player = event.getPlayer();
     
  3. Offline

    Major_Derp

    The onPlayrDeath changing OnDeath wouldn't really do anything, you could name that whatever, but getting the player instead of the entity might work.
     
  4. Offline

    DjTamo

    Derp! I thought he had "onDeath(EntityDeathEvent event)" but yes he needs to get the player not the entity. polo2673 Are you using eclipse? because it should have indicated this.
     
  5. Offline

    evilmidget38

    DjTamo Not trying to be a jerk or anything, but don't try to give advice on something you don't know. You can call the method for an EventHandler whatever you want, it's simply a standard practice back from when you couldn't to call it onPlayerDeath. Additionally, PlayerDeathEvent#getPlayer() doesn't exist. The OP is correct in using getEntity() as PlayerDeathEvent is an EntityEvent rather than a PlayerEvent.

    polo2673 Add @EventHandler above/before your onDeath method.
     
  6. Offline

    DjTamo

    Yes I realized that after. As I posted above I thought he had EntityDeathEvent not PlayerDeathEvent. And I looked back into my code from one of my plugins with the death event and saw that getEntity() is correct so sorry for giving that wrong info.
     
  7. Offline

    polo2673

    Thanks guys I'm new to this and the bukkit community is great
     
  8. Offline

    Major_Derp

    Didnt catch that event handler thing, Nice. And i was unsure of the getPlayer() thing, but thanks for the correction.
     
  9. Offline

    gomeow

    event.getPlayer() was wrong either way
     
  10. Offline

    RealDope

    Don't see why this was classified as "urgent".
     
Thread Status:
Not open for further replies.

Share This Page