diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiHost.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiHost.java index a168c3540..a6dd1cbae 100644 --- a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiHost.java +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/ApiHost.java @@ -1,21 +1,80 @@ package mineplex.core.common.api; -/** - * TODO: Store this in a file instead of being hardcoded - * - * @author Shaun Bennett - */ -public enum ApiHost +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.configuration.file.YamlConfiguration; + +public class ApiHost { - AMPLIFIERS("10.33.53.12", 7979), - ANTISPAM("10.33.53.12", 8181), - ENDERCHEST("10.33.53.10", 8010) - ; + private static final String API_HOST_FILE = "api-config.dat"; + private static final Object LOCK = new Object(); + + private static volatile boolean LOADED = false; + + private static final Map API_HOST_MAP = new HashMap<>(); + + public static ApiHost getAPIHost(String identifier) + { + if (!LOADED) + { + synchronized (LOCK) + { + if (!LOADED) + { + try + { + File configFile = new File(API_HOST_FILE); + YamlConfiguration configuration = YamlConfiguration.loadConfiguration(configFile); + + for (String key : configuration.getKeys(false)) + { + String ip = configuration.getConfigurationSection(key).getString("ip"); + // Use parseInt to catch non-ints instead of a 0 + int port = Integer.parseInt(configuration.getConfigurationSection(key).getString("port")); + if (ip == null) + { + throw new NullPointerException(); + } + + API_HOST_MAP.put(key, new ApiHost(ip, port)); + } + } + catch (Throwable t) + { + t.printStackTrace(); + } + finally + { + LOADED = true; + } + } + } + } + + return API_HOST_MAP.get(identifier); + } + + public static ApiHost getAmplifierService() + { + return getAPIHost("AMPLIFIERS"); + } + + public static ApiHost getAntispamService() + { + return getAPIHost("ANTISPAM"); + } + + public static ApiHost getEnderchestService() + { + return getAPIHost("ENDERCHEST"); + } private String _host; private int _port; - ApiHost(String host, int port) + private ApiHost(String host, int port) { _host = host; _port = port; diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/enderchest/EnderchestWorldLoader.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/enderchest/EnderchestWorldLoader.java index 0a782d6d7..5a196c26e 100644 --- a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/enderchest/EnderchestWorldLoader.java +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/api/enderchest/EnderchestWorldLoader.java @@ -18,7 +18,7 @@ public class EnderchestWorldLoader public EnderchestWorldLoader() { - String url = "http://" + ApiHost.ENDERCHEST.getHost() + ":" + ApiHost.ENDERCHEST.getPort() + "/"; + String url = "http://" + ApiHost.getEnderchestService().getHost() + ":" + ApiHost.getEnderchestService().getPort() + "/"; _webCall = new ApiWebCall(url); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/antispam/repository/AntiSpamRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/antispam/repository/AntiSpamRepository.java index 4211d3116..be920e079 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/antispam/repository/AntiSpamRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/antispam/repository/AntiSpamRepository.java @@ -17,7 +17,7 @@ public class AntiSpamRepository extends ApiEndpoint { public AntiSpamRepository() { - super(ApiHost.ANTISPAM, "/chat"); + super(ApiHost.getAntispamService(), "/chat"); } public AntiSpamApiResponse sendMessage(String source, ChatPayload payload) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterRepository.java index 25e8c2c64..2f42602e9 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/boosters/BoosterRepository.java @@ -24,7 +24,7 @@ public class BoosterRepository extends ApiEndpoint { public BoosterRepository() { - super(ApiHost.AMPLIFIERS, "/booster", new GsonBuilder().setFieldNamingStrategy(new ApiFieldNamingStrategy()) + super(ApiHost.getAmplifierService(), "/booster", new GsonBuilder().setFieldNamingStrategy(new ApiFieldNamingStrategy()) // .registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()) .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").create()); }