Skip to content

Commit

Permalink
Adds InvincibleVisitorFlagDamageRemovalEvent and tests.
Browse files Browse the repository at this point in the history
Resolves #2196
  • Loading branch information
tastybento committed Oct 8, 2023
1 parent 7fbd041 commit 2b65543
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Comparator;
import java.util.Objects;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
Expand All @@ -18,6 +19,7 @@

import world.bentobox.bentobox.BentoBox;
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.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
Expand Down Expand Up @@ -130,13 +132,21 @@ public void onVisitorGetDamage(EntityDamageEvent e) {
World world = e.getEntity().getWorld();
if (!(e.getEntity() instanceof Player p)
|| !getIWM().inWorld(world)
|| e.getEntity().hasMetadata("NPC")
|| p.hasMetadata("NPC")
|| !getIWM().getIvSettings(world).contains(e.getCause().name())
|| getIslands().userIsOnIsland(world, User.getInstance(e.getEntity()))
|| getIslands().userIsOnIsland(world, User.getInstance(p))
|| PVPAllowed(p.getLocation())
) {
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
e.setCancelled(true);
// Handle the void - teleport player back to island in a safe spot
Expand Down Expand Up @@ -169,18 +179,19 @@ public void onVisitorTargeting(EntityTargetLivingEntityEvent e)
World world = e.getEntity().getWorld();

if (!(e.getTarget() instanceof Player p) ||
!this.getIWM().inWorld(world) ||
e.getTarget().hasMetadata("NPC") ||
this.getIslands().userIsOnIsland(world, User.getInstance(e.getTarget())) ||
this.PVPAllowed(p.getLocation()) ||
e.getReason() == EntityTargetEvent.TargetReason.TARGET_DIED ||
!this.getIWM().getIvSettings(world).contains(DamageCause.ENTITY_ATTACK.name()))
!this.getIWM().inWorld(world) ||
e.getTarget().hasMetadata("NPC") ||
this.getIslands().userIsOnIsland(world, User.getInstance(e.getTarget())) ||
this.PVPAllowed(p.getLocation()) ||
e.getReason() == EntityTargetEvent.TargetReason.TARGET_DIED ||
!this.getIWM().getIvSettings(world).contains(DamageCause.ENTITY_ATTACK.name()))
{
return;
}

// Cancel targeting event.
e.setCancelled(true);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
Expand All @@ -51,6 +52,7 @@

import world.bentobox.bentobox.BentoBox;
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.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
Expand Down Expand Up @@ -84,6 +86,8 @@ public class InvincibleVisitorsListenerTest {
private Location location;
@Mock
private World world;
@Mock
private PluginManager pim;

/**
*/
Expand Down Expand Up @@ -169,6 +173,7 @@ public void setUp() throws Exception {
ItemMeta imeta = mock(ItemMeta.class);
when(itemF.getItemMeta(any())).thenReturn(imeta);
when(Bukkit.getItemFactory()).thenReturn(itemF);
when(Bukkit.getPluginManager()).thenReturn(pim);

Inventory top = mock(Inventory.class);
when(top.getSize()).thenReturn(9);
Expand Down Expand Up @@ -279,6 +284,7 @@ public void testOnVisitorGetDamageNotVoid() {
listener.onVisitorGetDamage(e);
assertTrue(e.isCancelled());
verify(player, never()).setGameMode(eq(GameMode.SPECTATOR));
verify(pim).callEvent(any(InvincibleVistorFlagDamageRemovalEvent.class));
}

@Test
Expand All @@ -297,6 +303,7 @@ public void testOnVisitorGetDamageVoidIslandHere() {
// Player should be teleported to this island
listener.onVisitorGetDamage(e);
assertTrue(e.isCancelled());
verify(pim).callEvent(any(InvincibleVistorFlagDamageRemovalEvent.class));
}

@Test
Expand All @@ -307,6 +314,7 @@ public void testOnVisitorGetDamageVoidNoIslandHerePlayerHasNoIsland() {
// Player should die
listener.onVisitorGetDamage(e);
assertFalse(e.isCancelled());
verify(pim).callEvent(any(InvincibleVistorFlagDamageRemovalEvent.class));
}

@Test
Expand All @@ -320,5 +328,6 @@ public void testOnVisitorGetDamageVoidPlayerHasIsland() {
listener.onVisitorGetDamage(e);
assertTrue(e.isCancelled());
verify(im).homeTeleportAsync(any(), eq(player));
verify(pim).callEvent(any(InvincibleVistorFlagDamageRemovalEvent.class));
}
}

0 comments on commit 2b65543

Please sign in to comment.