[URGENT] Mobs

Discussion in 'Plugin Development' started by CactusComboPvP, May 27, 2014.

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

    CactusComboPvP

    @EventHandler
    public void onZombie(EntityTargetLivingEntityEvent e) {
    Player p = (Player) e.getEntity();
    if ((KitNames.pkit.containsKey(p)) && (KitNames.pkit.get(p) == "hades")) {
    e.setCancelled(true);
    }
    }

    So basically this won't work.
    Like I am in that key 'hades' because I can do my functions but mobs still attack me.
    Help please.
     
  2. Offline

    metalhedd

    you cannot use == to compare strings. you must use .equals()
     
    thomasb454 likes this.
  3. Offline

    CactusComboPvP

    metalhedd

    That works on my overall plugins.

    Like say for example

    Code:
    @EventHandler
    public void onInteract(PlayerInteractEvent e)
    {
      final Player p = e.getPlayer();
      Action a = e.getAction();
     
      if ((a == Action.RIGHT_CLICK_BLOCK) || (a == Action.RIGHT_CLICK_AIR)) {
        if ((KitNames.pkit.containsKey(p)) && (KitNames.pkit.get(p) == "hades") &&
          (p.getItemInHand().getType() == Material.PUMPKIN))
        {
    That works, so why not the mob code?
     
  4. Offline

    fireblast709

    CactusComboPvP "Just the fact that it works for a set of cases, does not imply that it works for all". metalhedd is right, you have to use .equals() or .equalsIgnoreCase() due to how Strings work in Java.
     
    thomasb454 likes this.
  5. Offline

    metalhedd


    == compares object identity (ie. is this the exact same object, at the exact same memory location)
    .equals() just checks to see if they're equivalent.

    == sometimes works due to the fact that the JVM will reuse strings sometimes. but not always.
     
  6. Offline

    Obnoxious_Ninja

    Regardless, it's best practice to use .equals() or .equalsIgnoreCase for strings. Avoids potential future problems with plugins.
     
  7. Offline

    CactusComboPvP

    Obnoxious_Ninja metalhedd fireblast709
    Code:java
    1. @EventHandler
    2. public void onZombie(EntityTargetLivingEntityEvent e) {
    3. Player p = (Player) e.getEntity();
    4. if ((KitNames.pkit.containsKey(p)) && (KitNames.pkit.get(p).equalsIgnoreCase("hades"))) {
    5. e.setCancelled(true);
    6. }
    7. }


    Still no
     
  8. Offline

    Gater12

    CactusComboPvP
    Entity is the one targeting. getTarget is probably what you are looking for.
     
  9. Offline

    Garris0n

    Surely that code throws a ClassCastException.
     
  10. Offline

    CactusComboPvP

    Gater12 Ofcourse! Thanks! Worked :)
     
  11. Offline

    Garris0n

    Don't cast without doing an instanceof check first.
     
Thread Status:
Not open for further replies.

Share This Page