|
| 1 | +package org.simpmc.simppay.service; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import okhttp3.MediaType; |
| 6 | +import okhttp3.OkHttpClient; |
| 7 | +import okhttp3.Request; |
| 8 | +import okhttp3.RequestBody; |
| 9 | +import okhttp3.Response; |
| 10 | +import org.bukkit.Bukkit; |
| 11 | +import org.bukkit.event.EventHandler; |
| 12 | +import org.bukkit.event.EventPriority; |
| 13 | +import org.bukkit.event.Listener; |
| 14 | +import org.simpmc.simppay.SPPlugin; |
| 15 | +import org.simpmc.simppay.config.ConfigManager; |
| 16 | +import org.simpmc.simppay.config.types.DiscordConfig; |
| 17 | +import org.simpmc.simppay.config.types.DiscordConfig.EventConfig; |
| 18 | +import org.simpmc.simppay.data.PaymentType; |
| 19 | +import org.simpmc.simppay.event.MilestoneCompleteEvent; |
| 20 | +import org.simpmc.simppay.event.PaymentSuccessEvent; |
| 21 | +import org.simpmc.simppay.model.detail.CardDetail; |
| 22 | +import org.simpmc.simppay.util.MessageUtil; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.UUID; |
| 29 | + |
| 30 | +public class DiscordService implements IService, Listener { |
| 31 | + |
| 32 | + private OkHttpClient client; |
| 33 | + |
| 34 | + @Override |
| 35 | + public void setup() { |
| 36 | + client = new OkHttpClient(); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void shutdown() { |
| 41 | + if (client != null) { |
| 42 | + client.dispatcher().executorService().shutdown(); |
| 43 | + client.connectionPool().evictAll(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @EventHandler(priority = EventPriority.MONITOR) |
| 48 | + public void onPaymentSuccess(PaymentSuccessEvent event) { |
| 49 | + DiscordConfig config = ConfigManager.getInstance().getConfig(DiscordConfig.class); |
| 50 | + if (!config.enabled) return; |
| 51 | + |
| 52 | + UUID playerUUID = event.getPlayerUUID(); |
| 53 | + String playerName = Objects.requireNonNullElse(Bukkit.getOfflinePlayer(playerUUID).getName(), playerUUID.toString()); |
| 54 | + |
| 55 | + if (event.getPaymentType() == PaymentType.CARD) { |
| 56 | + CardDetail cardDetail = (CardDetail) event.getPaymentDetail(); |
| 57 | + Map<String, String> placeholders = new HashMap<>(); |
| 58 | + placeholders.put("{player}", playerName); |
| 59 | + placeholders.put("{amount}", String.valueOf((long) event.getAmount())); |
| 60 | + placeholders.put("{trueAmount}", String.valueOf((long) event.getTrueAmount())); |
| 61 | + placeholders.put("{gateway}", cardDetail.getType().toString()); |
| 62 | + placeholders.put("{paymentId}", event.getPaymentID().toString()); |
| 63 | + placeholders.put("{paymentType}", "CARD"); |
| 64 | + dispatch(config.cardPayment, placeholders); |
| 65 | + } else if (event.getPaymentType() == PaymentType.BANKING) { |
| 66 | + Map<String, String> placeholders = new HashMap<>(); |
| 67 | + placeholders.put("{player}", playerName); |
| 68 | + placeholders.put("{amount}", String.valueOf((long) event.getAmount())); |
| 69 | + placeholders.put("{trueAmount}", String.valueOf((long) event.getAmount())); |
| 70 | + placeholders.put("{gateway}", "BANKING"); |
| 71 | + placeholders.put("{paymentId}", event.getPaymentID().toString()); |
| 72 | + placeholders.put("{paymentType}", "BANKING"); |
| 73 | + dispatch(config.bankPayment, placeholders); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @EventHandler |
| 78 | + public void onMilestoneComplete(MilestoneCompleteEvent event) { |
| 79 | + DiscordConfig config = ConfigManager.getInstance().getConfig(DiscordConfig.class); |
| 80 | + if (!config.enabled) return; |
| 81 | + |
| 82 | + Map<String, String> placeholders = new HashMap<>(); |
| 83 | + placeholders.put("{milestoneType}", event.getType().name()); |
| 84 | + placeholders.put("{milestoneAmount}", String.valueOf(event.getMilestoneConfig().amount)); |
| 85 | + placeholders.put("{currentAmount}", String.valueOf(event.getCurrentAmount())); |
| 86 | + |
| 87 | + if (event.getPlayerUUID() == null) { |
| 88 | + dispatch(config.serverMilestone, placeholders); |
| 89 | + } else { |
| 90 | + String playerName = Objects.requireNonNullElse( |
| 91 | + Bukkit.getOfflinePlayer(event.getPlayerUUID()).getName(), |
| 92 | + event.getPlayerUUID().toString() |
| 93 | + ); |
| 94 | + placeholders.put("{player}", playerName); |
| 95 | + dispatch(config.playerMilestone, placeholders); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void dispatch(EventConfig eventConfig, Map<String, String> placeholders) { |
| 100 | + if (!eventConfig.enabled || eventConfig.webhookUrl.isBlank()) return; |
| 101 | + |
| 102 | + String payload = buildPayload(eventConfig, placeholders); |
| 103 | + |
| 104 | + SPPlugin.getInstance().getFoliaLib().getScheduler().runAsync(task -> { |
| 105 | + RequestBody body = RequestBody.create(payload, MediaType.parse("application/json")); |
| 106 | + Request request = new Request.Builder() |
| 107 | + .url(eventConfig.webhookUrl) |
| 108 | + .post(body) |
| 109 | + .build(); |
| 110 | + try (Response response = client.newCall(request).execute()) { |
| 111 | + if (!response.isSuccessful()) { |
| 112 | + MessageUtil.warn("[DiscordService] Webhook returned non-2xx: " + response.code()); |
| 113 | + } |
| 114 | + } catch (IOException e) { |
| 115 | + MessageUtil.warn("[DiscordService] Failed to send Discord webhook: " + e.getMessage()); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + private String buildPayload(EventConfig eventConfig, Map<String, String> placeholders) { |
| 121 | + JsonObject root = new JsonObject(); |
| 122 | + if (!eventConfig.botUsername.isBlank()) { |
| 123 | + root.addProperty("username", eventConfig.botUsername); |
| 124 | + } |
| 125 | + if (!eventConfig.avatarUrl.isBlank()) { |
| 126 | + root.addProperty("avatar_url", eventConfig.avatarUrl); |
| 127 | + } |
| 128 | + |
| 129 | + JsonObject embed = new JsonObject(); |
| 130 | + embed.addProperty("title", applyPlaceholders(eventConfig.embed.title, placeholders)); |
| 131 | + embed.addProperty("description", applyPlaceholders(eventConfig.embed.description, placeholders)); |
| 132 | + embed.addProperty("color", parseColor(eventConfig.embed.color)); |
| 133 | + |
| 134 | + if (!eventConfig.embed.thumbnailUrl.isBlank()) { |
| 135 | + JsonObject thumbnail = new JsonObject(); |
| 136 | + thumbnail.addProperty("url", eventConfig.embed.thumbnailUrl); |
| 137 | + embed.add("thumbnail", thumbnail); |
| 138 | + } |
| 139 | + |
| 140 | + if (!eventConfig.embed.footerText.isBlank()) { |
| 141 | + JsonObject footer = new JsonObject(); |
| 142 | + footer.addProperty("text", applyPlaceholders(eventConfig.embed.footerText, placeholders)); |
| 143 | + embed.add("footer", footer); |
| 144 | + } |
| 145 | + |
| 146 | + if (eventConfig.embed.fields != null && !eventConfig.embed.fields.isEmpty()) { |
| 147 | + JsonArray fields = new JsonArray(); |
| 148 | + for (DiscordConfig.FieldConfig field : eventConfig.embed.fields) { |
| 149 | + JsonObject fieldObj = new JsonObject(); |
| 150 | + fieldObj.addProperty("name", applyPlaceholders(field.name, placeholders)); |
| 151 | + fieldObj.addProperty("value", applyPlaceholders(field.value, placeholders)); |
| 152 | + fieldObj.addProperty("inline", field.inline); |
| 153 | + fields.add(fieldObj); |
| 154 | + } |
| 155 | + embed.add("fields", fields); |
| 156 | + } |
| 157 | + |
| 158 | + JsonArray embeds = new JsonArray(); |
| 159 | + embeds.add(embed); |
| 160 | + root.add("embeds", embeds); |
| 161 | + |
| 162 | + return root.toString(); |
| 163 | + } |
| 164 | + |
| 165 | + private String applyPlaceholders(String text, Map<String, String> placeholders) { |
| 166 | + if (text == null) return ""; |
| 167 | + for (Map.Entry<String, String> entry : placeholders.entrySet()) { |
| 168 | + text = text.replace(entry.getKey(), entry.getValue()); |
| 169 | + } |
| 170 | + return text; |
| 171 | + } |
| 172 | + |
| 173 | + private int parseColor(String hex) { |
| 174 | + try { |
| 175 | + String clean = hex.startsWith("#") ? hex.substring(1) : hex; |
| 176 | + return Integer.parseInt(clean, 16); |
| 177 | + } catch (NumberFormatException e) { |
| 178 | + return 5763719; // default green |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments