#1531 Move spigot detection to BukkitService (#1534)

This commit is contained in:
ljacqu 2018-03-19 23:08:48 +01:00 committed by HexelDev
parent 250bd0d148
commit a1a909c01d
4 changed files with 43 additions and 29 deletions

View File

@ -12,6 +12,7 @@ import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.scheduler.BukkitRunnable;
@ -25,6 +26,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
@ -376,4 +378,19 @@ public class BukkitService implements SettingsDependent {
public BanEntry banIp(String ip, String reason, Date expires, String source) {
return Bukkit.getServer().getBanList(BanList.Type.IP).addBan(ip, reason, expires, source);
}
/**
* Returns an optional with a boolean indicating whether bungeecord is enabled or not if the
* server implementation is Spigot. Otherwise returns an empty optional.
*
* @return Optional with configuration value for Spigot, empty optional otherwise
*/
public Optional<Boolean> isBungeeCordConfiguredForSpigot() {
try {
YamlConfiguration spigotConfig = Bukkit.spigot().getConfig();
return Optional.of(spigotConfig.getBoolean("settings.bungeecord"));
} catch (NoSuchMethodError e) {
return Optional.empty();
}
}
}

View File

@ -4,15 +4,15 @@ import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.Argon2;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit;
import javax.inject.Inject;
import java.util.Optional;
/**
* Logs warning messages in cases where the configured values suggest a misconfiguration.
@ -29,6 +29,9 @@ public class SettingsWarner {
@Inject
private AuthMe authMe;
@Inject
private BukkitService bukkitService;
SettingsWarner() {
}
@ -54,7 +57,7 @@ public class SettingsWarner {
}
// Warn if spigot.yml has settings.bungeecord set to true but config.yml has Hooks.bungeecord set to false
if (Utils.isSpigot() && Bukkit.spigot().getConfig().getBoolean("settings.bungeecord")
if (isTrue(bukkitService.isBungeeCordConfiguredForSpigot())
&& !settings.getProperty(HooksSettings.BUNGEECORD)) {
ConsoleLogger.warning("Note: Hooks.bungeecord is set to false but your server appears to be running in"
+ " bungeecord mode (see your spigot.yml). In order to allow the datasource caching and the"
@ -69,4 +72,8 @@ public class SettingsWarner {
authMe.stopOrUnload();
}
}
private static boolean isTrue(Optional<Boolean> value) {
return value.isPresent() && value.get();
}
}

View File

@ -1,7 +1,6 @@
package fr.xephi.authme.util;
import fr.xephi.authme.ConsoleLogger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
@ -23,20 +22,6 @@ public final class Utils {
private Utils() {
}
/**
* Returns if the running server instance is craftbukkit or spigot based.
*
* @return true if the running server instance is spigot-based.
*/
public static boolean isSpigot() {
try {
Bukkit.spigot();
return true;
} catch (NoSuchMethodError e) {
return false;
}
}
/**
* Compile Pattern sneaky without throwing Exception.
*

View File

@ -1,18 +1,21 @@
package fr.xephi.authme.settings;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Optional;
import java.util.logging.Logger;
import static org.mockito.ArgumentMatchers.anyString;
@ -27,12 +30,18 @@ import static org.mockito.internal.verification.VerificationModeFactory.times;
@RunWith(MockitoJUnitRunner.class)
public class SettingsWarnerTest {
@InjectMocks
private SettingsWarner settingsWarner;
@Mock
private Settings settings;
@Mock
private AuthMe authMe;
@Mock
private BukkitService bukkitService;
@Test
public void shouldLogWarnings() {
// given
@ -43,12 +52,14 @@ public class SettingsWarnerTest {
given(settings.getProperty(PluginSettings.SESSIONS_ENABLED)).willReturn(true);
given(settings.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(-5);
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.BCRYPT);
given(settings.getProperty(HooksSettings.BUNGEECORD)).willReturn(false);
given(bukkitService.isBungeeCordConfiguredForSpigot()).willReturn(Optional.of(true));
// when
createSettingsWarner().logWarningsForMisconfigurations();
settingsWarner.logWarningsForMisconfigurations();
// then
verify(logger, times(3)).warning(anyString());
verify(logger, times(4)).warning(anyString());
}
@Test
@ -59,18 +70,12 @@ public class SettingsWarnerTest {
given(settings.getProperty(EmailSettings.PORT25_USE_TLS)).willReturn(false);
given(settings.getProperty(PluginSettings.SESSIONS_ENABLED)).willReturn(false);
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.MD5);
given(bukkitService.isBungeeCordConfiguredForSpigot()).willReturn(Optional.empty());
// when
createSettingsWarner().logWarningsForMisconfigurations();
settingsWarner.logWarningsForMisconfigurations();
// then
verifyZeroInteractions(logger);
}
private SettingsWarner createSettingsWarner() {
SettingsWarner warner = new SettingsWarner();
ReflectionTestUtils.setField(warner, "settings", settings);
ReflectionTestUtils.setField(warner, "authMe", authMe);
return warner;
}
}