Merge pull request #2432 from BentoBoxWorld/2422_flags_for_1.21

2422 flags for 1.21
This commit is contained in:
tastybento 2024-07-16 17:11:44 -07:00 committed by GitHub
commit f515ae2e84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 291 additions and 29 deletions

View File

@ -100,9 +100,11 @@ public class BlockInteractionListener extends FlagListener
switch (type)
{
case BEACON -> this.checkIsland(e, player, loc, Flags.BEACON);
case BELL -> this.checkIsland(e, player, loc, Flags.BELL_RINGING);
case BREWING_STAND -> this.checkIsland(e, player, loc, Flags.BREWING);
case BEEHIVE, BEE_NEST -> this.checkIsland(e, player, loc, Flags.HIVE);
case BARREL -> this.checkIsland(e, player, loc, Flags.BARREL);
case CANDLE -> this.checkIsland(e, player, loc, Flags.CANDLES);
case CHEST, CHEST_MINECART -> this.checkIsland(e, player, loc, Flags.CHEST);
case TRAPPED_CHEST -> this.checkIsland(e, player, loc, Flags.TRAPPED_CHEST);
case FLOWER_POT -> this.checkIsland(e, player, loc, Flags.FLOWER_POT);

View File

@ -21,6 +21,8 @@ import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.vehicle.VehicleDamageEvent;
import com.google.common.base.Enums;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.lists.Flags;
@ -101,6 +103,10 @@ public class BreakBlocksListener extends FlagListener {
{
return;
}
if (Enums.getIfPresent(Material.class, "TRIAL_SPAWNER").isPresent() && m.equals(Material.TRIAL_SPAWNER)) {
this.checkIsland(e, p, l, Flags.BREAK_SPAWNERS);
return;
}
switch (m)
{
case CAKE -> this.checkIsland(e, p, l, Flags.BREAK_BLOCKS);
@ -182,7 +188,7 @@ public class BreakBlocksListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onProjectileHitBreakBlock(ProjectileHitEvent e) {
// We want to make sure this is an actual projectile (arrow or trident)
if (!(e.getEntity() instanceof AbstractArrow)) {
if (!(e.getEntity() instanceof Projectile)) {
return;
}

View File

@ -0,0 +1,33 @@
package world.bentobox.bentobox.listeners.flags.protection;
import org.bukkit.Tag;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerInteractEvent;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.lists.Flags;
/**
* Protects candles
* @author tastybento
* @since 2.4.2
*/
public class CandleListener extends FlagListener {
/**
* Prevent dying signs.
* @param e - event
*/
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onCandleInteract(final PlayerInteractEvent e) {
if (e.getClickedBlock() == null) {
return;
}
if (Tag.CANDLES.isTagged(e.getClickedBlock().getType())
|| Tag.CANDLE_CAKES.isTagged(e.getClickedBlock().getType())) {
this.checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.CANDLES);
}
}
}

View File

@ -27,7 +27,7 @@ import world.bentobox.bentobox.lists.Flags;
* Protects islands from visitors blowing things up
* @author tastybento
*/
public class TNTListener extends FlagListener {
public class ExplosionListener extends FlagListener {
/**
* Contains {@link EntityType}s that generates an explosion.
* @since 1.5.0

View File

@ -1,15 +1,21 @@
package world.bentobox.bentobox.listeners.flags.protection;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.lists.Flags;
@ -55,24 +61,43 @@ public class PhysicalInteractionListener extends FlagListener
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onProjectileHit(EntityInteractEvent e)
{
if (!(e.getEntity() instanceof Projectile p))
if (e.getEntity() instanceof Projectile p && p.getShooter() instanceof Player player)
{
return;
checkBlocks(e, player, e.getBlock());
}
}
private boolean checkBlocks(Event e, Player player, Block block) {
Map<Tag<Material>, Flag> TAG_TO_FLAG = Map.of(Tag.WOODEN_BUTTONS, Flags.BUTTON, Tag.PRESSURE_PLATES,
Flags.PRESSURE_PLATE, Tag.FENCE_GATES, Flags.GATE, Tag.DOORS, Flags.DOOR, Tag.CANDLE_CAKES,
Flags.CANDLES, Tag.CANDLES, Flags.CANDLES);
Map<Material, Flag> MAT_TO_FLAG = Map.of(Material.LEVER, Flags.LEVER, Material.TRIPWIRE, Flags.REDSTONE,
Material.TARGET, Flags.REDSTONE, Material.DECORATED_POT, Flags.BREAK_BLOCKS);
boolean result = TAG_TO_FLAG.entrySet().stream().filter(entry -> entry.getKey().isTagged(block.getType()))
.findFirst().map(entry -> this.checkIsland(e, player, block.getLocation(), entry.getValue()))
.orElse(true);
if (result && MAT_TO_FLAG.containsKey(block.getType())) {
result = this.checkIsland(e, player, block.getLocation(), MAT_TO_FLAG.get(block.getType()));
}
if (p.getShooter() instanceof Player player)
{
if (Tag.WOODEN_BUTTONS.isTagged(e.getBlock().getType()))
{
this.checkIsland(e, player, e.getBlock().getLocation(), Flags.BUTTON);
return;
}
return result;
}
if (Tag.PRESSURE_PLATES.isTagged(e.getBlock().getType()))
{
// Pressure plates
this.checkIsland(e, player, e.getBlock().getLocation(), Flags.PRESSURE_PLATE);
/**
* Protects buttons and plates, etc. from being activated by projectiles that explode
* @param e - event
*/
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onProjectileExplode(EntityExplodeEvent e) {
if (e.getEntity() instanceof Projectile p && p.getShooter() instanceof Player player) {
for (Block b : e.blockList()) {
if (!this.checkBlocks(e, player, b)) {
e.blockList().clear();
}
}
}
}
}

View File

@ -1,6 +1,5 @@
package world.bentobox.bentobox.listeners.flags.worldsettings;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerInteractEvent;
@ -21,8 +20,8 @@ public class SpawnerSpawnEggsListener extends FlagListener {
public void onSpawnerChange(final PlayerInteractEvent e) {
User user = User.getInstance(e.getPlayer());
// Checking if the clicked block is a spawner and the item in hand is a mob egg
if (e.getClickedBlock() != null && e.getClickedBlock().getType().equals(Material.SPAWNER)
&& e.getItem() != null && e.getItem().getType().toString().endsWith("_SPAWN_EGG")
if (e.getClickedBlock() != null && e.getClickedBlock().getType().name().endsWith("_SPAWNER")
&& e.getItem() != null && e.getItem().getType().name().endsWith("_SPAWN_EGG")
&& getIWM().inWorld(e.getClickedBlock().getWorld())
&& !(user.hasPermission(getIWM().getPermissionPrefix(e.getClickedBlock().getWorld()) + "mod.bypass." + Flags.SPAWNER_SPAWN_EGGS.getID() + ".everywhere")
|| user.hasPermission(getIWM().getPermissionPrefix(e.getClickedBlock().getWorld()) + "mod.bypassprotect"))

View File

@ -19,6 +19,7 @@ import world.bentobox.bentobox.listeners.flags.protection.BlockInteractionListen
import world.bentobox.bentobox.listeners.flags.protection.BreakBlocksListener;
import world.bentobox.bentobox.listeners.flags.protection.BreedingListener;
import world.bentobox.bentobox.listeners.flags.protection.BucketListener;
import world.bentobox.bentobox.listeners.flags.protection.CandleListener;
import world.bentobox.bentobox.listeners.flags.protection.DyeListener;
import world.bentobox.bentobox.listeners.flags.protection.EggListener;
import world.bentobox.bentobox.listeners.flags.protection.ElytraListener;
@ -38,7 +39,7 @@ import world.bentobox.bentobox.listeners.flags.protection.PortalListener;
import world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener;
import world.bentobox.bentobox.listeners.flags.protection.SculkShriekerListener;
import world.bentobox.bentobox.listeners.flags.protection.ShearingListener;
import world.bentobox.bentobox.listeners.flags.protection.TNTListener;
import world.bentobox.bentobox.listeners.flags.protection.ExplosionListener;
import world.bentobox.bentobox.listeners.flags.protection.TeleportationListener;
import world.bentobox.bentobox.listeners.flags.protection.ThrowingListener;
import world.bentobox.bentobox.listeners.flags.settings.DecayListener;
@ -266,9 +267,9 @@ public final class Flags {
* Prevents players from priming TNT.
* @since 1.5.0
*
* @see TNTListener
* @see ExplosionListener
*/
public static final Flag TNT_PRIMING = new Flag.Builder("TNT_PRIMING", Material.TNT).listener(new TNTListener()).build();
public static final Flag TNT_PRIMING = new Flag.Builder("TNT_PRIMING", Material.TNT).listener(new ExplosionListener()).build();
/**
* Prevents players from extinguishing fires.
@ -461,7 +462,7 @@ public final class Flags {
/**
* If {@code false}, prevents TNT from breaking blocks and damaging nearby entities.
* @since 1.5.0
* @see TNTListener
* @see ExplosionListener
*/
public static final Flag TNT_DAMAGE = new Flag.Builder("TNT_DAMAGE", Material.TNT).type(Type.SETTING)
.mode(Flag.Mode.ADVANCED).build();
@ -469,7 +470,7 @@ public final class Flags {
/**
* If {@code false}, prevents Block Explode from breaking blocks and damaging nearby entities.
* @since 1.19.1
* @see TNTListener
* @see ExplosionListener
*/
public static final Flag BLOCK_EXPLODE_DAMAGE = new Flag.Builder("BLOCK_EXPLODE_DAMAGE", Material.TNT_MINECART).type(Type.SETTING)
.mode(Flag.Mode.ADVANCED).build();
@ -477,7 +478,7 @@ public final class Flags {
/**
* If {@code false}, prevents TNT from breaking blocks and damaging nearby entities outside of island boundaries.
* @since 1.15.3
* @see TNTListener
* @see ExplosionListener
*/
public static final Flag WORLD_TNT_DAMAGE = new Flag.Builder("WORLD_TNT_DAMAGE", Material.TNT)
.type(Type.WORLD_SETTING)
@ -486,7 +487,7 @@ public final class Flags {
/**
* If {@code false}, prevents Block Explode from breaking blocks and damaging nearby entities outside of island boundaries.
* @since 1.19.1
* @see TNTListener
* @see ExplosionListener
*/
public static final Flag WORLD_BLOCK_EXPLODE_DAMAGE = new Flag.Builder("WORLD_BLOCK_EXPLODE_DAMAGE", Material.TNT_MINECART)
.type(Type.WORLD_SETTING)
@ -687,6 +688,23 @@ public final class Flags {
*/
public static final Flag SIGN_EDITING = new Flag.Builder("SIGN_EDITING", Material.DARK_OAK_SIGN).mode(Flag.Mode.BASIC).type(Type.PROTECTION).build();
/**
* Bell ringing protection
* Listeners are {@link BlockInteractionListener} and {@link PhysicalInteractionListener}
* @since 2.4.2
*/
public static final Flag BELL_RINGING = new Flag.Builder("BELL_RINGING", Material.BELL).mode(Flag.Mode.EXPERT)
.type(Type.PROTECTION).build();
/**
* Candle protection
* Listener is {@link CandleListener}
* @since 2.4.2
*/
public static final Flag CANDLES = new Flag.Builder("CANDLES", Material.CANDLE).mode(Flag.Mode.EXPERT)
.listener(new CandleListener())
.type(Type.PROTECTION).build();
/**
* Provides a list of all the Flag instances contained in this class using reflection.
* Deprecated Flags are ignored.

View File

@ -38,7 +38,7 @@ public class IslandCache {
* Map of all islands with island uniqueId as key
*/
@NonNull
private final Map<@NonNull String, @NonNull Island> islandsById;
private final Map<@NonNull String, Island> islandsById;
/**
* Every player who is associated with an island is in this map. Key is player
* UUID, value is a set of islands

View File

@ -912,6 +912,10 @@ protection:
description: Toggle interaction
name: Beacons
hint: Beacon use disabled
BELL_RINGING:
description: Toggle interaction
name: Allow bell ringing
hint: Bell ringing disabled
BED:
description: Toggle interaction
name: Beds
@ -960,6 +964,10 @@ protection:
description: Toggle button use
name: Buttons
hint: Button use disabled
CANDLES:
description: Toggle candle interaction
name: Candles
hint: Candle interaction disabled
CAKE:
description: Toggle cake interaction
name: Cakes

View File

@ -0,0 +1,110 @@
package world.bentobox.bentobox.listeners.flags.protection;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ BentoBox.class, Flags.class, Util.class, Bukkit.class })
public class CandleListenerTest extends AbstractCommonSetup {
private CandleListener l;
@Mock
private Block block;
/**
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
// Island manager
// Default is that everything is allowed
when(island.isAllowed(any(), any())).thenReturn(true);
when(block.getLocation()).thenReturn(location);
// Tags
when(Tag.CANDLES.isTagged(any(Material.class))).thenReturn(true);
when(Tag.CANDLE_CAKES.isTagged(any(Material.class))).thenReturn(true);
// Listener
l = new CandleListener();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.CandleListener#onCandleInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnCandleInteract() {
// Block
when(block.getType()).thenReturn(Material.CANDLE);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.LEFT_CLICK_BLOCK, null, block, BlockFace.UP);
l.onCandleInteract(e);
assertEquals(Result.ALLOW, e.useInteractedBlock());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.CandleListener#onCandleInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnCandleCakeInteract() {
// Block
when(block.getType()).thenReturn(Material.CANDLE_CAKE);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.LEFT_CLICK_BLOCK, null, block, BlockFace.UP);
l.onCandleInteract(e);
assertEquals(Result.ALLOW, e.useInteractedBlock());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.CandleListener#onCandleInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnCandleInteractFail() {
when(island.isAllowed(any(), any())).thenReturn(false);
// Block
when(block.getType()).thenReturn(Material.CANDLE);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.LEFT_CLICK_BLOCK, null, block, BlockFace.UP);
l.onCandleInteract(e);
assertEquals(Result.DENY, e.useInteractedBlock());
verify(notifier).notify(any(), eq("protection.protected"));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.CandleListener#onCandleInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnCandleCakeInteractFail() {
when(island.isAllowed(any(), any())).thenReturn(false);
// Block
when(block.getType()).thenReturn(Material.CANDLE_CAKE);
PlayerInteractEvent e = new PlayerInteractEvent(mockPlayer, Action.LEFT_CLICK_BLOCK, null, block, BlockFace.UP);
l.onCandleInteract(e);
assertEquals(Result.DENY, e.useInteractedBlock());
verify(notifier).notify(any(), eq("protection.protected"));
}
}

View File

@ -7,10 +7,13 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -25,6 +28,7 @@ import org.bukkit.entity.Slime;
import org.bukkit.entity.Zombie;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
@ -228,4 +232,61 @@ public class PhysicalInteractionListenerTest extends AbstractCommonSetup {
assertTrue(p.name() +" failed", e.isCancelled());
});
}
/**
* Test method for {@link PhysicalInteractionListener#onProjectileExplode(org.bukkit.event.entity.EntityExplodeEvent)}.
*/
@Test
public void testOnProjectileExplodeNotProjectile() {
Entity entity = mock(Entity.class);
List<Block> blocks = new ArrayList<>();
EntityExplodeEvent e = new EntityExplodeEvent(entity, location, blocks, 0);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onProjectileExplode(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link PhysicalInteractionListener#onProjectileExplode(org.bukkit.event.entity.EntityExplodeEvent)}.
*/
@Test
public void testOnProjectileExplodeProjectileNoPlayer() {
Projectile entity = mock(Projectile.class);
ProjectileSource source = mock(Creeper.class);
when(entity.getShooter()).thenReturn(source);
List<Block> blocks = new ArrayList<>();
EntityExplodeEvent e = new EntityExplodeEvent(entity, location, blocks, 0);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onProjectileExplode(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link PhysicalInteractionListener#onProjectileExplode(org.bukkit.event.entity.EntityExplodeEvent)}.
*/
@Test
public void testOnProjectileExplodeProjectilePlayer() {
Projectile entity = mock(Projectile.class);
when(entity.getShooter()).thenReturn(mockPlayer);
List<Block> blocks = new ArrayList<>();
Block block1 = mock(Block.class);
Block block2 = mock(Block.class);
when(block1.getLocation()).thenReturn(location);
when(block2.getLocation()).thenReturn(location);
blocks.add(block1);
blocks.add(block2);
EntityExplodeEvent e = new EntityExplodeEvent(entity, location, blocks, 0);
PhysicalInteractionListener i = new PhysicalInteractionListener();
// Test with wooden button
when(block1.getType()).thenReturn(Material.OAK_BUTTON);
when(Tag.WOODEN_BUTTONS.isTagged(Material.OAK_BUTTON)).thenReturn(true);
// Test with pressure plate
when(block2.getType()).thenReturn(Material.STONE_PRESSURE_PLATE);
when(Tag.PRESSURE_PLATES.isTagged(Material.STONE_PRESSURE_PLATE)).thenReturn(true);
i.onProjectileExplode(e);
verify(notifier, times(2)).notify(any(), eq("protection.protected"));
}
}

View File

@ -58,7 +58,7 @@ public class TNTListenerTest extends AbstractCommonSetup {
private Entity entity;
// Class under test
private TNTListener listener;
private ExplosionListener listener;
@Override
@Before
@ -85,7 +85,7 @@ public class TNTListenerTest extends AbstractCommonSetup {
// Util
when(Util.findFirstMatchingEnum(any(), anyString())).thenCallRealMethod();
listener = new TNTListener();
listener = new ExplosionListener();
listener.setPlugin(plugin);
}

View File

@ -46,7 +46,7 @@ public class FlagsManagerTest {
/**
* Update this value if the number of registered listeners changes
*/
private static final int NUMBER_OF_LISTENERS = 54;
private static final int NUMBER_OF_LISTENERS = 55;
@Mock
private BentoBox plugin;
@Mock