mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-23 03:05:16 +01:00
Fix and enhancement for #2105
This commit is contained in:
parent
70f044265a
commit
3285ef1ad2
@ -3,6 +3,7 @@ package world.bentobox.bentobox.listeners.flags.settings;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.entity.EntityTeleportEvent;
|
||||
@ -12,28 +13,38 @@ import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.lists.Flags;
|
||||
|
||||
/**
|
||||
* Handles natural mob teleporting.
|
||||
* Handles shulker or enderman teleporting
|
||||
* @author tastybento
|
||||
*/
|
||||
public class MobTeleportListener extends FlagListener {
|
||||
|
||||
/**
|
||||
* Check teleport of Endermen
|
||||
* Check teleport of Endermen or Shulker
|
||||
* @param event EntityTeleportEvent
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
public void onEntityTeleportEvent(EntityTeleportEvent event) {
|
||||
// Only cover shulkers and enderman
|
||||
if (!event.getEntityType().equals(EntityType.ENDERMAN) && !event.getEntityType().equals(EntityType.SHULKER)) {
|
||||
return;
|
||||
}
|
||||
World w = event.getFrom().getWorld();
|
||||
// If not in the right world exit immediately.
|
||||
if (!this.getIWM().inWorld(w)) {
|
||||
// If not in the right world exit immediately and check teleportation is within the same world
|
||||
if (!this.getIWM().inWorld(w) || event.getTo() == null || !event.getFrom().getWorld().equals(event.getTo().getWorld())) {
|
||||
return;
|
||||
}
|
||||
Optional<Island> island = getIslands().getIslandAt(event.getEntity().getLocation());
|
||||
|
||||
if (Boolean.TRUE.equals(island.map(i -> !i.isAllowed(Flags.ENDERMAN_TELEPORT)).orElseGet(
|
||||
if (event.getEntityType().equals(EntityType.ENDERMAN) && Boolean.TRUE.equals(island.map(i -> !i.isAllowed(Flags.ENDERMAN_TELEPORT)).orElseGet(
|
||||
() -> !Flags.ENDERMAN_TELEPORT.isSetForWorld(w)))) {
|
||||
// Enderman teleport is disabled on island or world. Cancel it.
|
||||
event.setCancelled(true);
|
||||
}
|
||||
|
||||
if (event.getEntityType().equals(EntityType.SHULKER) && Boolean.TRUE.equals(island.map(i -> !i.isAllowed(Flags.SHULKER_TELEPORT)).orElseGet(
|
||||
() -> !Flags.SHULKER_TELEPORT.isSetForWorld(w)))) {
|
||||
// Shulker teleport is disabled on island or world. Cancel it.
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
}
|
@ -495,6 +495,14 @@ public final class Flags {
|
||||
*/
|
||||
public static final Flag ENDERMAN_TELEPORT = new Flag.Builder("ENDERMAN_TELEPORT", Material.ENDER_PEARL).type(Type.SETTING)
|
||||
.defaultSetting(true).listener(new MobTeleportListener()).mode(Flag.Mode.ADVANCED).build();
|
||||
/**
|
||||
* If {@code false}, prevents Shulkers from teleporting
|
||||
* Uses same listener as ENDERMAN_TELEPORT
|
||||
* @since 1.22.1
|
||||
*/
|
||||
public static final Flag SHULKER_TELEPORT = new Flag.Builder("SHULKER_TELEPORT", Material.SHULKER_SHELL).type(Type.SETTING)
|
||||
.defaultSetting(true).mode(Flag.Mode.ADVANCED).build();
|
||||
|
||||
|
||||
public static final Flag ENTER_EXIT_MESSAGES = new Flag.Builder("ENTER_EXIT_MESSAGES", Material.DIRT).defaultSetting(true).type(Type.WORLD_SETTING)
|
||||
.listener(new EnterExitListener())
|
||||
|
@ -48,6 +48,8 @@ public class MobTeleportListenerTest {
|
||||
@Mock
|
||||
private Entity enderman;
|
||||
@Mock
|
||||
private Entity shulker;
|
||||
@Mock
|
||||
private Entity other;
|
||||
@Mock
|
||||
private Location from;
|
||||
@ -71,10 +73,12 @@ public class MobTeleportListenerTest {
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
when(im.getIslandAt(any())).thenReturn(Optional.of(island));
|
||||
when(island.isAllowed(Flags.ENDERMAN_TELEPORT)).thenReturn(true);
|
||||
when(island.isAllowed(Flags.SHULKER_TELEPORT)).thenReturn(true);
|
||||
|
||||
when(to.getWorld()).thenReturn(world);
|
||||
when(from.getWorld()).thenReturn(world);
|
||||
when(enderman.getType()).thenReturn(EntityType.ENDERMAN);
|
||||
when(shulker.getType()).thenReturn(EntityType.SHULKER);
|
||||
when(other.getType()).thenReturn(EntityType.AXOLOTL);
|
||||
mtl = new MobTeleportListener();
|
||||
|
||||
@ -145,4 +149,65 @@ public class MobTeleportListenerTest {
|
||||
assertFalse(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.listeners.flags.settings.MobTeleportListener#onEntityTeleportEvent(org.bukkit.event.entity.EntityTeleportEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEntityTeleportEventEndermanNotAllowedButOther() {
|
||||
Flags.ENDERMAN_TELEPORT.setSetting(world, false);
|
||||
Flags.SHULKER_TELEPORT.setSetting(world, false);
|
||||
when(island.isAllowed(Flags.ENDERMAN_TELEPORT)).thenReturn(false);
|
||||
when(island.isAllowed(Flags.SHULKER_TELEPORT)).thenReturn(false);
|
||||
EntityTeleportEvent e = new EntityTeleportEvent(other, from, to);
|
||||
mtl.onEntityTeleportEvent(e);
|
||||
assertFalse(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.listeners.flags.settings.MobTeleportListener#onEntityTeleportEvent(org.bukkit.event.entity.EntityTeleportEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEntityTeleportEventShulkerNotAllowed() {
|
||||
Flags.SHULKER_TELEPORT.setSetting(world, false);
|
||||
when(island.isAllowed(Flags.SHULKER_TELEPORT)).thenReturn(false);
|
||||
EntityTeleportEvent e = new EntityTeleportEvent(shulker, from, to);
|
||||
mtl.onEntityTeleportEvent(e);
|
||||
assertTrue(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.listeners.flags.settings.MobTeleportListener#onEntityTeleportEvent(org.bukkit.event.entity.EntityTeleportEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEntityTeleportEventShulkerNotAllowedWrongWorld() {
|
||||
when(iwm.inWorld(any(World.class))).thenReturn(false);
|
||||
Flags.SHULKER_TELEPORT.setSetting(world, false);
|
||||
when(island.isAllowed(Flags.SHULKER_TELEPORT)).thenReturn(false);
|
||||
EntityTeleportEvent e = new EntityTeleportEvent(shulker, from, to);
|
||||
mtl.onEntityTeleportEvent(e);
|
||||
assertFalse(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.listeners.flags.settings.MobTeleportListener#onEntityTeleportEvent(org.bukkit.event.entity.EntityTeleportEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEntityTeleportEventShulkerNotAllowedNotOnIsland() {
|
||||
when(im.getIslandAt(any())).thenReturn(Optional.empty());
|
||||
Flags.SHULKER_TELEPORT.setSetting(world, false);
|
||||
EntityTeleportEvent e = new EntityTeleportEvent(shulker, from, to);
|
||||
mtl.onEntityTeleportEvent(e);
|
||||
assertFalse(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.listeners.flags.settings.MobTeleportListener#onEntityTeleportEvent(org.bukkit.event.entity.EntityTeleportEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEntityTeleportEventShulkerAllowedDefault() {
|
||||
EntityTeleportEvent e = new EntityTeleportEvent(shulker, from, to);
|
||||
mtl.onEntityTeleportEvent(e);
|
||||
assertFalse(e.isCancelled());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user