Check entity

Discussion in 'Plugin Development' started by RickPlayingPL, Aug 23, 2014.

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

    RickPlayingPL

    Hello, i have probem with check if looped entity is a ink sack

    1. for(World w : getServer().getWorlds()) {
    2. for(Entity e : w.getEntities()) {
    3. if(e.getType() == Material.GHAST_TEAR) {
    4. //do stuff
     
  2. Offline

    Nico4898

    Does it give you an error with your code?
     
  3. Offline

    jacklin213

    Use Material.INK_SACK instead oF Material.GHAST_TEAR ?
     
  4. Offline

    stormneo7

    NO no no no no no no no
    e.getType() where e is an entity returns EntityType, not Material.
    For example, EntityType.SQUID, EntityType.BLAZE, EntityType.ITEM.
    You need to check if e is an Item.
    Then cast e as an Item and use the .getItemStack() to get it's ItemStack.
    Only then you can check .getType() where it will return a material.

    Code:java
    1. for(World w : getServer().getWorlds()) {
    2. for(Entity e : w.getEntities()) {
    3. if(e instanceof Item) {
    4. ItemStack item = ((Item)e).getItemStack();
    5. if(item.getType() == Material.GHAST_TEAR){
    6. // Code
    7. }
    8. }
    9. }
    10. }
     
Thread Status:
Not open for further replies.

Share This Page