From 2003cf1d7705b405d5afc06e198fe8ec8ae5b899 Mon Sep 17 00:00:00 2001 From: NotroDev <77281214+NotroDev@users.noreply.github.com> Date: Fri, 11 Nov 2022 20:20:07 +0100 Subject: [PATCH] Use GSON instead of org.json (resolves #4) * Change JSON library and config error messages * Update config.yml * Some changes --- build.gradle | 4 +- .../pl/vishop/vishopplugin/config/Config.java | 7 ++-- .../request/ConfirmRequester.java | 5 ++- .../request/PendingRequester.java | 41 ++++++++++--------- src/main/resources/config.yml | 3 ++ 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/build.gradle b/build.gradle index 48cf0c9..18e470b 100644 --- a/build.gradle +++ b/build.gradle @@ -22,8 +22,7 @@ repositories { dependencies { compileOnly 'org.bukkit:bukkit:1.8.8-R0.1-SNAPSHOT' - implementation 'com.squareup.okhttp3:okhttp:4.9.3' - implementation 'org.json:json:20211205' + implementation 'com.squareup.okhttp3:okhttp:4.10.0' } processResources { @@ -45,7 +44,6 @@ shadowJar { relocate 'kotlin', 'lib.kotlin' relocate 'okhttp3', 'lib.okhttp3' relocate 'okio', 'lib.okio' - relocate 'org.json', 'lib.json' archiveFileName = "${rootProject.name} v${archiveVersion.get()}.${archiveExtension.get()}" } diff --git a/src/main/java/pl/vishop/vishopplugin/config/Config.java b/src/main/java/pl/vishop/vishopplugin/config/Config.java index 87f38c3..9e13aaf 100644 --- a/src/main/java/pl/vishop/vishopplugin/config/Config.java +++ b/src/main/java/pl/vishop/vishopplugin/config/Config.java @@ -17,6 +17,7 @@ package pl.vishop.vishopplugin.config; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.configuration.ConfigurationSection; public class Config { @@ -30,19 +31,19 @@ public class Config { public boolean load(final ConfigurationSection cfg) { this.apiKey = cfg.getString("apiKey", ""); if (this.apiKey.isEmpty()) { - Bukkit.getLogger().warning("Uzupełnij klucz API w configu, aby łączyć się ze swoim sklepem w ViShop"); + Bukkit.getLogger().severe("Musisz uzupełnić klucz API w pliku konfiguracyjnym, aby plugin działał. Wyłączam..."); return false; } this.shopId = cfg.getString("shopId", ""); if (this.shopId.isEmpty()) { - Bukkit.getLogger().warning("Uzupełnij ID sklepu w configu, aby łączyć się ze swoim sklepem w ViShop"); + Bukkit.getLogger().severe("Musisz uzupełnić ID sklepu pliku konfiguracyjnym, aby plugin działał. Wyłączam..."); return false; } this.serverId = cfg.getString("serverId", ""); if (this.serverId.isEmpty()) { - Bukkit.getLogger().warning("Uzupełnij ID serwera w configu, aby łączyć się ze swoim sklepem w ViShop"); + Bukkit.getLogger().severe("Musisz uzupełnić ID serwera w pliku konfiguracyjnym, aby plugin działał. Wyłączam..."); return false; } diff --git a/src/main/java/pl/vishop/vishopplugin/request/ConfirmRequester.java b/src/main/java/pl/vishop/vishopplugin/request/ConfirmRequester.java index 3d0e5a1..35a6307 100644 --- a/src/main/java/pl/vishop/vishopplugin/request/ConfirmRequester.java +++ b/src/main/java/pl/vishop/vishopplugin/request/ConfirmRequester.java @@ -17,6 +17,8 @@ package pl.vishop.vishopplugin.request; import java.io.IOException; + +import com.google.gson.JsonIOException; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -24,7 +26,6 @@ import okhttp3.RequestBody; import okhttp3.Response; import org.bukkit.Bukkit; -import org.json.JSONException; import pl.vishop.vishopplugin.ViShopPlugin; import pl.vishop.vishopplugin.config.Config; import pl.vishop.vishopplugin.order.Order; @@ -49,7 +50,7 @@ public static boolean post(final OkHttpClient httpClient, final Config config, f } return true; - } catch (final IOException | JSONException exception) { + } catch (final IOException | JsonIOException exception) { Bukkit.getLogger().warning("Nieudane potwierdzenie zamówienia " + order.getOrderId().toString() + " w ViShop:"); Bukkit.getLogger().warning(exception.getMessage()); } diff --git a/src/main/java/pl/vishop/vishopplugin/request/PendingRequester.java b/src/main/java/pl/vishop/vishopplugin/request/PendingRequester.java index bed77d4..c8c4daa 100644 --- a/src/main/java/pl/vishop/vishopplugin/request/PendingRequester.java +++ b/src/main/java/pl/vishop/vishopplugin/request/PendingRequester.java @@ -16,25 +16,24 @@ package pl.vishop.vishopplugin.request; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.UUID; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonIOException; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Request.Builder; import okhttp3.Response; import okhttp3.ResponseBody; import org.bukkit.Bukkit; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; import pl.vishop.vishopplugin.ViShopPlugin; import pl.vishop.vishopplugin.config.Config; import pl.vishop.vishopplugin.order.Order; +import java.io.IOException; +import java.util.*; + public final class PendingRequester { private PendingRequester() {} @@ -57,24 +56,26 @@ public static Set get(final OkHttpClient httpClient, final Config config) throw new IOException("Puste body odpowiedzi"); } - final JSONArray orders = new JSONArray(responseBody.string()); - for (int i = 0; i < orders.length(); i++) { - final JSONObject order = orders.getJSONObject(i); - final JSONObject product = order.getJSONObject("product"); + final JsonArray orders = new JsonParser().parse(responseBody.string()).getAsJsonArray(); - final UUID orderId = UUID.fromString(order.getString("id")); - final String player = order.getString("player"); - final boolean requireOnline = product.getBoolean("require_player_online"); + for (JsonElement value : orders) { + final JsonObject order = value.getAsJsonObject(); + final JsonObject product = order.get("product").getAsJsonObject(); + + final UUID orderId = UUID.fromString(order.get("id").getAsString()); + final String player = order.get("player").getAsString(); + final boolean requireOnline = product.get("require_player_online").getAsBoolean(); final List commands = new ArrayList<>(); - final JSONArray commandsArray = product.getJSONArray("commands"); - for (int j = 0; j < commandsArray.length(); j++) { - commands.add(commandsArray.getString(j)); + final JsonArray commandsArray = product.get("commands").getAsJsonArray(); + for (JsonElement o : commandsArray) { + commands.add(o.getAsString()); } pendingOrders.add(new Order(orderId, player, requireOnline, commands)); } - } catch (final IOException | JSONException exception) { + + } catch (final IOException | JsonIOException exception) { Bukkit.getLogger().warning("Nieudane pobranie oczekujących zamówień z ViShop:"); Bukkit.getLogger().warning(exception.getMessage()); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index d25d1e4..88bf9b1 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,8 +1,11 @@ # Klucz API do autoryzacji połączeń +# Znajdziesz go wchodząc do panelu i wybierając zakładkę Ustawienia apiKey: "" # ID sklepu +# Jest na głównej stronie panelu sklepu shopId: "" # ID serwera w sklepie +# Znajduje się ono w zakładce Serwery w panelu serverId: "" \ No newline at end of file