Fixes damage to wandering traders.

https://github.com/BentoBoxWorld/BentoBox/issues/1029
This commit is contained in:
tastybento 2019-11-14 18:05:55 -08:00
parent f67baf6501
commit 2c8739a48b
5 changed files with 111 additions and 12 deletions

View File

@ -1,11 +1,11 @@
package world.bentobox.bentobox.listeners.flags.protection;
import org.bukkit.Material;
import org.bukkit.entity.AbstractVillager;
import org.bukkit.entity.Animals;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Boat;
import org.bukkit.entity.Vehicle;
import org.bukkit.entity.Villager;
import org.bukkit.entity.minecart.RideableMinecart;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -47,7 +47,7 @@ public class EntityInteractListener extends FlagListener {
}
}
// Villager trading
else if (e.getRightClicked() instanceof Villager) {
else if (e.getRightClicked() instanceof AbstractVillager) {
// Check naming and check trading
checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.TRADING);
if (e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.NAME_TAG)) {

View File

@ -5,6 +5,7 @@ import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Material;
import org.bukkit.entity.AbstractVillager;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
@ -13,7 +14,6 @@ import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Parrot;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
@ -53,7 +53,7 @@ public class HurtingListener extends FlagListener {
// Mobs being hurt
if (Util.isPassiveEntity(e.getEntity())) {
respond(e, e.getDamager(), Flags.HURT_ANIMALS);
} else if (e.getEntity() instanceof Villager) {
} else if (e.getEntity() instanceof AbstractVillager) {
respond(e, e.getDamager(), Flags.HURT_VILLAGERS);
} else if (Util.isHostileEntity(e.getEntity())) {
respond(e, e.getDamager(), Flags.HURT_MONSTERS);
@ -91,7 +91,7 @@ public class HurtingListener extends FlagListener {
if ((Util.isPassiveEntity(e.getCaught()) && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_ANIMALS))
|| (Util.isHostileEntity(e.getCaught()) && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_MONSTERS))
|| (e.getCaught() instanceof Villager && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_VILLAGERS))) {
|| (e.getCaught() instanceof AbstractVillager && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_VILLAGERS))) {
e.getHook().remove();
}
@ -145,7 +145,7 @@ public class HurtingListener extends FlagListener {
}
// Villagers being hurt
if (entity instanceof Villager && !checkIsland(e, attacker, entity.getLocation(), Flags.HURT_VILLAGERS)) {
if (entity instanceof AbstractVillager && !checkIsland(e, attacker, entity.getLocation(), Flags.HURT_VILLAGERS)) {
for (PotionEffect effect : e.getPotion().getEffects()) {
entity.removePotionEffect(effect.getType());
}
@ -198,7 +198,7 @@ public class HurtingListener extends FlagListener {
checkIsland(e, attacker, entity.getLocation(), Flags.HURT_ANIMALS);
}
// Villagers being hurt
if (entity instanceof Villager) {
if (entity instanceof AbstractVillager) {
checkIsland(e, attacker, entity.getLocation(), Flags.HURT_VILLAGERS);
}

View File

@ -1,10 +1,15 @@
package world.bentobox.bentobox.listeners.flags.protection;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.Optional;
@ -21,6 +26,7 @@ import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep;
import org.bukkit.entity.Villager;
import org.bukkit.entity.WanderingTrader;
import org.bukkit.entity.minecart.RideableMinecart;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
@ -329,6 +335,63 @@ public class EntityInteractListenerTest {
verify(notifier).notify(any(), eq("protection.protected"));
assertTrue(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractEntityEvent)}.
*/
@Test
public void testOnPlayerInteractEntityWanderingTraderNoInteraction() {
clickedEntity = mock(WanderingTrader.class);
when(clickedEntity.getLocation()).thenReturn(location);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand);
eil.onPlayerInteractEntity(e);
verify(notifier, times(2)).notify(any(), eq("protection.protected"));
assertTrue(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractAtEntityEvent)}.
*/
@Test
public void testOnPlayerInteractAtEntityWanderingTraderAllowed() {
when(island.isAllowed(any(), any())).thenReturn(true);
clickedEntity = mock(WanderingTrader.class);
when(clickedEntity.getLocation()).thenReturn(location);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand);
eil.onPlayerInteractEntity(e);
verify(notifier, never()).notify(any(), eq("protection.protected"));
assertFalse(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractAtEntityEvent)}.
*/
@Test
public void testOnPlayerInteractEntityNamingWanderingTraderAllowedNoTrading() {
when(island.isAllowed(any(), eq(Flags.TRADING))).thenReturn(false);
when(island.isAllowed(any(), eq(Flags.NAME_TAG))).thenReturn(true);
clickedEntity = mock(WanderingTrader.class);
when(clickedEntity.getLocation()).thenReturn(location);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand);
eil.onPlayerInteractEntity(e);
verify(notifier).notify(any(), eq("protection.protected"));
assertTrue(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractAtEntityEvent)}.
*/
@Test
public void testOnPlayerInteractEntityNamingWanderingTraderAllowedTradingNoNaming() {
when(island.isAllowed(any(), eq(Flags.TRADING))).thenReturn(true);
when(island.isAllowed(any(), eq(Flags.NAME_TAG))).thenReturn(false);
clickedEntity = mock(WanderingTrader.class);
when(clickedEntity.getLocation()).thenReturn(location);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(player, clickedEntity, hand);
eil.onPlayerInteractEntity(e);
verify(notifier).notify(any(), eq("protection.protected"));
assertTrue(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractEntityEvent)}.

