MineAlert/src/main/java/xyz/etztech/minealert/commands/MainCommand.java

106 lines
3.6 KiB
Java
Raw Normal View History

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.minealert.Color;
import xyz.etztech.minealert.Lang;
import xyz.etztech.minealert.MineAlert;
import xyz.etztech.minealert.Webhook;
public class MainCommand implements CommandExecutor {
MineAlert plugin;
public MainCommand(MineAlert plugin) {
this.plugin = plugin;
this.plugin.getCommand("minealert").setExecutor(this);
}
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
if (!commandSender.hasPermission("minealert.admin")) {
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;
case "webhook":
webhook(commandSender);
break;
default:
Lang.UNKNOWN_COMMAND.sms(commandSender);
break;
}
}
return true;
}
private void help(CommandSender commandSender) {
String version = Bukkit.getPluginManager().getPlugin("MineAlert").getDescription().getVersion();
BaseComponent[] message = new ComponentBuilder()
.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())
.create();
commandSender.spigot().sendMessage(message);
}
private void reload(CommandSender commandSender) {
this.plugin.reloadConfig();
Lang.PLUGIN_RELOADED.sms(commandSender);
}
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();
embed.addProperty("color", Color.hexToInt("#3273dc"));
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();
}
}