Solved Detecting Sweet bush vs. Cactus on EntityDamageEvent?

Discussion in 'Spigot Plugin Development' started by SocialSpy, Feb 13, 2020.

Thread Status:
Not open for further replies.
  1. Hi all,
    I'm having trouble finding an efficient method for determining whether the DamageCause.CONTACT fired in EntityDamageEvent is caused by either a Sweet Berry Bush or Cactus block.

    There is of course the workaround of checking every surrounding block's type, but I'd prefer something a little less resource intensive.
    Code (Text):
        @EventHandler(ignoreCancelled = false)
        public void onDamage(EntityDamageEvent e) {
            U.logI("Damage type: " + e.getCause());
            if (e.getCause().equals(DamageCause.CONTACT)) {
                for (Location check : surrounding(e.getEntity().getLocation())) {
                    if (check.getBlock().getType().equals(Material.CACTUS)) {
                        // do cactus stuff
                        return;
                    }
                }
                // do sugar bush stuff
            }
        }
       
        private Location[] surrounding(final Location l) {
            final List<Location> all = new ArrayList<>();
            Location corner = l.clone();
            corner.add(-1, -2, -1);
            for (int x = corner.getBlockX(); x <= corner.getBlockX() + 2; x++) {
                for (int y = corner.getBlockY(); y <= corner.getBlockY() + 4; y++) {
                    for (int z = corner.getBlockZ(); z <= corner.getBlockZ() + 2; z++) {
                        all.add(new Location(corner.getWorld(), corner.getX() + x, corner.getY() + y, corner.getZ() + z));
                    }
                }
            }
            return all.toArray(new Location[all.size()]);
        }
     
    #1 SocialSpy, Feb 13, 2020
    Last edited: Feb 13, 2020
  2. Hint: Use EntityDamageByBlock instead.
     
    • Useful Useful x 1
  3. <3 I spent way too much time stuck on this, little embarrassed. Thanks!
     
Thread Status:
Not open for further replies.