View File

@ -27,6 +27,7 @@ import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Villager;
import org.bukkit.entity.WanderingTrader;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerFishEvent.State;
@ -330,6 +331,22 @@ public class HurtingListenerTest {
// Verify
verify(notifier).notify(eq(user), eq("protection.protected"));
}
/**
* Test method for {@link HurtingListener#onFishing(org.bukkit.event.player.PlayerFishEvent)}.
*/
@Test
public void testOnFishingDisallowWanderingTraderCatching() {
WanderingTrader entity = mock(WanderingTrader.class);
when(entity.getLocation()).thenReturn(location);
State state = State.CAUGHT_ENTITY;
PlayerFishEvent e = new PlayerFishEvent(player, entity, hookEntity, state);
HurtingListener hl = new HurtingListener();
hl.onFishing(e);
// Verify
verify(notifier).notify(eq(user), eq("protection.protected"));
}
/**
* Test method for {@link HurtingListener#onFishing(org.bukkit.event.player.PlayerFishEvent)}.
@ -347,6 +364,24 @@ public class HurtingListenerTest {
// Verify
verify(notifier, never()).notify(eq(user), eq("protection.protected"));
}
/**
* Test method for {@link HurtingListener#onFishing(org.bukkit.event.player.PlayerFishEvent)}.
*/
@Test
public void testOnFishingAllowWanderingTraderCatching() {
WanderingTrader entity = mock(WanderingTrader.class);
when(entity.getLocation()).thenReturn(location);
State state = State.CAUGHT_ENTITY;
PlayerFishEvent e = new PlayerFishEvent(player, entity, hookEntity, state);
HurtingListener hl = new HurtingListener();
// Allow
when(island.isAllowed(any(), any())).thenReturn(true);
hl.onFishing(e);
// Verify
verify(notifier, never()).notify(eq(user), eq("protection.protected"));
}
/**
* Test method for {@link HurtingListener#onPlayerFeedParrots(org.bukkit.event.player.PlayerInteractEntityEvent)}.
*/

View File

@ -29,6 +29,7 @@ import org.bukkit.block.ShulkerBox;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.entity.WanderingTrader;
import org.bukkit.entity.minecart.StorageMinecart;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction;
@ -76,7 +77,7 @@ public class InventoryListenerTest {
private final static List<Class<?>> HOLDERS = Arrays.asList(Horse.class, Chest.class,ShulkerBox.class, StorageMinecart.class,
Dispenser.class,
Dropper.class, Hopper.class, Furnace.class, BrewingStand.class,
Villager.class);
Villager.class, WanderingTrader.class);
private Location location;
private BentoBox plugin;