Merge pull request #2148 from BentoBoxWorld/sculk_cal

Adds calibrated sculk sensor to protection
This commit is contained in:
tastybento 2023-06-24 12:55:36 -07:00 committed by GitHub
commit 66d27e7070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 169 additions and 6 deletions

View File

@ -125,6 +125,7 @@ public abstract class FlagListener implements Listener {
* @return true if the check is okay, false if it was disallowed
*/
public boolean checkIsland(@NonNull Event e, @Nullable Player player, @Nullable Location loc, @NonNull Flag flag, boolean silent) {
// Set user
user = player == null ? null : User.getInstance(player);
if (loc == null) {

View File

@ -13,6 +13,8 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockReceiveGameEvent;
import com.google.common.base.Enums;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.lists.Flags;
@ -25,20 +27,22 @@ public class SculkSensorListener extends FlagListener
/**
* This listener detects if a visitor activates sculk sensor, and block it, if required.
* @param event Sculk activation event.
* @return true if the check is okay, false if it was disallowed
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onSculkSensor(BlockReceiveGameEvent event)
public boolean onSculkSensor(BlockReceiveGameEvent event)
{
if (!this.getIWM().inWorld(event.getBlock().getWorld()))
{
return;
return true;
}
if (event.getBlock().getType() == Material.SCULK_SENSOR &&
event.getEntity() != null &&
event.getEntity() instanceof Player player)
if ((event.getBlock().getType() == Material.SCULK_SENSOR
|| event.getBlock().getType() == Enums.getIfPresent(Material.class, "CALIBRATED_SCULK_SENSOR").or(Material.SCULK_SENSOR))
&& event.getEntity() != null && event.getEntity() instanceof Player player)
{
this.checkIsland(event, player, event.getBlock().getLocation(), Flags.SCULK_SENSOR, true);
return this.checkIsland(event, player, event.getBlock().getLocation(), Flags.SCULK_SENSOR, true);
}
return true;
}
}

View File

@ -0,0 +1,158 @@
package world.bentobox.bentobox.listeners.flags.protection;
import static org.junit.Assert.*;
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.GameEvent;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.event.block.BlockReceiveGameEvent;
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.api.user.User;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class} )
public class SculkSensorListenerTest extends AbstractCommonSetup {
private SculkSensorListener ssl;
@Mock
private Block block;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
super.setUp();
// Default is that everything is allowed
when(island.isAllowed(any(), any())).thenReturn(true);
// In world
when(iwm.inWorld(any(World.class))).thenReturn(true);
// Block
when(block.getType()).thenReturn(Material.SCULK_SENSOR);
when(block.getWorld()).thenReturn(world);
when(block.getLocation()).thenReturn(location);
// User
when(player.getWorld()).thenReturn(world);
when(player.getLocation()).thenReturn(location);
User.getInstance(player);
ssl = new SculkSensorListener();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener#onSculkSensor(org.bukkit.event.block.BlockReceiveGameEvent)}.
*/
@Test
public void testOnSculkSensorNotAllowed() {
when(island.isAllowed(any(), any())).thenReturn(false);
BlockReceiveGameEvent e = new BlockReceiveGameEvent(GameEvent.BLOCK_ACTIVATE, block, player);
assertFalse(ssl.onSculkSensor(e));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener#onSculkSensor(org.bukkit.event.block.BlockReceiveGameEvent)}.
*/
@Test
public void testOnSculkSensorAllowed() {
BlockReceiveGameEvent e = new BlockReceiveGameEvent(GameEvent.BLOCK_ACTIVATE, block, player);
assertTrue(ssl.onSculkSensor(e));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener#onSculkSensor(org.bukkit.event.block.BlockReceiveGameEvent)}.
*/
@Test
public void testOnSculkSensorNotInWorld() {
when(iwm.inWorld(any(World.class))).thenReturn(false);
BlockReceiveGameEvent e = new BlockReceiveGameEvent(GameEvent.BLOCK_ACTIVATE, block, player);
assertTrue(ssl.onSculkSensor(e));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener#onSculkSensor(org.bukkit.event.block.BlockReceiveGameEvent)}.
*/
@Test
public void testOnSculkSensorNotAllowedCalibrated() {
when(block.getType()).thenReturn(Material.CALIBRATED_SCULK_SENSOR);
when(island.isAllowed(any(), any())).thenReturn(false);
BlockReceiveGameEvent e = new BlockReceiveGameEvent(GameEvent.BLOCK_ACTIVATE, block, player);
assertFalse(ssl.onSculkSensor(e));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener#onSculkSensor(org.bukkit.event.block.BlockReceiveGameEvent)}.
*/
@Test
public void testOnSculkSensorAllowedCalibrated() {
when(block.getType()).thenReturn(Material.CALIBRATED_SCULK_SENSOR);
BlockReceiveGameEvent e = new BlockReceiveGameEvent(GameEvent.BLOCK_ACTIVATE, block, player);
assertTrue(ssl.onSculkSensor(e));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener#onSculkSensor(org.bukkit.event.block.BlockReceiveGameEvent)}.
*/
@Test
public void testOnSculkSensorNotInWorldCalibrated() {
when(block.getType()).thenReturn(Material.CALIBRATED_SCULK_SENSOR);
when(iwm.inWorld(any(World.class))).thenReturn(false);
BlockReceiveGameEvent e = new BlockReceiveGameEvent(GameEvent.BLOCK_ACTIVATE, block, player);
assertTrue(ssl.onSculkSensor(e));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener#onSculkSensor(org.bukkit.event.block.BlockReceiveGameEvent)}.
*/
@Test
public void testOnSculkSensorNotAllowedNotSculk() {
when(block.getType()).thenReturn(Material.SHULKER_BOX);
when(island.isAllowed(any(), any())).thenReturn(false);
BlockReceiveGameEvent e = new BlockReceiveGameEvent(GameEvent.BLOCK_ACTIVATE, block, player);
assertTrue(ssl.onSculkSensor(e));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener#onSculkSensor(org.bukkit.event.block.BlockReceiveGameEvent)}.
*/
@Test
public void testOnSculkSensorAllowedNotSculk() {
when(block.getType()).thenReturn(Material.SHULKER_BOX);
BlockReceiveGameEvent e = new BlockReceiveGameEvent(GameEvent.BLOCK_ACTIVATE, block, player);
assertTrue(ssl.onSculkSensor(e));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener#onSculkSensor(org.bukkit.event.block.BlockReceiveGameEvent)}.
*/
@Test
public void testOnSculkSensorNotInWorldNotSculk() {
when(block.getType()).thenReturn(Material.SHULKER_BOX);
when(iwm.inWorld(any(World.class))).thenReturn(false);
BlockReceiveGameEvent e = new BlockReceiveGameEvent(GameEvent.BLOCK_ACTIVATE, block, player);
assertTrue(ssl.onSculkSensor(e));
}
}