Hi, i've been coding a plugin for my friend recently, When a snowball hit's a player/around a player it adds two potions effects to them, Blindness, and Confusion it seems to be fine but when i try and bring the entities up it gives me the error of Type mismatch: cannot convert from element type Object to Entity? I've included the code below.. any help would be greatly appreciated Thanks Code: @EventHandler public void onHit(ProjectileHitEvent event) { if ((event.getEntity() instanceof Snowball)) { Entity snowball = event.getEntity(); Location loc = event.getEntity().getLocation(); World world = event.getEntity().getWorld(); world.createExplosion(loc, 0.0F, false); List entities = snowball.getNearbyEntities(4.0D, 4.0D, 4.0D); for (Entity entity : entities) { if (!(entity instanceof Player)) { continue; } ((Player)entity).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, this.plugin.getConfig().getInt("Grenades.FlashBombEffect"), 1), true); ((Player)entity).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, this.plugin.getConfig().getInt("Grenades.FlashBombEffect"), 1), true); } } else if ((event.getEntity() instanceof Egg)) { Location loc = event.getEntity().getLocation(); World world = event.getEntity().getWorld(); world.createExplosion(loc, this.plugin.getConfig().getInt("Grenades.EggGrenadeExplosion"), false); } } } The error that gets brought up in eclipse is Code: for (Entity entity : entities) And as i said before, any help would be appreciated xD
Give the list a type. Code: @EventHandler public void onHit(ProjectileHitEvent event) { if ((event.getEntity() instanceof Snowball)) { Entity snowball = event.getEntity(); Location loc = event.getEntity().getLocation(); World world = event.getEntity().getWorld(); world.createExplosion(loc, 0.0F, false); //This makes the list a list of Entities List<Entity> entities = snowball.getNearbyEntities(4.0D, 4.0D, 4.0D); for (Entity entity : entities) { if (!(entity instanceof Player)) { continue; } ((Player)entity).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, this.plugin.getConfig().getInt("Grenades.FlashBombEffect"), 1), true); ((Player)entity).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, this.plugin.getConfig().getInt("Grenades.FlashBombEffect"), 1), true); } } else if ((event.getEntity() instanceof Egg)) { Location loc = event.getEntity().getLocation(); World world = event.getEntity().getWorld(); world.createExplosion(loc, this.plugin.getConfig().getInt("Grenades.EggGrenadeExplosion"), false); } } }
do something like this: Code: List entities = ... Entity[] e = new Entity[entities.size()]; e = entities.toArray[e]; for(Entity entity : e) { //stuff } that will convert the list into in array of entities and should fix your problem
That's a bit of redundancy when he can just define the type of List he's dealing with with List<Entity> list = ... I think you're just a bit confused. Code: List entities = ... // - No type, so it defaults to Object Entity[] e = new Entity[entities.size()]; // - Now it's a list of Entity e = entities.toArray[e]; // - Syntax and conversion errors //Changing to this fixes all errors, even though it's sloppy List<Entity> entities = ... // - Okay, that works better now Entity[] e = new Entity[entities.size()]; // - Now it's a list of Entity e = (Entity[]) entities.toArray(); // - Converting the Entity list to Entity Array // Works since the List above has a type While this would prevent issues with List synchronization since you don't modify the List directly, I personally prefer this way. It's just cleaner and less to type.: Code: List<Entity> entities = ... for (Entity ent : entities) { } Also, your syntax and usage is incorrect: Code: e = entities.toArray[e]; ^ ^ ^ | | this is an Entity[] but toArray() doesn't need an argument | toArray[] cannot be resolved or is not a field -- no square brackets here! needs to be fixed to cast to Entity[] and not have an argument Anyways, that being said, don't take this personal; I'm just here to help you learn. Happy coding.
Thanks for the quick replies! Scizzr your solution worked, I also tried yours Jogy34 but yours didnt work, it gave loads of errors :/ But thanks Scizzr again, i needed this plugin fixing