Use Javacord

Signed-off-by: Etzelia <etzelia@hotmail.com>
grief
Etzelia 2020-07-30 16:52:47 -05:00
parent 316efb0ba7
commit be57583491
No known key found for this signature in database
GPG Key ID: 3CAEB74806C4ADE5
6 changed files with 55 additions and 77 deletions

View File

@ -34,6 +34,11 @@
<version>1.16.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xyz.etztech</groupId>
<artifactId>javacord</artifactId>
<version>0.0.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
@ -47,6 +52,10 @@
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>etztech-repo</id>
<url>http://repo.etztech.xyz/</url>
</repository>
<repository>
<id>mvn-repo</id>
<url>https://mvnrepository.com/artifact/</url>

View File

@ -4,7 +4,6 @@ import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
public enum Color {
DEFAULT("#AAAAAA"),

View File

@ -5,6 +5,11 @@ import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.minealert.commands.MainCommand;
import xyz.etztech.minealert.listeners.BlockBreakListener;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
public class MineAlert extends JavaPlugin {
@ -62,5 +67,23 @@ public class MineAlert extends JavaPlugin {
}
return def;
}
public static void sendWebhook(String webhook, String embed) throws Exception {
URL url = new URL(webhook);
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setDoOutput(true);
byte[] out = embed.getBytes(StandardCharsets.UTF_8);
int length = out.length;
http.setFixedLengthStreamingMode(length);
http.setRequestProperty("Content-Type", "application/json; utf-8");
http.setRequestProperty("User-Agent", "MineAlert Agent");
try (OutputStream os = http.getOutputStream()) {
os.write(out, 0, out.length);
}
}
}

View File

@ -1,28 +0,0 @@
package xyz.etztech.minealert;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
public class Webhook {
public static void send(String webhook, String embed) throws Exception {
URL url = new URL(webhook);
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setDoOutput(true);
byte[] out = embed.getBytes(StandardCharsets.UTF_8);
int length = out.length;
http.setFixedLengthStreamingMode(length);
http.setRequestProperty("Content-Type", "application/json; utf-8");
http.setRequestProperty("User-Agent", "MineAlert Agent");
try (OutputStream os = http.getOutputStream()) {
os.write(out, 0, out.length);
}
}
}

View File

@ -1,17 +1,17 @@
package xyz.etztech.minealert.commands;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import xyz.etztech.embed.Author;
import xyz.etztech.embed.Embed;
import xyz.etztech.embed.Webhook;
import xyz.etztech.minealert.Color;
import xyz.etztech.minealert.Lang;
import xyz.etztech.minealert.MineAlert;
import xyz.etztech.minealert.Webhook;
public class MainCommand implements CommandExecutor {
@ -75,7 +75,7 @@ public class MainCommand implements CommandExecutor {
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> {
try {
Webhook.send(webhook, embed());
MineAlert.sendWebhook(webhook, embed());
Color.INFO.sms(commandSender, "Webhook sent!");
} catch (Exception e) {
Lang.WEBHOOK_FAILED.sms(commandSender);
@ -85,21 +85,11 @@ public class MainCommand implements CommandExecutor {
}
private String embed() {
JsonObject json = new JsonObject();
JsonArray embeds = new JsonArray();
Embed embed = new Embed()
.color(Color.PRIMARY.getInt())
.description("Test Message")
.author(new Author("Console", "", "https://minotar.net/helm/Notch/100.png", ""));
JsonObject embed = new JsonObject();
embed.addProperty("color", Color.PRIMARY.getInt());
embed.addProperty("description", "Test Message");
JsonObject author = new JsonObject();
author.addProperty("name", "Console");
author.addProperty("icon_url", "https://minotar.net/helm/Notch/100.png");
embed.add("author", author);
embeds.add(embed);
json.add("embeds", embeds);
return json.toString();
return new Webhook("", embed).toString();
}
}

View File

@ -1,27 +1,22 @@
package xyz.etztech.minealert.listeners;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import xyz.etztech.embed.Author;
import xyz.etztech.embed.Embed;
import xyz.etztech.embed.Webhook;
import xyz.etztech.minealert.Color;
import xyz.etztech.minealert.Lang;
import xyz.etztech.minealert.MineAlert;
import xyz.etztech.minealert.Webhook;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -169,7 +164,7 @@ public class BlockBreakListener implements Listener {
String embed = embed(event, message, ping, hexColor);
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> {
try {
Webhook.send(webhook, embed);
MineAlert.sendWebhook(webhook, embed);
} catch (Exception e) {
this.plugin.log(Lang.WEBHOOK_FAILED.getMessage());
}
@ -178,25 +173,15 @@ public class BlockBreakListener implements Listener {
}
private String embed(BlockEvent event, String message, boolean ping, String hexColor) {
JsonObject json = new JsonObject();
if (ping) {
json.addProperty("content", "@here");
}
JsonArray embeds = new JsonArray();
JsonObject embed = new JsonObject();
embed.addProperty("color", Color.hexToInt(hexColor));
embed.addProperty("description", message);
JsonObject author = new JsonObject();
author.addProperty("name", event.getPlayer().getName());
author.addProperty("icon_url", String.format("https://minotar.net/helm/%s/100.png", event.getPlayer().getName()));
embed.add("author", author);
embeds.add(embed);
json.add("embeds", embeds);
return json.toString();
Embed embed = new Embed()
.color(Color.hexToInt(hexColor))
.description(message)
.timestamp(OffsetDateTime.now())
.author(new Author(event.getPlayer().getName(),
"",
String.format("https://minotar.net/helm/%s/100.png", event.getPlayer().getName()),
""));
return new Webhook(ping ? "@here": "", embed).toString();
}
}