Skip to content

Commit

Permalink
Use GSON instead of org.json (resolves #4)
Browse files Browse the repository at this point in the history
* Change JSON library and config error messages
* Update config.yml
* Some changes
  • Loading branch information
NotroDev authored Nov 11, 2022
1 parent 1992b84 commit 2003cf1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()}"
}
7 changes: 4 additions & 3 deletions src/main/java/pl/vishop/vishopplugin/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
package pl.vishop.vishopplugin.request;

import java.io.IOException;

import com.google.gson.JsonIOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Request.Builder;
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;
Expand All @@ -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());
}
Expand Down
41 changes: 21 additions & 20 deletions src/main/java/pl/vishop/vishopplugin/request/PendingRequester.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand All @@ -57,24 +56,26 @@ public static Set<Order> 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<String> 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());
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -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: ""

0 comments on commit 2003cf1

Please sign in to comment.