diff --git a/src/main/java/me/william278/huskhomes2/HuskHomes.java b/src/main/java/me/william278/huskhomes2/HuskHomes.java index 4da22709..7e5c0617 100644 --- a/src/main/java/me/william278/huskhomes2/HuskHomes.java +++ b/src/main/java/me/william278/huskhomes2/HuskHomes.java @@ -79,11 +79,12 @@ public final class HuskHomes extends JavaPlugin { /** * Returns the HuskHomes API + * @deprecated Use {@link HuskHomesAPI#getInstance()} instead * @return an instance of the HuskHomes API * @see HuskHomesAPI */ public HuskHomesAPI getAPI() { - return new HuskHomesAPI(); + return HuskHomesAPI.getInstance(); } diff --git a/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java b/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java index 16d8e9a3..dc970113 100644 --- a/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java +++ b/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java @@ -20,13 +20,19 @@ import java.util.logging.Level; * Only use methods/events found within the API package */ public class HuskHomesAPI { + private static HuskHomesAPI instance; + private final HuskHomes huskHomes; - private static final HuskHomes huskHomes = HuskHomes.getInstance(); - private Connection getConnection() { - return HuskHomes.getConnection(); + private HuskHomesAPI() { + huskHomes = HuskHomes.getInstance(); } - public HuskHomesAPI() { } + public static HuskHomesAPI getInstance() { + if (instance == null) { + instance = new HuskHomesAPI(); + } + return instance; + } /** Returns a Home set by a player, or NULL if it does not exist @@ -48,7 +54,7 @@ public class HuskHomesAPI { */ public Home getHome(String ownerUsername, String homeName) { try { - return DataManager.getHome(ownerUsername, homeName, getConnection()); + return DataManager.getHome(ownerUsername, homeName, huskHomes.getInstance().getConnection()); } catch (SQLException e) { huskHomes.getLogger().log(Level.WARNING, "An SQL exception occurred retrieving data for API access", e); return null; @@ -63,7 +69,7 @@ public class HuskHomesAPI { */ public Warp getWarp(String warpName) { try { - return DataManager.getWarp(warpName, getConnection()); + return DataManager.getWarp(warpName, huskHomes.getInstance().getConnection()); } catch (SQLException e) { huskHomes.getLogger().log(Level.WARNING, "An SQL exception occurred retrieving data for API access", e); return null; @@ -88,7 +94,7 @@ public class HuskHomesAPI { */ public List getHomes(String ownerUsername) { try { - return DataManager.getPlayerHomes(ownerUsername, getConnection()); + return DataManager.getPlayerHomes(ownerUsername, huskHomes.getInstance().getConnection()); } catch (SQLException e) { huskHomes.getLogger().log(Level.WARNING, "An SQL exception occurred retrieving data for API access", e); return null; @@ -130,7 +136,7 @@ public class HuskHomesAPI { */ public List getPublicHomes() { try { - return DataManager.getPublicHomes(getConnection()); + return DataManager.getPublicHomes(huskHomes.getInstance().getConnection()); } catch (SQLException e) { huskHomes.getLogger().log(Level.WARNING, "An SQL exception occurred retrieving data for API access", e); return null; @@ -153,7 +159,7 @@ public class HuskHomesAPI { */ public List getWarps() { try { - return DataManager.getWarps(getConnection()); + return DataManager.getWarps(huskHomes.getInstance().getConnection()); } catch (SQLException e) { huskHomes.getLogger().log(Level.WARNING, "An SQL exception occurred retrieving data for API access", e); return null; @@ -185,7 +191,7 @@ public class HuskHomesAPI { * @see TeleportationPoint */ public void teleportPlayer(Player player, TeleportationPoint point, boolean timed) { - Connection connection = getConnection(); + Connection connection = huskHomes.getInstance().getConnection(); Bukkit.getScheduler().runTask(huskHomes, () -> { try { if (timed) { @@ -207,7 +213,7 @@ public class HuskHomesAPI { * @see TeleportationPoint */ public void teleportPlayer(Player player, String targetPlayerName, boolean timed) { - Connection connection = getConnection(); + Connection connection = huskHomes.getInstance().getConnection(); Bukkit.getScheduler().runTask(huskHomes, () -> { try { if (timed) {