Move #isNpc method to PlayerUtils

- After dropping our hook to CombatTagPlus it is not relevant for it to be in PluginHooksService anymore
This commit is contained in:
ljacqu 2017-10-21 12:08:23 +02:00
parent 58657f5d3f
commit 4e8deec258
11 changed files with 49 additions and 51 deletions

View File

@ -9,8 +9,8 @@ import fr.xephi.authme.process.register.executors.ApiPasswordRegisterParams;
import fr.xephi.authme.process.register.executors.RegistrationMethod; import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.util.PlayerUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -35,7 +35,6 @@ public class NewAPI {
private static NewAPI singleton; private static NewAPI singleton;
private final AuthMe plugin; private final AuthMe plugin;
private final PluginHookService pluginHookService;
private final DataSource dataSource; private final DataSource dataSource;
private final PasswordSecurity passwordSecurity; private final PasswordSecurity passwordSecurity;
private final Management management; private final Management management;
@ -46,10 +45,9 @@ public class NewAPI {
* Constructor for NewAPI. * Constructor for NewAPI.
*/ */
@Inject @Inject
NewAPI(AuthMe plugin, PluginHookService pluginHookService, DataSource dataSource, PasswordSecurity passwordSecurity, NewAPI(AuthMe plugin, DataSource dataSource, PasswordSecurity passwordSecurity,
Management management, ValidationService validationService, PlayerCache playerCache) { Management management, ValidationService validationService, PlayerCache playerCache) {
this.plugin = plugin; this.plugin = plugin;
this.pluginHookService = pluginHookService;
this.dataSource = dataSource; this.dataSource = dataSource;
this.passwordSecurity = passwordSecurity; this.passwordSecurity = passwordSecurity;
this.management = management; this.management = management;
@ -108,7 +106,7 @@ public class NewAPI {
* @return true if the player is an npc * @return true if the player is an npc
*/ */
public boolean isNPC(Player player) { public boolean isNPC(Player player) {
return pluginHookService.isNpc(player); return PlayerUtils.isNpc(player);
} }
/** /**

View File

@ -10,8 +10,8 @@ import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.GeoIpService; import fr.xephi.authme.service.GeoIpService;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.util.PlayerUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -33,7 +33,6 @@ public class AuthMeApi {
private static AuthMeApi singleton; private static AuthMeApi singleton;
private final AuthMe plugin; private final AuthMe plugin;
private final PluginHookService pluginHookService;
private final DataSource dataSource; private final DataSource dataSource;
private final PasswordSecurity passwordSecurity; private final PasswordSecurity passwordSecurity;
private final Management management; private final Management management;
@ -45,11 +44,9 @@ public class AuthMeApi {
* Constructor for AuthMeApi. * Constructor for AuthMeApi.
*/ */
@Inject @Inject
AuthMeApi(AuthMe plugin, PluginHookService pluginHookService, DataSource dataSource, PlayerCache playerCache, AuthMeApi(AuthMe plugin, DataSource dataSource, PlayerCache playerCache, PasswordSecurity passwordSecurity,
PasswordSecurity passwordSecurity, Management management, ValidationService validationService, Management management, ValidationService validationService, GeoIpService geoIpService) {
GeoIpService geoIpService) {
this.plugin = plugin; this.plugin = plugin;
this.pluginHookService = pluginHookService;
this.dataSource = dataSource; this.dataSource = dataSource;
this.passwordSecurity = passwordSecurity; this.passwordSecurity = passwordSecurity;
this.management = management; this.management = management;
@ -109,7 +106,7 @@ public class AuthMeApi {
* @return true if the player is an npc * @return true if the player is an npc
*/ */
public boolean isNpc(Player player) { public boolean isNpc(Player player) {
return pluginHookService.isNpc(player); return PlayerUtils.isNpc(player);
} }
/** /**

View File

@ -5,11 +5,11 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboService; import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.PlayerUtils;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -31,8 +31,6 @@ public class OnShutdownPlayerSaver {
@Inject @Inject
private SpawnLoader spawnLoader; private SpawnLoader spawnLoader;
@Inject @Inject
private PluginHookService pluginHookService;
@Inject
private PlayerCache playerCache; private PlayerCache playerCache;
@Inject @Inject
private LimboService limboService; private LimboService limboService;
@ -51,7 +49,7 @@ public class OnShutdownPlayerSaver {
private void savePlayer(Player player) { private void savePlayer(Player player) {
final String name = player.getName().toLowerCase(); final String name = player.getName().toLowerCase();
if (pluginHookService.isNpc(player) || validationService.isUnrestricted(name)) { if (PlayerUtils.isNpc(player) || validationService.isUnrestricted(name)) {
return; return;
} }
if (limboService.hasLimboPlayer(name)) { if (limboService.hasLimboPlayer(name)) {

View File

@ -2,11 +2,11 @@ package fr.xephi.authme.listener;
import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.initialization.SettingsDependent; import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.util.PlayerUtils;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityEvent; import org.bukkit.event.entity.EntityEvent;
@ -20,17 +20,15 @@ import javax.inject.Inject;
class ListenerService implements SettingsDependent { class ListenerService implements SettingsDependent {
private final DataSource dataSource; private final DataSource dataSource;
private final PluginHookService pluginHookService;
private final PlayerCache playerCache; private final PlayerCache playerCache;
private final ValidationService validationService; private final ValidationService validationService;
private boolean isRegistrationForced; private boolean isRegistrationForced;
@Inject @Inject
ListenerService(Settings settings, DataSource dataSource, PluginHookService pluginHookService, ListenerService(Settings settings, DataSource dataSource, PlayerCache playerCache,
PlayerCache playerCache, ValidationService validationService) { ValidationService validationService) {
this.dataSource = dataSource; this.dataSource = dataSource;
this.pluginHookService = pluginHookService;
this.playerCache = playerCache; this.playerCache = playerCache;
this.validationService = validationService; this.validationService = validationService;
reload(settings); reload(settings);
@ -79,7 +77,7 @@ 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(Player player) { public boolean shouldCancelEvent(Player player) {
return player != null && !checkAuth(player.getName()) && !pluginHookService.isNpc(player); return player != null && !checkAuth(player.getName()) && !PlayerUtils.isNpc(player);
} }
@Override @Override

View File

@ -76,16 +76,6 @@ public class PluginHookService {
return null; return null;
} }
/**
* Checks whether the player is an NPC.
*
* @param player The player to process
* @return True if player is NPC, false otherwise
*/
public boolean isNpc(Player player) {
return player.hasMetadata("NPC");
}
// ------ // ------
// "Is plugin available" methods // "Is plugin available" methods

View File

@ -39,4 +39,15 @@ public final class PlayerUtils {
public static String getPlayerIp(Player p) { public static String getPlayerIp(Player p) {
return p.getAddress().getAddress().getHostAddress(); return p.getAddress().getAddress().getHostAddress();
} }
/**
* Returns if the player is an NPC or not.
*
* @param player The player to check
*
* @return True if the player is an NPC, false otherwise
*/
public static boolean isNpc(Player player) {
return player.hasMetadata("NPC");
}
} }

View File

@ -7,7 +7,6 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.service.ValidationService;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -43,8 +42,6 @@ public class NewAPITest {
@InjectMocks @InjectMocks
private NewAPI api; private NewAPI api;
@Mock
private PluginHookService pluginHookService;
@Mock @Mock
private ValidationService validationService; private ValidationService validationService;
@Mock @Mock
@ -84,13 +81,14 @@ public class NewAPITest {
public void shouldReturnIfPlayerIsNpc() { public void shouldReturnIfPlayerIsNpc() {
// given // given
Player player = mock(Player.class); Player player = mock(Player.class);
given(pluginHookService.isNpc(player)).willReturn(true); given(player.hasMetadata("NPC")).willReturn(true);
// when // when
boolean result = api.isNPC(player); boolean result = api.isNPC(player);
// then // then
assertThat(result, equalTo(true)); assertThat(result, equalTo(true));
verify(player).hasMetadata("NPC");
} }
@Test @Test

View File

@ -6,7 +6,6 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.service.ValidationService;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -43,8 +42,6 @@ public class AuthMeApiTest {
@InjectMocks @InjectMocks
private AuthMeApi api; private AuthMeApi api;
@Mock
private PluginHookService pluginHookService;
@Mock @Mock
private ValidationService validationService; private ValidationService validationService;
@Mock @Mock
@ -84,13 +81,14 @@ public class AuthMeApiTest {
public void shouldReturnIfPlayerIsNpc() { public void shouldReturnIfPlayerIsNpc() {
// given // given
Player player = mock(Player.class); Player player = mock(Player.class);
given(pluginHookService.isNpc(player)).willReturn(true); given(player.hasMetadata("NPC")).willReturn(true);
// when // when
boolean result = api.isNpc(player); boolean result = api.isNpc(player);
// then // then
assertThat(result, equalTo(true)); assertThat(result, equalTo(true));
verify(player).hasMetadata("NPC");
} }
@Test @Test

View File

@ -5,10 +5,9 @@ import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed; import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.service.PluginHookService; import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@ -40,9 +39,6 @@ public class ListenerServiceTest {
@Mock @Mock
private DataSource dataSource; private DataSource dataSource;
@Mock
private PluginHookService pluginHookService;
@Mock @Mock
private PlayerCache playerCache; private PlayerCache playerCache;
@ -161,14 +157,14 @@ public class ListenerServiceTest {
Player player = mockPlayerWithName(playerName); Player player = mockPlayerWithName(playerName);
EntityEvent event = mock(EntityEvent.class); EntityEvent event = mock(EntityEvent.class);
given(event.getEntity()).willReturn(player); given(event.getEntity()).willReturn(player);
given(pluginHookService.isNpc(player)).willReturn(true); given(player.hasMetadata("NPC")).willReturn(true);
// when // when
boolean result = listenerService.shouldCancelEvent(event); boolean result = listenerService.shouldCancelEvent(event);
// then // then
assertThat(result, equalTo(false)); assertThat(result, equalTo(false));
verify(pluginHookService).isNpc(player); verify(player).hasMetadata("NPC");
} }
@Test @Test
@ -215,7 +211,6 @@ public class ListenerServiceTest {
assertThat(result, equalTo(true)); assertThat(result, equalTo(true));
verify(playerCache).isAuthenticated(playerName); verify(playerCache).isAuthenticated(playerName);
verifyZeroInteractions(dataSource); verifyZeroInteractions(dataSource);
verify(pluginHookService).isNpc(player);
} }
private static Player mockPlayerWithName(String name) { private static Player mockPlayerWithName(String name) {

View File

@ -58,8 +58,6 @@ public class PluginHookServiceTest {
assertThat(pluginHookService.isEssentialsAvailable(), equalTo(true)); assertThat(pluginHookService.isEssentialsAvailable(), equalTo(true));
} }
// Note ljacqu 20160312: Cannot test with CombatTagPlus because its class is declared final
@Test @Test
public void shouldHookIntoEssentialsAtInitialization() { public void shouldHookIntoEssentialsAtInitialization() {
// given // given

View File

@ -70,4 +70,21 @@ public class PlayerUtilsTest {
// given / when / then // given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(PlayerUtils.class); TestHelper.validateHasOnlyPrivateEmptyConstructor(PlayerUtils.class);
} }
@Test
public void shouldCheckIfIsNpc() {
// given
Player player1 = mock(Player.class);
given(player1.hasMetadata("NPC")).willReturn(false);
Player player2 = mock(Player.class);
given(player2.hasMetadata("NPC")).willReturn(true);
// when
boolean result1 = PlayerUtils.isNpc(player1);
boolean result2 = PlayerUtils.isNpc(player2);
// then
assertThat(result1, equalTo(false));
assertThat(result2, equalTo(true));
}
} }