mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-26 10:01:41 +01:00
parent
7fbd041be9
commit
2b65543935
@ -0,0 +1,50 @@
|
|||||||
|
package world.bentobox.bentobox.api.events.flags;
|
||||||
|
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Cancellable;
|
||||||
|
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
||||||
|
|
||||||
|
import world.bentobox.bentobox.api.events.BentoBoxEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This event is fired just before damage is prevented to visitors on an island, if that protection is provided.
|
||||||
|
* @author tastybento
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class InvincibleVistorFlagDamageRemovalEvent extends BentoBoxEvent implements Cancellable {
|
||||||
|
private final Player player;
|
||||||
|
private final DamageCause cause;
|
||||||
|
private boolean cancel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This event is fired just before damage is prevented to visitors on an island, if that protection is provided.
|
||||||
|
* @param player player being protected
|
||||||
|
* @param cause damage cause
|
||||||
|
*/
|
||||||
|
public InvincibleVistorFlagDamageRemovalEvent(Player player, DamageCause cause) {
|
||||||
|
this.player = player;
|
||||||
|
this.cause = cause;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean isCancelled() {
|
||||||
|
return cancel;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void setCancelled(boolean cancel) {
|
||||||
|
this.cancel = cancel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the player
|
||||||
|
*/
|
||||||
|
public Player getPlayer() {
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return the cause
|
||||||
|
*/
|
||||||
|
public DamageCause getCause() {
|
||||||
|
return cause;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ import java.util.Arrays;
|
|||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Sound;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
@ -18,6 +19,7 @@ import org.bukkit.event.inventory.ClickType;
|
|||||||
|
|
||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||||
|
import world.bentobox.bentobox.api.events.flags.InvincibleVistorFlagDamageRemovalEvent;
|
||||||
import world.bentobox.bentobox.api.flags.FlagListener;
|
import world.bentobox.bentobox.api.flags.FlagListener;
|
||||||
import world.bentobox.bentobox.api.panels.Panel;
|
import world.bentobox.bentobox.api.panels.Panel;
|
||||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||||
@ -130,13 +132,21 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
|||||||
World world = e.getEntity().getWorld();
|
World world = e.getEntity().getWorld();
|
||||||
if (!(e.getEntity() instanceof Player p)
|
if (!(e.getEntity() instanceof Player p)
|
||||||
|| !getIWM().inWorld(world)
|
|| !getIWM().inWorld(world)
|
||||||
|| e.getEntity().hasMetadata("NPC")
|
|| p.hasMetadata("NPC")
|
||||||
|| !getIWM().getIvSettings(world).contains(e.getCause().name())
|
|| !getIWM().getIvSettings(world).contains(e.getCause().name())
|
||||||
|| getIslands().userIsOnIsland(world, User.getInstance(e.getEntity()))
|
|| getIslands().userIsOnIsland(world, User.getInstance(p))
|
||||||
|| PVPAllowed(p.getLocation())
|
|| PVPAllowed(p.getLocation())
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Fire event
|
||||||
|
InvincibleVistorFlagDamageRemovalEvent event = new InvincibleVistorFlagDamageRemovalEvent(p, e.getCause());
|
||||||
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
|
if (event.isCancelled()) {
|
||||||
|
// Give others a chance to ignore the protection
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Player is a visitor and should be protected from damage
|
// Player is a visitor and should be protected from damage
|
||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
// Handle the void - teleport player back to island in a safe spot
|
// Handle the void - teleport player back to island in a safe spot
|
||||||
@ -169,12 +179,12 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
|||||||
World world = e.getEntity().getWorld();
|
World world = e.getEntity().getWorld();
|
||||||
|
|
||||||
if (!(e.getTarget() instanceof Player p) ||
|
if (!(e.getTarget() instanceof Player p) ||
|
||||||
!this.getIWM().inWorld(world) ||
|
!this.getIWM().inWorld(world) ||
|
||||||
e.getTarget().hasMetadata("NPC") ||
|
e.getTarget().hasMetadata("NPC") ||
|
||||||
this.getIslands().userIsOnIsland(world, User.getInstance(e.getTarget())) ||
|
this.getIslands().userIsOnIsland(world, User.getInstance(e.getTarget())) ||
|
||||||
this.PVPAllowed(p.getLocation()) ||
|
this.PVPAllowed(p.getLocation()) ||
|
||||||
e.getReason() == EntityTargetEvent.TargetReason.TARGET_DIED ||
|
e.getReason() == EntityTargetEvent.TargetReason.TARGET_DIED ||
|
||||||
!this.getIWM().getIvSettings(world).contains(DamageCause.ENTITY_ATTACK.name()))
|
!this.getIWM().getIvSettings(world).contains(DamageCause.ENTITY_ATTACK.name()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -182,5 +192,6 @@ public class InvincibleVisitorsListener extends FlagListener implements ClickHan
|
|||||||
// Cancel targeting event.
|
// Cancel targeting event.
|
||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +36,7 @@ import org.bukkit.inventory.Inventory;
|
|||||||
import org.bukkit.inventory.ItemFactory;
|
import org.bukkit.inventory.ItemFactory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.bukkit.plugin.PluginManager;
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@ -51,6 +52,7 @@ import org.powermock.reflect.Whitebox;
|
|||||||
|
|
||||||
import world.bentobox.bentobox.BentoBox;
|
import world.bentobox.bentobox.BentoBox;
|
||||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||||
|
import world.bentobox.bentobox.api.events.flags.InvincibleVistorFlagDamageRemovalEvent;
|
||||||
import world.bentobox.bentobox.api.flags.Flag;
|
import world.bentobox.bentobox.api.flags.Flag;
|
||||||
import world.bentobox.bentobox.api.panels.Panel;
|
import world.bentobox.bentobox.api.panels.Panel;
|
||||||
import world.bentobox.bentobox.api.panels.PanelItem;
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
||||||
@ -84,6 +86,8 @@ public class InvincibleVisitorsListenerTest {
|
|||||||
private Location location;
|
private Location location;
|
||||||
@Mock
|
@Mock
|
||||||
private World world;
|
private World world;
|
||||||
|
@Mock
|
||||||
|
private PluginManager pim;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
@ -169,6 +173,7 @@ public class InvincibleVisitorsListenerTest {
|
|||||||
ItemMeta imeta = mock(ItemMeta.class);
|
ItemMeta imeta = mock(ItemMeta.class);
|
||||||
when(itemF.getItemMeta(any())).thenReturn(imeta);
|
when(itemF.getItemMeta(any())).thenReturn(imeta);
|
||||||
when(Bukkit.getItemFactory()).thenReturn(itemF);
|
when(Bukkit.getItemFactory()).thenReturn(itemF);
|
||||||
|
when(Bukkit.getPluginManager()).thenReturn(pim);
|
||||||
|
|
||||||
Inventory top = mock(Inventory.class);
|
Inventory top = mock(Inventory.class);
|
||||||
when(top.getSize()).thenReturn(9);
|
when(top.getSize()).thenReturn(9);
|
||||||
@ -279,6 +284,7 @@ public class InvincibleVisitorsListenerTest {
|
|||||||
listener.onVisitorGetDamage(e);
|
listener.onVisitorGetDamage(e);
|
||||||
assertTrue(e.isCancelled());
|
assertTrue(e.isCancelled());
|
||||||
verify(player, never()).setGameMode(eq(GameMode.SPECTATOR));
|
verify(player, never()).setGameMode(eq(GameMode.SPECTATOR));
|
||||||
|
verify(pim).callEvent(any(InvincibleVistorFlagDamageRemovalEvent.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -297,6 +303,7 @@ public class InvincibleVisitorsListenerTest {
|
|||||||
// Player should be teleported to this island
|
// Player should be teleported to this island
|
||||||
listener.onVisitorGetDamage(e);
|
listener.onVisitorGetDamage(e);
|
||||||
assertTrue(e.isCancelled());
|
assertTrue(e.isCancelled());
|
||||||
|
verify(pim).callEvent(any(InvincibleVistorFlagDamageRemovalEvent.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -307,6 +314,7 @@ public class InvincibleVisitorsListenerTest {
|
|||||||
// Player should die
|
// Player should die
|
||||||
listener.onVisitorGetDamage(e);
|
listener.onVisitorGetDamage(e);
|
||||||
assertFalse(e.isCancelled());
|
assertFalse(e.isCancelled());
|
||||||
|
verify(pim).callEvent(any(InvincibleVistorFlagDamageRemovalEvent.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -320,5 +328,6 @@ public class InvincibleVisitorsListenerTest {
|
|||||||
listener.onVisitorGetDamage(e);
|
listener.onVisitorGetDamage(e);
|
||||||
assertTrue(e.isCancelled());
|
assertTrue(e.isCancelled());
|
||||||
verify(im).homeTeleportAsync(any(), eq(player));
|
verify(im).homeTeleportAsync(any(), eq(player));
|
||||||
|
verify(pim).callEvent(any(InvincibleVistorFlagDamageRemovalEvent.class));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user