tastybento 2020-11-26 18:05:26 -08:00
parent caed56f16e
commit 2cb1e7c306
2 changed files with 62 additions and 10 deletions

View File

@ -18,7 +18,6 @@ import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.lists.Flags;
@ -85,11 +84,11 @@ public class TNTListener extends FlagListener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onExplosion(final EntityExplodeEvent e) {
if (TNT_TYPES.contains(e.getEntityType())) {
// Check world and types
if (getIWM().inWorld(e.getLocation()) && TNT_TYPES.contains(e.getEntityType())) {
// Remove any blocks from the explosion list if required
e.blockList().removeIf(b -> protect(b.getLocation()));
e.setCancelled(protect(e.getLocation()));
BentoBox.getInstance().logDebug(e.getEventName() + " " + e.isCancelled());
}
}
@ -105,13 +104,12 @@ public class TNTListener extends FlagListener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onExplosion(final EntityDamageByEntityEvent e) {
// Check if this a TNT exploding
if (!e.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION) || !TNT_TYPES.contains(e.getDamager().getType())) {
return;
// Check if this in world, an explosion, and TNT exploding
if (getIWM().inWorld(e.getEntity().getLocation())
&& e.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)
&& TNT_TYPES.contains(e.getDamager().getType())) {
// Check if it is disallowed, then cancel it.
e.setCancelled(protect(e.getEntity().getLocation()));
}
// Check if it is disallowed, then cancel it.
e.setCancelled(protect(e.getEntity().getLocation()));
BentoBox.getInstance().logDebug(e.getEventName() + " " + e.isCancelled());
}
}

View File

@ -35,6 +35,8 @@ import org.bukkit.entity.Zombie;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
@ -176,6 +178,13 @@ public class TNTListenerTest {
// Entity
when(entity.getType()).thenReturn(EntityType.PRIMED_TNT);
when(entity.getWorld()).thenReturn(world);
when(entity.getLocation()).thenReturn(location);
// Player
when(player.getLocation()).thenReturn(location);
// In world
when(iwm.inWorld(any(Location.class))).thenReturn(true);
listener = new TNTListener();
@ -238,6 +247,16 @@ public class TNTListenerTest {
assertFalse(list.isEmpty());
}
@Test
public void testOnExplosionWrongWorld() {
when(iwm.inWorld(any(Location.class))).thenReturn(false);
List<Block> list = new ArrayList<>();
list.add(block);
EntityExplodeEvent e = new EntityExplodeEvent(entity, location, list, 0);
listener.onExplosion(e);
assertFalse(list.isEmpty());
}
@Test
public void testOnTNTDamageInWorldTNTNotProjectile() {
// Block on fire
@ -389,5 +408,40 @@ public class TNTListenerTest {
}
@Test
public void testOnEntityExplosion() {
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, player, DamageCause.ENTITY_EXPLOSION, 20D);
listener.onExplosion(e);
assertTrue(e.isCancelled());
}
@Test
public void testOnEntityExplosionOutsideIsland() {
Flags.WORLD_TNT_DAMAGE.setDefaultSetting(false);
assertFalse(Flags.WORLD_TNT_DAMAGE.isSetForWorld(world));
when(im.getProtectedIslandAt(any())).thenReturn(Optional.empty());
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, player, DamageCause.ENTITY_EXPLOSION, 20D);
listener.onExplosion(e);
assertTrue(e.isCancelled());
}
@Test
public void testOnEntityExplosionOutsideIslandAllowed() {
Flags.WORLD_TNT_DAMAGE.setDefaultSetting(true);
assertTrue(Flags.WORLD_TNT_DAMAGE.isSetForWorld(world));
when(im.getProtectedIslandAt(any())).thenReturn(Optional.empty());
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, player, DamageCause.ENTITY_EXPLOSION, 20D);
listener.onExplosion(e);
assertFalse(e.isCancelled());
}
@Test
public void testOnEntityExplosionWrongWorld() {
when(iwm.inWorld(any(Location.class))).thenReturn(false);
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, player, DamageCause.ENTITY_EXPLOSION, 20D);
listener.onExplosion(e);
assertFalse(e.isCancelled());
}
}