2020-07-29 17:31:54 +00:00
|
|
|
package xyz.etztech.minealert.commands;
|
2020-07-21 03:17:08 +00:00
|
|
|
|
2020-07-29 19:34:28 +00:00
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonObject;
|
2020-07-21 03:17:08 +00:00
|
|
|
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;
|
2020-07-29 17:31:54 +00:00
|
|
|
import xyz.etztech.minealert.Color;
|
|
|
|
import xyz.etztech.minealert.Lang;
|
|
|
|
import xyz.etztech.minealert.MineAlert;
|
2020-07-29 19:34:28 +00:00
|
|
|
import xyz.etztech.minealert.Webhook;
|
2020-07-21 03:17:08 +00:00
|
|
|
|
|
|
|
public class MainCommand implements CommandExecutor {
|
|
|
|
|
|
|
|
|
2020-07-29 17:31:54 +00:00
|
|
|
MineAlert plugin;
|
2020-07-21 03:17:08 +00:00
|
|
|
|
2020-07-29 17:31:54 +00:00
|
|
|
public MainCommand(MineAlert plugin) {
|
2020-07-21 03:17:08 +00:00
|
|
|
this.plugin = plugin;
|
2020-07-29 17:31:54 +00:00
|
|
|
this.plugin.getCommand("minealert").setExecutor(this);
|
2020-07-21 03:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
|
2020-07-29 17:31:54 +00:00
|
|
|
if (!commandSender.hasPermission("minealert.admin")) {
|
2020-07-21 03:17:08 +00:00
|
|
|
Lang.NO_PERMISSION.sms(commandSender);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (args.length == 0) {
|
|
|
|
help(commandSender);
|
|
|
|
} else {
|
|
|
|
switch (args[0]) {
|
|
|
|
case "help":
|
|
|
|
help(commandSender);
|
|
|
|
break;
|
|
|
|
case "reload":
|
|
|
|
reload(commandSender);
|
|
|
|
break;
|
2020-07-29 19:34:28 +00:00
|
|
|
case "webhook":
|
|
|
|
webhook(commandSender);
|
|
|
|
break;
|
2020-07-21 03:17:08 +00:00
|
|
|
default:
|
|
|
|
Lang.UNKNOWN_COMMAND.sms(commandSender);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void help(CommandSender commandSender) {
|
2020-07-29 17:31:54 +00:00
|
|
|
String version = Bukkit.getPluginManager().getPlugin("MineAlert").getDescription().getVersion();
|
2020-07-21 03:17:08 +00:00
|
|
|
BaseComponent[] message = new ComponentBuilder()
|
2020-07-29 19:34:28 +00:00
|
|
|
.append(String.format("===== MineAlert v%s =====", version)).color(Color.PRIMARY.getChatColor())
|
|
|
|
.append("\n/minealert help - Show this message").color(Color.INFO.getChatColor())
|
|
|
|
.append("\n/minealert reload - Reload the config").color(Color.INFO.getChatColor())
|
|
|
|
.append("\n/minealert webhook - Test the global webhook").color(Color.INFO.getChatColor())
|
2020-07-21 03:17:08 +00:00
|
|
|
.create();
|
|
|
|
commandSender.spigot().sendMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void reload(CommandSender commandSender) {
|
|
|
|
this.plugin.reloadConfig();
|
|
|
|
Lang.PLUGIN_RELOADED.sms(commandSender);
|
|
|
|
}
|
2020-07-29 19:34:28 +00:00
|
|
|
|
|
|
|
private void webhook(CommandSender commandSender) {
|
|
|
|
String webhook = this.plugin.getConfig().getString("webhook", "");
|
|
|
|
if ("".equals(webhook)) {
|
|
|
|
Color.ERROR.sms(commandSender, "Global webhook cannot be blank to test.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> {
|
|
|
|
try {
|
|
|
|
Webhook.send(webhook, embed());
|
|
|
|
Color.INFO.sms(commandSender, "Webhook sent!");
|
|
|
|
} catch (Exception e) {
|
|
|
|
Lang.WEBHOOK_FAILED.sms(commandSender);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private String embed() {
|
|
|
|
JsonObject json = new JsonObject();
|
|
|
|
JsonArray embeds = new JsonArray();
|
|
|
|
|
|
|
|
JsonObject embed = new JsonObject();
|
2020-07-29 19:48:05 +00:00
|
|
|
embed.addProperty("color", Color.PRIMARY.getInt());
|
2020-07-29 19:34:28 +00:00
|
|
|
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();
|
|
|
|
}
|
2020-07-21 03:17:08 +00:00
|
|
|
}
|