Plugin doesn't seem to like looping through a list

Discussion in 'Plugin Development' started by ZodiacTheories, Jul 27, 2014.

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


    Hi, so I am creating a plugin where you click a tnt, you get blasted in the air, and when you click again, you get pushed back down and you damage the entities beneath you. Everything but damaging the entities beneath you works. I added debug lines to see where the code didn't work, and it didn't work before

    1. for(Entity damaged : entities) {

    Here is my code:

    1. @EventHandler
    2. public void onInteract1(PlayerInteractEvent e) {
    3. if(!(e.getAction() == Action.LEFT_CLICK_AIR)) return;
    4. if(!nuker.contains(e.getPlayer().getName()) && !abouttonuke.contains(e.getPlayer().getName())) return;
    5. final Player p = e.getPlayer();
    6. p.setVelocity(new Vector(0, -2.25, 0));
    7. List<Entity> entities = p.getNearbyEntities(1, 1, 1);
    8. Core.getInstance().getLogger().info("Got the entities in a 1x1x1");
    9. for(Entity damaged : entities) {
    10. Core.getInstance().getLogger().info("Looped through");
    11. if(damaged instanceof Damageable) {
    12. ((Damageable) damaged).damage(10D);
    13. Core.getInstance().getLogger().info("Damaged");
    14. p.getWorld().playEffect(p.getLocation(), Effect.GHAST_SHOOT, 6, 6);
    15. Core.getInstance().getLogger().info("Ghast Shoot");
    16. p.getWorld().playSound(p.getLocation(), Sound.EXPLODE, 6F, 1F);
    17. Core.getInstance().getLogger().info("Explode");
    18. abouttonuke.remove(p.getName());
    19. Core.getInstance().getLogger().info("Removed from ArrayList");
  2. Offline


    What is the problem?

    Are there any entities nearby? You are only checking in a 1 block radius. You can check by outputting the size of entities with

    1. Core.getInstance().getLogger().info("Size of entity array: " + entities.size());

    Oh, just checking the java docs, the values passed in p.getNearbyEntities(x,y,z) are only half the radius. It looks like it creates a bounding box centered at the player.
  3. Offline



    Ok thanks I will try


    Thanks, that worked :)

    Also, is the entity not supposed to glow red (like when you take damage)? If so, would I have to use NMS to do it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 9, 2016
  4. Offline


    I'm not sure, but I know that if you check to see if they are an instanceof LivingEntity I'm pretty sure it would work to use entity.damage(10);
Thread Status:
Not open for further replies.

Share This Page