Let Citizens handle its own PVP protection.

https://github.com/BentoBoxWorld/BentoBox/issues/1518
This commit is contained in:
tastybento 2020-09-17 08:09:39 -07:00
parent edacd33fd1
commit 5a5435d815
2 changed files with 33 additions and 6 deletions

View File

@ -50,8 +50,8 @@ public class PVPListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onEntityDamage(EntityDamageByEntityEvent e) { public void onEntityDamage(EntityDamageByEntityEvent e) {
if (e.getEntity() instanceof Player && getPlugin().getIWM().inWorld(e.getEntity().getWorld())) { if (e.getEntity() instanceof Player && getPlugin().getIWM().inWorld(e.getEntity().getWorld())) {
// Allow self damage // Allow self damage or NPC attack because Citizens handles its own PVP
if (e.getEntity().equals(e.getDamager())) { if (e.getEntity().equals(e.getDamager()) || e.getEntity().hasMetadata("NPC")) {
return; return;
} }
// Protect visitors // Protect visitors
@ -112,8 +112,8 @@ public class PVPListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onFishing(PlayerFishEvent e) { public void onFishing(PlayerFishEvent e) {
if (e.getCaught() instanceof Player && getPlugin().getIWM().inWorld(e.getCaught().getLocation())) { if (e.getCaught() instanceof Player && getPlugin().getIWM().inWorld(e.getCaught().getLocation())) {
// Allow self-inflicted damage // Allow self-inflicted damage or NPC damage
if (e.getCaught().equals(e.getPlayer())) { if (e.getCaught().equals(e.getPlayer()) || e.getCaught().hasMetadata("NPC")) {
return; return;
} }
// Protect visitors // Protect visitors
@ -154,8 +154,8 @@ public class PVPListener extends FlagListener {
* @return true if PVP should be blocked otherwise false * @return true if PVP should be blocked otherwise false
*/ */
private boolean blockPVP(User user, LivingEntity le, Event e, Flag flag) { private boolean blockPVP(User user, LivingEntity le, Event e, Flag flag) {
// Check for self-inflicted damage // Check for self-inflicted damage or Citizen NPCs
if (le.equals(user.getPlayer())) { if (le.equals(user.getPlayer()) || le.hasMetadata("NPC")) {
return false; return false;
} }
if (le instanceof Player) { if (le instanceof Player) {

View File

@ -273,6 +273,25 @@ public class PVPListenerTest {
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
} }
/**
* Test method for {@link PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/
@Test
public void testOnEntityDamageNPC() {
// Player 2 is an NPC
when(player2.hasMetadata(eq("NPC"))).thenReturn(true);
// PVP is not allowed
when(island.isAllowed(any())).thenReturn(false);
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(player, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
// PVP should be allowed for NPC
assertFalse(e.isCancelled());
verify(player, never()).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
}
/** /**
* Test method for {@link PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}. * Test method for {@link PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/ */
@ -671,6 +690,14 @@ public class PVPListenerTest {
new PVPListener().onFishing(pfe); new PVPListener().onFishing(pfe);
assertFalse(pfe.isCancelled()); assertFalse(pfe.isCancelled());
// Disallow PVP , attack on NPC
when(player2.hasMetadata(eq("NPC"))).thenReturn(true);
when(island.isAllowed(any())).thenReturn(false);
pfe = new PlayerFishEvent(player, player2, hook, null);
new PVPListener().onFishing(pfe);
assertFalse(pfe.isCancelled());
// Wrong world // Wrong world
wrongWorld(); wrongWorld();
pfe = new PlayerFishEvent(player, player2, hook, null); pfe = new PlayerFishEvent(player, player2, hook, null);