mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-02-18 21:32:04 +01:00
Flags for Block Explode (#1899)
* add Block Explosion support for TNT_DAMAGE flag * prevent block explosion from damaging entities * seperated flags for block explode * invert and cancel * pattern instanceof
This commit is contained in:
parent
dffe8a811e
commit
7088a2736f
@ -10,6 +10,7 @@ import org.bukkit.entity.Projectile;
|
|||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.block.Action;
|
import org.bukkit.event.block.Action;
|
||||||
|
import org.bukkit.event.block.BlockExplodeEvent;
|
||||||
import org.bukkit.event.entity.EntityChangeBlockEvent;
|
import org.bukkit.event.entity.EntityChangeBlockEvent;
|
||||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||||
import org.bukkit.event.entity.EntityDamageEvent;
|
import org.bukkit.event.entity.EntityDamageEvent;
|
||||||
@ -50,8 +51,8 @@ public class TNTListener extends FlagListener {
|
|||||||
// Stop TNT from being damaged if it is being caused by a visitor with a flaming arrow
|
// Stop TNT from being damaged if it is being caused by a visitor with a flaming arrow
|
||||||
if (e.getEntity() instanceof Projectile projectile) {
|
if (e.getEntity() instanceof Projectile projectile) {
|
||||||
// Find out who fired it
|
// Find out who fired it
|
||||||
if (projectile.getShooter() instanceof Player && projectile.getFireTicks() > 0
|
if (projectile.getShooter() instanceof Player shooter && projectile.getFireTicks() > 0
|
||||||
&& !checkIsland(e, (Player)projectile.getShooter(), e.getBlock().getLocation(), Flags.TNT_PRIMING)) {
|
&& !checkIsland(e, shooter, e.getBlock().getLocation(), Flags.TNT_PRIMING)) {
|
||||||
// Remove the arrow
|
// Remove the arrow
|
||||||
projectile.remove();
|
projectile.remove();
|
||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
@ -82,12 +83,17 @@ public class TNTListener extends FlagListener {
|
|||||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||||
public void onExplosion(final EntityExplodeEvent e) {
|
public void onExplosion(final EntityExplodeEvent e) {
|
||||||
// Check world and types
|
// Check world and types
|
||||||
if (getIWM().inWorld(e.getLocation()) && TNT_TYPES.contains(e.getEntityType())) {
|
if (!getIWM().inWorld(e.getLocation()) || !TNT_TYPES.contains(e.getEntityType())) {
|
||||||
// Remove any blocks from the explosion list if required
|
return;
|
||||||
e.blockList().removeIf(b -> protect(b.getLocation()));
|
|
||||||
e.setCancelled(protect(e.getLocation()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (protect(e.getLocation())) {
|
||||||
|
// This is protected as a whole, so just cancel the event
|
||||||
|
e.setCancelled(true);
|
||||||
|
} else {
|
||||||
|
// Remove any blocks from the explosion list if required
|
||||||
|
e.blockList().removeIf(b -> protect(b.getLocation()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean protect(Location location) {
|
protected boolean protect(Location location) {
|
||||||
@ -109,4 +115,43 @@ public class TNTListener extends FlagListener {
|
|||||||
e.setCancelled(protect(e.getEntity().getLocation()));
|
e.setCancelled(protect(e.getEntity().getLocation()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean protectBlockExplode(Location location) {
|
||||||
|
return getIslands().getProtectedIslandAt(location).map(i -> !i.isAllowed(Flags.BLOCK_EXPLODE_DAMAGE))
|
||||||
|
.orElseGet(() -> !Flags.WORLD_BLOCK_EXPLODE_DAMAGE.isSetForWorld(location.getWorld()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prevents block explosion from breaking blocks
|
||||||
|
* @param e - event
|
||||||
|
*/
|
||||||
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||||
|
public void onExplosion(final BlockExplodeEvent e) {
|
||||||
|
// Check world
|
||||||
|
if (!getIWM().inWorld(e.getBlock().getLocation())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (protectBlockExplode(e.getBlock().getLocation())) {
|
||||||
|
// This is protected as a whole, so just cancel the event
|
||||||
|
e.setCancelled(true);
|
||||||
|
} else {
|
||||||
|
// Remove any blocks from the explosion list if required
|
||||||
|
e.blockList().removeIf(b -> protectBlockExplode(b.getLocation()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prevents block explosion from damaging entities.
|
||||||
|
* @param e event
|
||||||
|
*/
|
||||||
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||||
|
public void onExplosion(final EntityDamageEvent e) {
|
||||||
|
// Check if this in world and a block explosion
|
||||||
|
if (getIWM().inWorld(e.getEntity().getLocation())
|
||||||
|
&& e.getCause().equals(EntityDamageEvent.DamageCause.BLOCK_EXPLOSION)) {
|
||||||
|
// Check if it is disallowed, then cancel it.
|
||||||
|
e.setCancelled(protectBlockExplode(e.getEntity().getLocation()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -392,6 +392,14 @@ public final class Flags {
|
|||||||
public static final Flag TNT_DAMAGE = new Flag.Builder("TNT_DAMAGE", Material.TNT).type(Type.SETTING)
|
public static final Flag TNT_DAMAGE = new Flag.Builder("TNT_DAMAGE", Material.TNT).type(Type.SETTING)
|
||||||
.mode(Flag.Mode.ADVANCED).build();
|
.mode(Flag.Mode.ADVANCED).build();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If {@code false}, prevents Block Explode from breaking blocks and damaging nearby entities.
|
||||||
|
* @since 1.19.1
|
||||||
|
* @see TNTListener
|
||||||
|
*/
|
||||||
|
public static final Flag BLOCK_EXPLODE_DAMAGE = new Flag.Builder("BLOCK_EXPLODE_DAMAGE", Material.TNT_MINECART).type(Type.SETTING)
|
||||||
|
.mode(Flag.Mode.ADVANCED).build();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If {@code false}, prevents TNT from breaking blocks and damaging nearby entities outside of island boundaries.
|
* If {@code false}, prevents TNT from breaking blocks and damaging nearby entities outside of island boundaries.
|
||||||
* @since 1.15.3
|
* @since 1.15.3
|
||||||
@ -401,6 +409,15 @@ public final class Flags {
|
|||||||
.type(Type.WORLD_SETTING)
|
.type(Type.WORLD_SETTING)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If {@code false}, prevents Block Explode from breaking blocks and damaging nearby entities outside of island boundaries.
|
||||||
|
* @since 1.19.1
|
||||||
|
* @see TNTListener
|
||||||
|
*/
|
||||||
|
public static final Flag WORLD_BLOCK_EXPLODE_DAMAGE = new Flag.Builder("WORLD_BLOCK_EXPLODE_DAMAGE", Material.TNT_MINECART)
|
||||||
|
.type(Type.WORLD_SETTING)
|
||||||
|
.build();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* World Settings - they apply to every island in the game worlds.
|
* World Settings - they apply to every island in the game worlds.
|
||||||
*/
|
*/
|
||||||
|
@ -815,6 +815,12 @@ protection:
|
|||||||
name: "Barrels"
|
name: "Barrels"
|
||||||
description: "Toggle barrel interaction"
|
description: "Toggle barrel interaction"
|
||||||
hint: "Barrel access disabled"
|
hint: "Barrel access disabled"
|
||||||
|
BLOCK_EXPLODE_DAMAGE:
|
||||||
|
description: |-
|
||||||
|
&a Allow Bed & Respawn Anchors
|
||||||
|
&a to break blocks and damage
|
||||||
|
&a entities.
|
||||||
|
name: "Block explode damage"
|
||||||
COMPOSTER:
|
COMPOSTER:
|
||||||
name: "Composters"
|
name: "Composters"
|
||||||
description: "Toggle composter interaction"
|
description: "Toggle composter interaction"
|
||||||
@ -1320,6 +1326,12 @@ protection:
|
|||||||
description: |-
|
description: |-
|
||||||
&a If active, withers can
|
&a If active, withers can
|
||||||
&a damage blocks and players
|
&a damage blocks and players
|
||||||
|
WORLD_BLOCK_EXPLODE_DAMAGE:
|
||||||
|
description: |-
|
||||||
|
&a Allow Bed & Respawn Anchors
|
||||||
|
&a to break blocks and damage
|
||||||
|
&a entities outside of island limits.
|
||||||
|
name: "World block explode damage"
|
||||||
WORLD_TNT_DAMAGE:
|
WORLD_TNT_DAMAGE:
|
||||||
description: |-
|
description: |-
|
||||||
&a Allow TNT and TNT minecarts
|
&a Allow TNT and TNT minecarts
|
||||||
|
Loading…
Reference in New Issue
Block a user