#1743 Stop mobs from targetting players when not authenticated

This commit is contained in:
ljacqu 2019-06-22 20:54:01 +02:00
parent d1b6161687
commit ff2f43bdc5
3 changed files with 37 additions and 6 deletions

View File

@ -56,7 +56,7 @@ public class EntityListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onEntityTarget(EntityTargetEvent event) { public void onEntityTarget(EntityTargetEvent event) {
if (listenerService.shouldCancelEvent(event)) { if (listenerService.shouldCancelEvent(event.getTarget())) {
event.setTarget(null); event.setTarget(null);
event.setCancelled(true); event.setCancelled(true);
} }

View File

@ -52,11 +52,11 @@ class ListenerService implements SettingsDependent {
* @return true if the associated event should be canceled, false otherwise * @return true if the associated event should be canceled, false otherwise
*/ */
public boolean shouldCancelEvent(Entity entity) { public boolean shouldCancelEvent(Entity entity) {
if (entity == null || !(entity instanceof Player)) { if (entity instanceof Player) {
return false; Player player = (Player) entity;
return shouldCancelEvent(player);
} }
Player player = (Player) entity; return false;
return shouldCancelEvent(player);
} }
/** /**

View File

@ -45,7 +45,6 @@ public class EntityListenerTest {
@Test @Test
public void shouldHandleSimpleEvents() { public void shouldHandleSimpleEvents() {
withServiceMock(listenerService) withServiceMock(listenerService)
.check(listener::onEntityTarget, EntityTargetEvent.class)
.check(listener::onFoodLevelChange, FoodLevelChangeEvent.class) .check(listener::onFoodLevelChange, FoodLevelChangeEvent.class)
.check(listener::onShoot, EntityShootBowEvent.class) .check(listener::onShoot, EntityShootBowEvent.class)
.check(listener::onEntityInteract, EntityInteractEvent.class) .check(listener::onEntityInteract, EntityInteractEvent.class)
@ -216,4 +215,36 @@ public class EntityListenerTest {
verify(listenerService).shouldCancelEvent(shooter); verify(listenerService).shouldCancelEvent(shooter);
assertThat(event.isCancelled(), equalTo(true)); assertThat(event.isCancelled(), equalTo(true));
} }
@Test
public void shouldCancelEntityTargetEvent() {
// given
EntityTargetEvent event = mock(EntityTargetEvent.class);
Entity target = mock(Entity.class);
given(event.getTarget()).willReturn(target);
given(listenerService.shouldCancelEvent(target)).willReturn(true);
// when
listener.onEntityTarget(event);
// then
verify(listenerService).shouldCancelEvent(target);
verify(event).setCancelled(true);
}
@Test
public void shouldNotCancelEntityTargetEvent() {
// given
EntityTargetEvent event = mock(EntityTargetEvent.class);
Entity target = mock(Entity.class);
given(event.getTarget()).willReturn(target);
given(listenerService.shouldCancelEvent(target)).willReturn(false);
// when
listener.onEntityTarget(event);
// then
verify(listenerService).shouldCancelEvent(target);
verify(event, only()).getTarget();
}
} }