Initializing Variables?

Discussion in 'Plugin Development' started by iClipse, Dec 24, 2016.

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


    So I have 2 variables here, and the last variable in both underlined lines are underline red for the reason "The local variable monster may not have been initialized"

    Im trying to make it find out if I killed a monster

    public void onEntityDeathEvent(EntityDeathEvent event){
    Entity entity = (Entity)entity;
    if (((EntityDeathEvent) entity).getEntity() instanceof Monster){
    Monster monster = (Monster)monster;
    if(monster.getKiller()instanceof Player){
    Player p = monster.getKiller();

    How do I initialize a variable?
    Last edited: Dec 25, 2016
  2. Offline


    If this is your full code, you're creating an Entity named entity that is entity casted as an Entity (Same thing, same with monster). I believe you're confusing it with event. You're then casting the event to that entity which you should really just use event.getEntity(). I don't think you need to cast the event to it. Try:
    2. @EventHandler
    3. public void onEntityDeathEvent(EntityDeathEvent event){
    4. [I]Entity entity = event.GetEntity();[/I]
    5. if (entity instanceof Monster){
    6. entity.getEntityId(); // you aren't declaring the id to an int (?) so why are you..?
    7. [I]Monster monster = (Monster) entity.getKiller(); [/I]
    8. if(monster instanceof Player){ // if you're checking if it's a player, why cast it as a monster? (See line below)
    9. Player p = monster; // removed the .getKiller. If monster is the killer of an entity, is it going to track the killer of the killer?

    Code above is assuming that you mixed up names, etc, as it's all you provided.
  3. Offline


    Oh uhhh

    No, im seeing if it was a monster. Thanks tho

    And then I have another wall of code, but thats working. I just want to make it check to see if I killed a monster so I can continue
  4. Offline


    Monster monster = (Monster)monster;
    You can't have monster declaring itself!
    Check if e.getEntity().getKiller() instanceof Monster
    Wispyy likes this.
  5. Offline


    No. Im checking if I killed a monster dangit
  6. Offline


  7. Offline


    but in your code, you have if(monster instanceof player)

    you arent really going by what Im trying to get. Still abit of help tho
  8. Offline


    Monster monster = monster

    How does that work?
    That's like saying

    Word word = word

    And then asking what the word is!

    Just do "if (e.getEntity().getKiller() instanceof Monster)"!
  9. Offline


    You initialize a variable by setting its value equal to something.

    Sent from my iPhone using Tapatalk
Thread Status:
Not open for further replies.

Share This Page