Skip to content

Commit

Permalink
Merge pull request #1 from Kamilkime-Plugins/main
Browse files Browse the repository at this point in the history
Drobne zmiany
  • Loading branch information
ivall authored May 8, 2022
2 parents 6935eae + 7e4e1ae commit 9baa6a5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# VIshop-plugin
Plugin for Minecraft itemshop - VIshop.pl
Plugin łączący serwer Minecraft z itemshopem stworzonym na [VIshop.pl](https://vishop.pl/)</br>
Plugin przeznaczony dla silników pochodnych Bukkita (Spigota, Papera, itp.)

Author of plugin: Author of plugin: [Kamilkime](https://github.com/Kamilkime)
Autor pluginu: [Kamilkime](https://github.com/Kamilkime)
15 changes: 8 additions & 7 deletions src/main/java/pl/vishop/vishopplugin/PendingTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ public void run() {
return;
}

Bukkit.getScheduler().runTask(this.plugin, () -> {
pendingOrders.forEach(order -> {
order.getCommands().forEach(command -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
});
});
pendingOrders.stream()
.filter(order -> !order.requiresOnline() || Bukkit.getPlayerExact(order.getPlayer()) != null)
.filter(order -> ConfirmRequester.post(this.httpClient, this.config, order))
.forEach(this::executeCommands);
}

pendingOrders.forEach(order -> {
ConfirmRequester.post(this.httpClient, this.config, order);
private void executeCommands(final Order order) {
Bukkit.getScheduler().runTask(this.plugin, () -> {
order.getCommands().forEach(command -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command));
});
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/pl/vishop/vishopplugin/ViShopPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

public final class ViShopPlugin extends JavaPlugin {

public static final String BACKEND_ADDRESS = "https://dev123.vishop.pl/panel/shops/%1$s/servers/%2$s/payments/%3$s";
private final OkHttpClient httpClient = new OkHttpClient.Builder().build();

@Override
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/pl/vishop/vishopplugin/order/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ public class Order {

private final UUID orderId;
private final String player;
private final boolean requireOnline;
private final List<String> commands;

public Order(final UUID orderId, final String player, final List<String> commands) {
public Order(final UUID orderId, final String player, final boolean requireOnline, final List<String> commands) {
this.orderId = orderId;
this.player = player;
this.requireOnline = requireOnline;
this.commands = commands;
}

Expand All @@ -25,6 +27,10 @@ public String getPlayer() {
return this.player;
}

public boolean requiresOnline() {
return this.requireOnline;
}

public List<String> getCommands() {
return this.commands.stream().map(command -> StringUtils.replace(command, "{NICK}", this.player)).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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 @@ -34,7 +35,7 @@ public final class ConfirmRequester {

private ConfirmRequester() {}

public static void post(final OkHttpClient httpClient, final Config config, final Order order) {
public static boolean post(final OkHttpClient httpClient, final Config config, final Order order) {
final Request request = new Builder()
.url(getUrl(config, order))
.header("User-Agent", "ViShopPlugin/1.0")
Expand All @@ -46,15 +47,18 @@ public static void post(final OkHttpClient httpClient, final Config config, fina
if (!response.isSuccessful()) {
throw new IOException("Otrzymany kod odpowiedzi " + response.code());
}

return true;
} catch (final IOException | JSONException exception) {
Bukkit.getLogger().warning("Nieudane potwierdzenie zamówienia " + order.getOrderId().toString() + " w ViShop:");
Bukkit.getLogger().warning(exception.getMessage());
}

return false;
}

private static String getUrl(final Config config, final Order order) {
return "https://dev123.vishop.pl/panel/shops/" + config.shopId + "/servers/" + config.serverId + "/payments/"
+ order.getOrderId().toString() + "/";
return String.format(ViShopPlugin.BACKEND_ADDRESS, config.shopId, config.serverId, order.getOrderId().toString() + "/");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;

Expand Down Expand Up @@ -59,22 +60,19 @@ public static Set<Order> get(final OkHttpClient httpClient, final Config config)
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 UUID orderId = UUID.fromString(order.getString("id"));
final String player = order.getString("player");
final boolean requirePlayerOnline = order.getJSONObject("product").getBoolean("require_player_online");
if (Bukkit.getServer().getPlayer(player) == null && requirePlayerOnline) {
Bukkit.getLogger().warning("Nieudane wykonanie zamówienia "+ orderId +". Gracz nie jest na serwerze.");
continue;
}
final boolean requireOnline = product.getBoolean("require_player_online");
final List<String> commands = new ArrayList<>();

final JSONArray commandsArray = order.getJSONObject("product").getJSONArray("commands");
final JSONArray commandsArray = product.getJSONArray("commands");
for (int j = 0; j < commandsArray.length(); j++) {
commands.add(commandsArray.getString(j));
}

pendingOrders.add(new Order(orderId, player, commands));
pendingOrders.add(new Order(orderId, player, requireOnline, commands));
}
} catch (final IOException | JSONException exception) {
Bukkit.getLogger().warning("Nieudane pobranie oczekujących zamówień z ViShop:");
Expand All @@ -85,7 +83,7 @@ public static Set<Order> get(final OkHttpClient httpClient, final Config config)
}

private static String getUrl(final Config config) {
return "https://dev123.vishop.pl/panel/shops/" + config.shopId + "/servers/" + config.serverId + "/payments/?status=executing";
return String.format(ViShopPlugin.BACKEND_ADDRESS, config.shopId, config.serverId, "?status=executing");
}

}

0 comments on commit 9baa6a5

Please sign in to comment.