diff --git a/README.md b/README.md index 3694ff4..d6db7ab 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # VIshop-plugin -Plugin łączący serwer Minecraft z itemshopem stworzonym na [VIshop.pl](https://vishop.pl/) +Plugin łączący serwer Minecraft z itemshopem stworzonym na [VIshop.pl](https://vishop.pl/) +Wspierane (przetestowane przez autorów) wersje Minecrafta — od 1.8 do 1.19.3 Plugin przeznaczony dla platform: - pochodnych Bukkita (Spigot, Paper, itp.) diff --git a/build.gradle b/build.gradle index 7a37878..21900a5 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ allprojects { apply plugin: 'com.github.johnrengelman.shadow' group = 'pl.vishop.plugin' - version = '2.2' + version = '2.3' sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 diff --git a/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitConfigLoader.java b/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitConfigLoader.java new file mode 100644 index 0000000..81ef6e3 --- /dev/null +++ b/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitConfigLoader.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2022 VIshop-plugin Contributors + * https://github.com/ivall/VIshop-plugin/graphs/contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.vishop.plugin.bukkit; + +import org.bukkit.configuration.ConfigurationSection; +import pl.vishop.plugin.config.ConfigLoader; + +public class BukkitConfigLoader implements ConfigLoader { + + private final ConfigurationSection configFile; + + public BukkitConfigLoader(final ConfigurationSection configFile) { + this.configFile = configFile; + } + + @Override + public boolean getBoolean(final String key) { + return this.configFile.getBoolean(key); + } + + @Override + public String getString(final String key) { + return this.configFile.getString(key); + } + +} diff --git a/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitOrderTask.java b/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitOrderTask.java index e1cda37..229928e 100644 --- a/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitOrderTask.java +++ b/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitOrderTask.java @@ -21,27 +21,18 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import pl.vishop.plugin.config.Config; +import pl.vishop.plugin.logger.ViShopLogger; import pl.vishop.plugin.order.OrderTask; public class BukkitOrderTask extends OrderTask { private final Plugin plugin; - public BukkitOrderTask(final Plugin plugin, final OkHttpClient httpClient, final Config config) { - super(httpClient, config); + public BukkitOrderTask(final Plugin plugin, final OkHttpClient httpClient, final Config config, final ViShopLogger logger) { + super(httpClient, config, logger); this.plugin = plugin; } - @Override - public void logInfo(final String message) { - this.plugin.getLogger().info(message); - } - - @Override - public void logError(final String message) { - this.plugin.getLogger().severe(message); - } - @Override public boolean isPlayerOnline(final String playerName) { return Bukkit.getPlayerExact(playerName) != null; diff --git a/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitViShopLogger.java b/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitViShopLogger.java new file mode 100644 index 0000000..76c677f --- /dev/null +++ b/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitViShopLogger.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2022 VIshop-plugin Contributors + * https://github.com/ivall/VIshop-plugin/graphs/contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.vishop.plugin.bukkit; + +import java.util.logging.Logger; +import pl.vishop.plugin.logger.ViShopLogger; + +public class BukkitViShopLogger implements ViShopLogger { + + private final Logger logger; + + public BukkitViShopLogger(final Logger logger) { + this.logger = logger; + } + + @Override + public void info(final String message) { + this.logger.info(message); + } + + @Override + public void error(final String message) { + this.logger.severe(message); + } + + @Override + public void debug(final String message) { + this.logger.info(String.format("[DEBUG] %s", message)); + } + +} diff --git a/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitViShopPlugin.java b/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitViShopPlugin.java index d730d99..605b0f3 100644 --- a/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitViShopPlugin.java +++ b/bukkit/src/main/java/pl/vishop/plugin/bukkit/BukkitViShopPlugin.java @@ -19,10 +19,10 @@ import okhttp3.OkHttpClient; import org.bukkit.Bukkit; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; import pl.vishop.plugin.config.Config; import pl.vishop.plugin.config.EmptyConfigFieldException; +import pl.vishop.plugin.logger.ViShopLogger; public final class BukkitViShopPlugin extends JavaPlugin { @@ -32,22 +32,20 @@ public final class BukkitViShopPlugin extends JavaPlugin { public void onEnable() { this.saveDefaultConfig(); - final Config config; try { - final FileConfiguration cfgFile = this.getConfig(); - config = new Config(cfgFile.getString("apiKey"), cfgFile.getString("shopId"), cfgFile.getString("serverId")); + final Config config = new Config(new BukkitConfigLoader(this.getConfig())); + final ViShopLogger logger = new BukkitViShopLogger(this.getLogger()); + + Bukkit.getScheduler().runTaskTimerAsynchronously( + this, + new BukkitOrderTask(this, this.httpClient, config, logger), + 0L, + config.taskInterval.getSeconds() * 20L + ); } catch (final EmptyConfigFieldException exception) { this.getLogger().severe(exception.getMessage()); Bukkit.getPluginManager().disablePlugin(this); - return; } - - Bukkit.getScheduler().runTaskTimerAsynchronously( - this, - new BukkitOrderTask(this, this.httpClient, config), - 0L, - config.taskInterval.getSeconds() * 20L - ); } @Override diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index ae3d14c..70362be 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: ViShopPlugin -version: '2.2' +version: '2.3' api-version: 1.13 main: pl.vishop.plugin.bukkit.BukkitViShopPlugin description: "Wykonuj zamówienia ze swojego sklepu ViShop" diff --git a/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeConfigLoader.java b/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeConfigLoader.java new file mode 100644 index 0000000..cbb5981 --- /dev/null +++ b/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeConfigLoader.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2022 VIshop-plugin Contributors + * https://github.com/ivall/VIshop-plugin/graphs/contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.vishop.plugin.bungee; + +import net.md_5.bungee.config.Configuration; +import pl.vishop.plugin.config.ConfigLoader; + +public class BungeeConfigLoader implements ConfigLoader { + + private final Configuration configFile; + + public BungeeConfigLoader(final Configuration configFile) { + this.configFile = configFile; + } + + @Override + public boolean getBoolean(final String key) { + return this.configFile.getBoolean(key); + } + + @Override + public String getString(final String key) { + return this.configFile.getString(key); + } + +} diff --git a/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeOrderTask.java b/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeOrderTask.java index 745366a..23e258e 100644 --- a/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeOrderTask.java +++ b/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeOrderTask.java @@ -17,32 +17,19 @@ package pl.vishop.plugin.bungee; -import java.util.logging.Logger; import net.md_5.bungee.api.ProxyServer; -import net.md_5.bungee.api.plugin.Plugin; import okhttp3.OkHttpClient; import pl.vishop.plugin.config.Config; +import pl.vishop.plugin.logger.ViShopLogger; import pl.vishop.plugin.order.OrderTask; public class BungeeOrderTask extends OrderTask { private final ProxyServer proxy; - private final Logger logger; - public BungeeOrderTask(final Plugin plugin, final OkHttpClient httpClient, final Config config) { - super(httpClient, config); - this.proxy = plugin.getProxy(); - this.logger = plugin.getLogger(); - } - - @Override - public void logInfo(final String message) { - this.logger.info(message); - } - - @Override - public void logError(final String message) { - this.logger.severe(message); + public BungeeOrderTask(final OkHttpClient httpClient, final Config config, final ViShopLogger logger) { + super(httpClient, config, logger); + this.proxy = ProxyServer.getInstance(); } @Override diff --git a/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeViShopLogger.java b/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeViShopLogger.java new file mode 100644 index 0000000..a13cbfc --- /dev/null +++ b/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeViShopLogger.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2022 VIshop-plugin Contributors + * https://github.com/ivall/VIshop-plugin/graphs/contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.vishop.plugin.bungee; + +import java.util.logging.Logger; +import pl.vishop.plugin.logger.ViShopLogger; + +public class BungeeViShopLogger implements ViShopLogger { + + private final Logger logger; + + public BungeeViShopLogger(final Logger logger) { + this.logger = logger; + } + + @Override + public void info(final String message) { + this.logger.info(message); + } + + @Override + public void error(final String message) { + this.logger.severe(message); + } + + @Override + public void debug(final String message) { + this.logger.info(String.format("[DEBUG] %s", message)); + } + +} diff --git a/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeViShopPlugin.java b/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeViShopPlugin.java index 2afe6ff..0db4c74 100644 --- a/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeViShopPlugin.java +++ b/bungee/src/main/java/pl/vishop/plugin/bungee/BungeeViShopPlugin.java @@ -23,6 +23,7 @@ import okhttp3.OkHttpClient; import pl.vishop.plugin.config.Config; import pl.vishop.plugin.config.EmptyConfigFieldException; +import pl.vishop.plugin.logger.ViShopLogger; import pl.vishop.plugin.resource.ResourceLoader; import pl.vishop.plugin.resource.ResourceLoaderException; @@ -39,23 +40,21 @@ public void onEnable() { return; } - final Configuration cfgFile = resourceLoader.load("config.yml"); - final Config config; - try { - config = new Config(cfgFile.getString("apiKey"), cfgFile.getString("shopId"), cfgFile.getString("serverId")); + final Configuration cfgFile = resourceLoader.load("config.yml"); + final Config config = new Config(new BungeeConfigLoader(cfgFile)); + final ViShopLogger logger = new BungeeViShopLogger(this.getLogger()); + + this.getProxy().getScheduler().schedule( + this, + new BungeeOrderTask(this.httpClient, config, logger), + 0L, + config.taskInterval.getSeconds(), + TimeUnit.SECONDS + ); } catch (final EmptyConfigFieldException exception) { this.getLogger().severe(exception.getMessage()); - return; } - - this.getProxy().getScheduler().schedule( - this, - new BungeeOrderTask(this, this.httpClient, config), - 0L, - config.taskInterval.getSeconds(), - TimeUnit.SECONDS - ); } catch (final ResourceLoaderException exception) { this.getLogger().severe(exception.getReason().getMessage("config.yml")); this.getLogger().severe("Przyczyna: " + exception.getCause().getMessage()); diff --git a/bungee/src/main/resources/bungee.yml b/bungee/src/main/resources/bungee.yml index f9b48b1..b4755b7 100644 --- a/bungee/src/main/resources/bungee.yml +++ b/bungee/src/main/resources/bungee.yml @@ -1,5 +1,5 @@ name: ViShopPlugin -version: '2.2' +version: '2.3' api-version: 1.13 main: pl.vishop.plugin.bungee.BungeeViShopPlugin description: "Wykonuj zamówienia ze swojego sklepu ViShop" diff --git a/common/src/main/java/pl/vishop/plugin/config/Config.java b/common/src/main/java/pl/vishop/plugin/config/Config.java index e56fc09..7d3a1e6 100644 --- a/common/src/main/java/pl/vishop/plugin/config/Config.java +++ b/common/src/main/java/pl/vishop/plugin/config/Config.java @@ -25,13 +25,15 @@ public class Config { public final String apiKey; public final String shopId; public final String serverId; + public final boolean debug; public final Duration taskInterval = Duration.of(30, ChronoUnit.SECONDS); - public Config(final String apiKey, final String shopId, final String serverId) throws EmptyConfigFieldException { - this.apiKey = apiKey; - this.shopId = shopId; - this.serverId = serverId; + public Config(final ConfigLoader loader) throws EmptyConfigFieldException { + this.apiKey = loader.getString("apiKey"); + this.shopId = loader.getString("shopId"); + this.serverId = loader.getString("serverId"); + this.debug = loader.getBoolean("debug"); this.checkValues(); } diff --git a/common/src/main/java/pl/vishop/plugin/config/ConfigLoader.java b/common/src/main/java/pl/vishop/plugin/config/ConfigLoader.java new file mode 100644 index 0000000..35b0891 --- /dev/null +++ b/common/src/main/java/pl/vishop/plugin/config/ConfigLoader.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2022 VIshop-plugin Contributors + * https://github.com/ivall/VIshop-plugin/graphs/contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.vishop.plugin.config; + +public interface ConfigLoader { + + boolean getBoolean(final String key); + + String getString(final String key); + +} diff --git a/common/src/main/java/pl/vishop/plugin/logger/ViShopLogger.java b/common/src/main/java/pl/vishop/plugin/logger/ViShopLogger.java new file mode 100644 index 0000000..8535c6b --- /dev/null +++ b/common/src/main/java/pl/vishop/plugin/logger/ViShopLogger.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2022 VIshop-plugin Contributors + * https://github.com/ivall/VIshop-plugin/graphs/contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.vishop.plugin.logger; + +public interface ViShopLogger { + + void info(final String message); + + void error(final String message); + + void debug(final String message); + +} diff --git a/common/src/main/java/pl/vishop/plugin/order/OrderTask.java b/common/src/main/java/pl/vishop/plugin/order/OrderTask.java index 24ec53f..bd1a921 100644 --- a/common/src/main/java/pl/vishop/plugin/order/OrderTask.java +++ b/common/src/main/java/pl/vishop/plugin/order/OrderTask.java @@ -20,6 +20,7 @@ import java.util.Arrays; import okhttp3.OkHttpClient; import pl.vishop.plugin.config.Config; +import pl.vishop.plugin.logger.ViShopLogger; import pl.vishop.plugin.request.ConfirmOrderRequest; import pl.vishop.plugin.request.PendingOrderRequest; import pl.vishop.plugin.request.RequestException; @@ -28,16 +29,14 @@ public abstract class OrderTask implements Runnable { private final PendingOrderRequest pendingOrderRequest; private final ConfirmOrderRequest confirmOrderRequest; + private final ViShopLogger logger; - protected OrderTask(final OkHttpClient httpClient, final Config config) { - this.pendingOrderRequest = new PendingOrderRequest(httpClient, config); - this.confirmOrderRequest = new ConfirmOrderRequest(httpClient, config); + protected OrderTask(final OkHttpClient httpClient, final Config config, final ViShopLogger logger) { + this.pendingOrderRequest = new PendingOrderRequest(httpClient, config, logger); + this.confirmOrderRequest = new ConfirmOrderRequest(httpClient, config, logger); + this.logger = logger; } - public abstract void logInfo(final String message); - - public abstract void logError(final String message); - public abstract boolean isPlayerOnline(final String playerName); public abstract void executeCommand(final String command); @@ -47,8 +46,8 @@ public void run() { try { Arrays.stream(this.pendingOrderRequest.get()).forEach(this::processOrder); } catch (final RequestException exception) { - this.logError("Nieudane pobranie zamówień z ViShop"); - this.logError("Przyczyna: " + exception.getMessage()); + this.logger.error("Nieudane pobranie zamówień z ViShop"); + this.logger.error("Przyczyna: " + exception.getMessage()); } } @@ -60,12 +59,12 @@ private void processOrder(final Order order) { try { this.confirmOrderRequest.put(order); order.getCommands().forEach(command -> { - this.logInfo(String.format("Wykonywanie komendy dla zamówienia %s: %s", order.getId(), command)); + this.logger.info(String.format("Wykonywanie komendy dla zamówienia %s: %s", order.getId(), command)); this.executeCommand(command); }); } catch (final RequestException exception) { - this.logError(String.format("Nieudane potwierdzenie zamówienia %s w ViShop", order.getId())); - this.logError("Przyczyna: " + exception.getMessage()); + this.logger.error(String.format("Nieudane potwierdzenie zamówienia %s w ViShop", order.getId())); + this.logger.error("Przyczyna: " + exception.getMessage()); } } diff --git a/common/src/main/java/pl/vishop/plugin/request/ConfirmOrderRequest.java b/common/src/main/java/pl/vishop/plugin/request/ConfirmOrderRequest.java index 18d05da..335506f 100644 --- a/common/src/main/java/pl/vishop/plugin/request/ConfirmOrderRequest.java +++ b/common/src/main/java/pl/vishop/plugin/request/ConfirmOrderRequest.java @@ -17,13 +17,14 @@ package pl.vishop.plugin.request; -import java.io.IOException; +import okhttp3.HttpUrl; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import pl.vishop.plugin.config.Config; +import pl.vishop.plugin.logger.ViShopLogger; import pl.vishop.plugin.order.Order; public final class ConfirmOrderRequest extends ViShopRequest { @@ -33,16 +34,28 @@ public final class ConfirmOrderRequest extends ViShopRequest { private final OkHttpClient httpClient; private final Config config; + private final ViShopLogger logger; - public ConfirmOrderRequest(final OkHttpClient httpClient, final Config config) { + public ConfirmOrderRequest(final OkHttpClient httpClient, final Config config, final ViShopLogger logger) { this.httpClient = httpClient; this.config = config; + this.logger = logger; } public void put(final Order order) throws RequestException { final Request request = this.preparePutRequest(this.getRequestUrl(order), this.config.apiKey, REQUEST_BODY); + if (this.config.debug) { + this.logger.debug(String.format("Sending PUT request to url: %s", request.url())); + this.logger.debug(String.format("Attaching API key: %s", this.config.apiKey)); + } + try (final Response response = this.httpClient.newCall(request).execute()) { + final String responseBody = response.body() != null ? response.body().string() : null; + if (this.config.debug) { + this.logger.debug(String.format("Response for PUT request: %s", responseBody)); + } + if (!response.isSuccessful()) { throw new RequestException("Otrzymany kod odpowiedzi " + response.code()); } @@ -51,8 +64,9 @@ public void put(final Order order) throws RequestException { } } - private String getRequestUrl(final Order order) { - return String.format(BACKEND_ADDRESS, this.config.shopId, this.config.serverId, order.getId() + "/"); + private HttpUrl getRequestUrl(final Order order) { + final String urlString = String.format(BACKEND_ADDRESS, this.config.shopId, this.config.serverId, order.getId() + "/"); + return HttpUrl.parse(urlString); } } diff --git a/common/src/main/java/pl/vishop/plugin/request/PendingOrderRequest.java b/common/src/main/java/pl/vishop/plugin/request/PendingOrderRequest.java index 4205bd0..c12aa9a 100644 --- a/common/src/main/java/pl/vishop/plugin/request/PendingOrderRequest.java +++ b/common/src/main/java/pl/vishop/plugin/request/PendingOrderRequest.java @@ -22,11 +22,12 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; import java.io.IOException; +import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -import okhttp3.ResponseBody; import pl.vishop.plugin.config.Config; +import pl.vishop.plugin.logger.ViShopLogger; import pl.vishop.plugin.order.Order; public final class PendingOrderRequest extends ViShopRequest { @@ -34,30 +35,39 @@ public final class PendingOrderRequest extends ViShopRequest { private final Gson gson; private final OkHttpClient httpClient; private final Config config; - private final String requestUrl; + private final ViShopLogger logger; - public PendingOrderRequest(final OkHttpClient httpClient, final Config config) { + public PendingOrderRequest(final OkHttpClient httpClient, final Config config, final ViShopLogger logger) { this.gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); this.httpClient = httpClient; this.config = config; - this.requestUrl = String.format(BACKEND_ADDRESS, config.shopId, config.serverId, "?status=executing"); + this.logger = logger; } public Order[] get() throws RequestException { - final Request request = this.prepareGetRequest(this.requestUrl, this.config.apiKey); + final Request request = this.prepareGetRequest(this.getRequestUrl(), this.config.apiKey); + + if (this.config.debug) { + this.logger.debug(String.format("Sending GET request to url: %s", request.url())); + this.logger.debug(String.format("Attaching API key: %s", this.config.apiKey)); + } try (final Response response = this.httpClient.newCall(request).execute()) { + final String responseBody = response.body() != null ? response.body().string() : null; + if (this.config.debug) { + this.logger.debug(String.format("Response for GET request: %s", responseBody)); + } + if (!response.isSuccessful()) { throw new RequestException("Otrzymany kod odpowiedzi " + response.code()); } - final ResponseBody responseBody = response.body(); if (responseBody == null) { throw new RequestException("Puste body odpowiedzi"); } try { - return this.gson.fromJson(responseBody.string(), Order[].class); + return this.gson.fromJson(responseBody, Order[].class); } catch (final JsonSyntaxException exception) { throw new RequestException(exception.getMessage()); } @@ -66,4 +76,9 @@ public Order[] get() throws RequestException { } } + private HttpUrl getRequestUrl() { + final String urlString = String.format(BACKEND_ADDRESS, this.config.shopId, this.config.serverId, "?status=executing"); + return HttpUrl.parse(urlString); + } + } diff --git a/common/src/main/java/pl/vishop/plugin/request/ViShopRequest.java b/common/src/main/java/pl/vishop/plugin/request/ViShopRequest.java index 3c2e82a..c99857b 100644 --- a/common/src/main/java/pl/vishop/plugin/request/ViShopRequest.java +++ b/common/src/main/java/pl/vishop/plugin/request/ViShopRequest.java @@ -17,6 +17,7 @@ package pl.vishop.plugin.request; +import okhttp3.HttpUrl; import okhttp3.Request; import okhttp3.RequestBody; @@ -24,18 +25,18 @@ public class ViShopRequest { public static final String BACKEND_ADDRESS = "https://dev123.vishop.pl/panel/shops/%1$s/servers/%2$s/payments/%3$s"; - protected Request prepareGetRequest(final String url, final String apiKey) { + protected Request prepareGetRequest(final HttpUrl url, final String apiKey) { return this.prepareRequestBuilder(url, apiKey).build(); } - protected Request preparePutRequest(final String url, final String apiKey, final RequestBody body) { + protected Request preparePutRequest(final HttpUrl url, final String apiKey, final RequestBody body) { return this.prepareRequestBuilder(url, apiKey).put(body).build(); } - private Request.Builder prepareRequestBuilder(final String url, final String apiKey) { + private Request.Builder prepareRequestBuilder(final HttpUrl url, final String apiKey) { return new Request.Builder() .url(url) - .header("User-Agent", "ViShopPlugin/2.2") + .header("User-Agent", "ViShopPlugin/2.3") .header("Authorization", apiKey); } diff --git a/common/src/main/resources/config.yml b/common/src/main/resources/config.yml index 9a92c01..e9a99f0 100644 --- a/common/src/main/resources/config.yml +++ b/common/src/main/resources/config.yml @@ -9,3 +9,13 @@ shopId: "" # ID serwera w sklepie # Znajdziesz je w panelu sklepu, w zakładce Serwery serverId: "" + +# Czy włączyć logi debug? +# +# UWAGA — logi debug powinny być włączone tylko w przypadku problemów z pluginem +# Po ich ustąpieniu — powinny znowu zostać wyłączone, a stare pliki logów skasowane +# Uruchomienie tych logów będzie pokazywać w konsoli wrażliwe informacje: +# - klucz API +# - adresy, z którymi łączy się plugin +# - dane wysyłane przez ViShop +debug: false diff --git a/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityConfigLoader.java b/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityConfigLoader.java new file mode 100644 index 0000000..c8eab78 --- /dev/null +++ b/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityConfigLoader.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2022 VIshop-plugin Contributors + * https://github.com/ivall/VIshop-plugin/graphs/contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.vishop.plugin.velocity; + +import ninja.leaping.configurate.ConfigurationNode; +import pl.vishop.plugin.config.ConfigLoader; + +public class VelocityConfigLoader implements ConfigLoader { + + private final ConfigurationNode configFile; + + public VelocityConfigLoader(final ConfigurationNode configFile) { + this.configFile = configFile; + } + + @Override + public boolean getBoolean(final String key) { + return this.configFile.getNode(key).getBoolean(); + } + + @Override + public String getString(final String key) { + return this.configFile.getNode(key).getString(); + } + +} diff --git a/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityOrderTask.java b/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityOrderTask.java index 655d380..a9a29bf 100644 --- a/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityOrderTask.java +++ b/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityOrderTask.java @@ -19,29 +19,17 @@ import com.velocitypowered.api.proxy.ProxyServer; import okhttp3.OkHttpClient; -import org.slf4j.Logger; import pl.vishop.plugin.config.Config; +import pl.vishop.plugin.logger.ViShopLogger; import pl.vishop.plugin.order.OrderTask; public class VelocityOrderTask extends OrderTask { private final ProxyServer proxy; - private final Logger logger; - protected VelocityOrderTask(final ProxyServer proxy, final Logger logger, final OkHttpClient httpClient, final Config config) { - super(httpClient, config); + protected VelocityOrderTask(final ProxyServer proxy, final OkHttpClient httpClient, final Config config, final ViShopLogger logger) { + super(httpClient, config, logger); this.proxy = proxy; - this.logger = logger; - } - - @Override - public void logInfo(final String message) { - this.logger.info(message); - } - - @Override - public void logError(final String message) { - this.logger.error(message); } @Override diff --git a/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityViShopLogger.java b/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityViShopLogger.java new file mode 100644 index 0000000..e25cd62 --- /dev/null +++ b/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityViShopLogger.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2022 VIshop-plugin Contributors + * https://github.com/ivall/VIshop-plugin/graphs/contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package pl.vishop.plugin.velocity; + +import org.slf4j.Logger; +import pl.vishop.plugin.logger.ViShopLogger; + +public class VelocityViShopLogger implements ViShopLogger { + + private final Logger logger; + + public VelocityViShopLogger(final Logger logger) { + this.logger = logger; + } + + @Override + public void info(final String message) { + this.logger.info(message); + } + + @Override + public void error(final String message) { + this.logger.error(message); + } + + @Override + public void debug(final String message) { + this.logger.info(String.format("[DEBUG] %s", message)); + } + +} diff --git a/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityViShopPlugin.java b/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityViShopPlugin.java index daac335..5b7a799 100644 --- a/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityViShopPlugin.java +++ b/velocity/src/main/java/pl/vishop/plugin/velocity/VelocityViShopPlugin.java @@ -31,13 +31,14 @@ import org.slf4j.Logger; import pl.vishop.plugin.config.Config; import pl.vishop.plugin.config.EmptyConfigFieldException; +import pl.vishop.plugin.logger.ViShopLogger; import pl.vishop.plugin.resource.ResourceLoader; import pl.vishop.plugin.resource.ResourceLoaderException; @Plugin( id = "vishop", name = "ViShopPlugin", - version = "2.2", + version = "2.3", description = "Wykonuj zamówienia ze swojego sklepu ViShop", url = "https://vishop.pl/", authors = "VIshop-plugin Contributors" @@ -67,24 +68,18 @@ public void onProxyInit(final ProxyInitializeEvent event) { return; } - final ConfigurationNode cfgFile = resourceLoader.load("config.yml"); - final Config config; - try { - config = new Config( - cfgFile.getNode("apiKey").getString(), - cfgFile.getNode("shopId").getString(), - cfgFile.getNode("serverId").getString() - ); + final ConfigurationNode cfgFile = resourceLoader.load("config.yml"); + final Config config = new Config(new VelocityConfigLoader(cfgFile)); + final ViShopLogger viShopLogger = new VelocityViShopLogger(this.logger); + + this.orderTask = this.proxy.getScheduler() + .buildTask(this, new VelocityOrderTask(this.proxy, this.httpClient, config, viShopLogger)) + .repeat(config.taskInterval) + .schedule(); } catch (final EmptyConfigFieldException exception) { this.logger.error(exception.getMessage()); - return; } - - this.orderTask = this.proxy.getScheduler() - .buildTask(this, new VelocityOrderTask(this.proxy, this.logger, this.httpClient, config)) - .repeat(config.taskInterval) - .schedule(); } catch (final ResourceLoaderException exception) { this.logger.error(exception.getReason().getMessage("config.yml")); this.logger.error("Przyczyna: " + exception.getCause().getMessage());