Events - How to get players in a not Player Event?

Discussion in 'Plugin Development' started by Smex, Oct 6, 2011.

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

    Smex

    Im trying to extend my plugin LastChance, the problem is, as I dont know
    how to get a player on a EntityDeath Event, I just used a Player_Move Event.
    Well thats not the goal, it should be able to check if the Entity is a Player and than
    I should be able to make a f**** Player player variable, but how as the
    EntityDeath Event does not get players/getPlayer()?

    This is what I have so far:


    Code:
            this.getServer().getPluginManager().registerEvent(Event.Type.ENTITY_DEATH, new PlayerListener(){
                public void onEntityDeath(EntityDeathEvent event){
                    Entity creat = event.getEntity();
                    if(creat instanceof Player){
     -------> How the **** do I get the player here? <------
                    if(player.hasPermission("lastchance.use")){
                    Random ran = new Random();
                    int num;
                    double a = player.getHealth();
                    if(a<1){
                        num = ran.nextInt(w);
                        if(num==1){
                            player.setHealth(1);
                            return;
                        }
    
                        }
                    }return;
                    }
                }
            },Event.Priority.Normal, this);
     
  2. Offline

    ZerothAngel

    @Smex If "creat instanceof Player" is true, you can safely cast it to a Player. Insert this after your test:
    Code:
    Player player = (Player)creat;
    
     
    Smex likes this.
  3. Offline

    Smex

    Thank you very much, you should be my mentor. :p
    So thing you wrote is like a convert from something into another thing?
    Or how do I have to understand this?
     
  4. Offline

    ZNickq

    You convert from a commandSender (which isn't a player,it doesn't have a Location,etc.) to a Player,which as a location,and a body,and can be teleported,etc.
     
  5. Offline

    thehutch


    Casting is basically convert one object to other so you can convert a commandsender into a player
     
  6. Offline

    SparrowMaxx

    Look up casting for the techincal definition, but basically you can use (Object you want it to be)Object to cast it, ex. if you have an entity B and want it to be called a player X,
    Player X = (Player) B;
    Note that if you try to cast something and they aren't convertable to each other, you're going to get an error.
     
  7. Offline

    Celeixen

    Well casting takes the required information from another object, e.g.
    A player needs:
    An inventory, a location, health, name

    If your entity has those then it can be cast to a player. But you cant cast a creeper because it doesnt have an inventory.
     
Thread Status:
Not open for further replies.

Share This Page