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> <version>1.16.1-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>xyz.etztech</groupId>
<artifactId>javacord</artifactId>
<version>0.0.2</version>
</dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang</artifactId>
@ -47,6 +52,10 @@
<id>spigotmc-repo</id> <id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url> <url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository> </repository>
<repository>
<id>etztech-repo</id>
<url>http://repo.etztech.xyz/</url>
</repository>
<repository> <repository>
<id>mvn-repo</id> <id>mvn-repo</id>
<url>https://mvnrepository.com/artifact/</url> <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 net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
public enum Color { public enum Color {
DEFAULT("#AAAAAA"), DEFAULT("#AAAAAA"),

View File

@ -5,6 +5,11 @@ import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.minealert.commands.MainCommand; import xyz.etztech.minealert.commands.MainCommand;
import xyz.etztech.minealert.listeners.BlockBreakListener; 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; import java.util.logging.Logger;
public class MineAlert extends JavaPlugin { public class MineAlert extends JavaPlugin {
@ -62,5 +67,23 @@ public class MineAlert extends JavaPlugin {
} }
return def; 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; 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.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; 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.Color;
import xyz.etztech.minealert.Lang; import xyz.etztech.minealert.Lang;
import xyz.etztech.minealert.MineAlert; import xyz.etztech.minealert.MineAlert;
import xyz.etztech.minealert.Webhook;
public class MainCommand implements CommandExecutor { public class MainCommand implements CommandExecutor {
@ -75,7 +75,7 @@ public class MainCommand implements CommandExecutor {
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> { this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> {
try { try {
Webhook.send(webhook, embed()); MineAlert.sendWebhook(webhook, embed());
Color.INFO.sms(commandSender, "Webhook sent!"); Color.INFO.sms(commandSender, "Webhook sent!");
} catch (Exception e) { } catch (Exception e) {
Lang.WEBHOOK_FAILED.sms(commandSender); Lang.WEBHOOK_FAILED.sms(commandSender);
@ -85,21 +85,11 @@ public class MainCommand implements CommandExecutor {
} }
private String embed() { private String embed() {
JsonObject json = new JsonObject(); Embed embed = new Embed()
JsonArray embeds = new JsonArray(); .color(Color.PRIMARY.getInt())
.description("Test Message")
.author(new Author("Console", "", "https://minotar.net/helm/Notch/100.png", ""));
JsonObject embed = new JsonObject(); return new Webhook("", embed).toString();
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();
} }
} }

View File

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