Switched from plugin.getServer() to Bukkit.getServer() to enable tests

plugin.getServer() cannot be mocked because it is final.
This commit is contained in:
tastybento 2019-03-11 22:58:30 -07:00
parent adf0b8247f
commit cb09ec2e3a
5 changed files with 17 additions and 13 deletions

View File

@ -1,5 +1,7 @@
package world.bentobox.bentobox.database.mongodb; package world.bentobox.bentobox.database.mongodb;
import org.bukkit.Bukkit;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.AbstractDatabaseHandler; import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.DatabaseConnectionSettingsImpl; import world.bentobox.bentobox.database.DatabaseConnectionSettingsImpl;
@ -11,10 +13,10 @@ public class MongoDBDatabase implements DatabaseSetup {
public <T> AbstractDatabaseHandler<T> getHandler(Class<T> type) { public <T> AbstractDatabaseHandler<T> getHandler(Class<T> type) {
BentoBox plugin = BentoBox.getInstance(); BentoBox plugin = BentoBox.getInstance();
// Check if the MongoDB plugin exists // Check if the MongoDB plugin exists
if (plugin.getServer().getPluginManager().getPlugin("BsbMongo") == null) { if (Bukkit.getServer().getPluginManager().getPlugin("BsbMongo") == null) {
plugin.logError("You must install BsbMongo plugin for MongoDB support!"); plugin.logError("You must install BsbMongo plugin for MongoDB support!");
plugin.logError("See: https://github.com/tastybento/bsbMongo/releases/"); plugin.logError("See: https://github.com/tastybento/bsbMongo/releases/");
plugin.getServer().getPluginManager().disablePlugin(plugin); Bukkit.getServer().getPluginManager().disablePlugin(plugin);
return null; return null;
} }
return new MongoDBDatabaseHandler<>(plugin, type, new MongoDBDatabaseConnector(new DatabaseConnectionSettingsImpl( return new MongoDBDatabaseHandler<>(plugin, type, new MongoDBDatabaseConnector(new DatabaseConnectionSettingsImpl(

View File

@ -720,8 +720,8 @@ public class Island implements DataObject {
user.sendMessage("commands.admin.info.owner", "[owner]", plugin.getPlayers().getName(owner), "[uuid]", owner.toString()); user.sendMessage("commands.admin.info.owner", "[owner]", plugin.getPlayers().getName(owner), "[uuid]", owner.toString());
// Fixes #getLastPlayed() returning 0 when it is the owner's first connection. // Fixes #getLastPlayed() returning 0 when it is the owner's first connection.
long lastPlayed = (plugin.getServer().getOfflinePlayer(owner).getLastPlayed() != 0) ? long lastPlayed = (Bukkit.getServer().getOfflinePlayer(owner).getLastPlayed() != 0) ?
plugin.getServer().getOfflinePlayer(owner).getLastPlayed() : plugin.getServer().getOfflinePlayer(owner).getFirstPlayed(); Bukkit.getServer().getOfflinePlayer(owner).getLastPlayed() : Bukkit.getServer().getOfflinePlayer(owner).getFirstPlayed();
user.sendMessage("commands.admin.info.last-login","[date]", new Date(lastPlayed).toString()); user.sendMessage("commands.admin.info.last-login","[date]", new Date(lastPlayed).toString());
user.sendMessage("commands.admin.info.deaths", "[number]", String.valueOf(plugin.getPlayers().getDeaths(world, owner))); user.sendMessage("commands.admin.info.deaths", "[number]", String.valueOf(plugin.getPlayers().getDeaths(world, owner)));

View File

@ -175,7 +175,7 @@ public class JoinLeaveListener implements Listener {
plugin.getIWM().getOverWorlds().forEach(w -> { plugin.getIWM().getOverWorlds().forEach(w -> {
Island island = plugin.getIslands().getIsland(w, User.getInstance(event.getPlayer())); Island island = plugin.getIslands().getIsland(w, User.getInstance(event.getPlayer()));
// Are there any online players still for this island? // Are there any online players still for this island?
if (island != null && plugin.getServer().getOnlinePlayers().stream() if (island != null && Bukkit.getServer().getOnlinePlayers().stream()
.filter(p -> !event.getPlayer().equals(p)) .filter(p -> !event.getPlayer().equals(p))
.noneMatch(p -> plugin.getIslands().getMembers(w, event.getPlayer().getUniqueId()).contains(p.getUniqueId()))) { .noneMatch(p -> plugin.getIslands().getMembers(w, event.getPlayer().getUniqueId()).contains(p.getUniqueId()))) {
// No, there are no more players online on this island // No, there are no more players online on this island

View File

@ -7,6 +7,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SkullMeta;
@ -31,7 +32,7 @@ public class HeadGetter {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private void runPlayerHeadGetter() { private void runPlayerHeadGetter() {
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, () -> { Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> {
synchronized(names) { synchronized(names) {
Iterator<Entry<String, PanelItem>> it = names.entrySet().iterator(); Iterator<Entry<String, PanelItem>> it = names.entrySet().iterator();
if (it.hasNext()) { if (it.hasNext()) {
@ -46,7 +47,7 @@ public class HeadGetter {
if (headRequesters.containsKey(en.getKey())) { if (headRequesters.containsKey(en.getKey())) {
for (HeadRequester req : headRequesters.get(en.getKey())) { for (HeadRequester req : headRequesters.get(en.getKey())) {
en.getValue().setHead(playerSkull.clone()); en.getValue().setHead(playerSkull.clone());
plugin.getServer().getScheduler().runTask(plugin, () -> req.setHead(en.getValue())); Bukkit.getServer().getScheduler().runTask(plugin, () -> req.setHead(en.getValue()));
} }
} }
it.remove(); it.remove();
@ -54,12 +55,12 @@ public class HeadGetter {
} }
}, 0L, 20L); }, 0L, 20L);
} }
/** /**
* @param panelItem - head to update * @param panelItem - head to update
* @param requester - callback class * @param requester - callback class
*/ */
public static void getHead(PanelItem panelItem, HeadRequester requester) { public static void getHead(PanelItem panelItem, HeadRequester requester) {
// Check if in cache // Check if in cache
if (cachedHeads.containsKey(panelItem.getName())) { if (cachedHeads.containsKey(panelItem.getName())) {
panelItem.setHead(cachedHeads.get(panelItem.getName()).clone()); panelItem.setHead(cachedHeads.get(panelItem.getName()).clone());
@ -73,5 +74,5 @@ public class HeadGetter {
names.put(panelItem.getName(), panelItem); names.put(panelItem.getName(), panelItem);
} }
} }
} }

View File

@ -1,5 +1,6 @@
package world.bentobox.bentobox.versions; package world.bentobox.bentobox.versions;
import org.bukkit.Bukkit;
import org.bukkit.Server; import org.bukkit.Server;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
@ -119,7 +120,7 @@ public class ServerCompatibility {
public Compatibility checkCompatibility(BentoBox plugin) { public Compatibility checkCompatibility(BentoBox plugin) {
if (result == null) { if (result == null) {
// Check the server version first // Check the server version first
ServerVersion version = getServerVersion(plugin.getServer()); ServerVersion version = getServerVersion(Bukkit.getServer());
if (version == null || version.getCompatibility().equals(Compatibility.INCOMPATIBLE)) { if (version == null || version.getCompatibility().equals(Compatibility.INCOMPATIBLE)) {
// 'Version = null' means that it's not listed. And therefore, it's implicitly incompatible. // 'Version = null' means that it's not listed. And therefore, it's implicitly incompatible.
@ -128,7 +129,7 @@ public class ServerCompatibility {
} }
// Now, check the server software // Now, check the server software
ServerSoftware software = getServerSoftware(plugin.getServer()); ServerSoftware software = getServerSoftware(Bukkit.getServer());
if (software == null || software.getCompatibility().equals(Compatibility.INCOMPATIBLE)) { if (software == null || software.getCompatibility().equals(Compatibility.INCOMPATIBLE)) {
// 'software = null' means that it's not listed. And therefore, it's implicitly incompatible. // 'software = null' means that it's not listed. And therefore, it's implicitly incompatible.