2018-09-12 15:53:47 +00:00
|
|
|
package xyz.etztech.qol.listeners;
|
|
|
|
|
|
|
|
import net.md_5.bungee.api.chat.ClickEvent;
|
|
|
|
import net.md_5.bungee.api.chat.TextComponent;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.bukkit.ChatColor;
|
2018-09-22 19:18:06 +00:00
|
|
|
import org.bukkit.GameMode;
|
2018-09-12 15:53:47 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
2018-09-12 16:06:02 +00:00
|
|
|
import xyz.etztech.core.CoreUtils;
|
|
|
|
import xyz.etztech.core.web.CoreWeb;
|
2018-09-12 15:53:47 +00:00
|
|
|
import xyz.etztech.qol.QoL;
|
|
|
|
import xyz.etztech.qol.other.LinkCommand;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2018-09-23 03:27:02 +00:00
|
|
|
import java.util.UUID;
|
2018-09-12 15:53:47 +00:00
|
|
|
|
|
|
|
public class CommandPreprocessListener implements Listener {
|
|
|
|
|
|
|
|
|
|
|
|
QoL plugin;
|
2018-09-23 03:27:02 +00:00
|
|
|
private Map<UUID, String> confirmTpMap = new HashMap<>();
|
2018-09-12 15:53:47 +00:00
|
|
|
|
|
|
|
public CommandPreprocessListener(QoL plugin) {
|
|
|
|
this.plugin = plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
|
|
|
|
|
|
|
|
String command = event.getMessage();
|
|
|
|
String base = command.split(" ")[0].substring(1).toLowerCase(); // Strip the slash
|
|
|
|
Player sender = event.getPlayer();
|
|
|
|
|
|
|
|
// Command Auditing
|
2018-09-12 16:06:02 +00:00
|
|
|
if (sender.hasPermission("qol.audit") && plugin.getConfig().getBoolean("audit.enabled")) {
|
|
|
|
boolean auditable = false;
|
2018-09-23 03:27:02 +00:00
|
|
|
boolean isTpCommand = false;
|
2018-09-22 19:50:07 +00:00
|
|
|
|
2018-09-23 03:27:02 +00:00
|
|
|
//check if the command is a tp command
|
2018-09-23 03:42:08 +00:00
|
|
|
if (noSlash(base).equals("tp") || noSlash(base).startsWith("tele") || noSlash(command).startsWith("lagg tpchunk")) {
|
2018-09-23 03:27:02 +00:00
|
|
|
//If the user is in the confirm tp map, remove them and let the command run
|
|
|
|
if (command.equals(confirmTpMap.get(sender.getUniqueId()))) {
|
|
|
|
confirmTpMap.remove(sender.getUniqueId());
|
|
|
|
}
|
|
|
|
//If the user is running the tp command for the first time outside of spec
|
|
|
|
else if (sender.getGameMode() != GameMode.SPECTATOR) {
|
|
|
|
//Cancel the command
|
|
|
|
event.setCancelled(true);
|
|
|
|
|
|
|
|
//Add the user to the tp confirm map
|
|
|
|
confirmTpMap.put(sender.getUniqueId(), command);
|
|
|
|
|
|
|
|
TextComponent message = new TextComponent(ChatColor.GREEN + "You are TPing out of spec, run command again to confirm.");
|
|
|
|
sender.spigot().sendMessage(message);
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 19:50:07 +00:00
|
|
|
|
2018-09-12 16:06:02 +00:00
|
|
|
for (String audit : QoL.getAudits()) {
|
|
|
|
if (noSlash(command).startsWith(noSlash(audit))) {
|
|
|
|
auditable = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 19:50:07 +00:00
|
|
|
|
2018-09-12 16:06:02 +00:00
|
|
|
if (auditable) {
|
|
|
|
Map<String, String> post = new HashMap<>();
|
2018-09-23 03:27:02 +00:00
|
|
|
|
2018-09-22 19:50:07 +00:00
|
|
|
|
2018-09-12 16:06:02 +00:00
|
|
|
post.put("username", "QoL Auditor");
|
|
|
|
post.put("content", "[" + StringUtils.capitalize(sender.getGameMode().name().toLowerCase()) + "] " + sender.getName() + " executed command: " + command);
|
|
|
|
String webhook = plugin.getConfig().getString("audit.webhook");
|
|
|
|
if (StringUtils.isNotEmpty(webhook)) {
|
|
|
|
CoreWeb.asyncPost(plugin, webhook, post);
|
|
|
|
}
|
2018-09-22 19:18:06 +00:00
|
|
|
|
2018-09-12 15:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Links
|
|
|
|
for (LinkCommand linkCommand : QoL.getLinks()) {
|
|
|
|
if (base.equalsIgnoreCase(linkCommand.getCommand())) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
TextComponent link = new TextComponent(ChatColor.GREEN + linkCommand.getMessage());
|
|
|
|
link.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, linkCommand.getUrl()));
|
|
|
|
sender.spigot().sendMessage(link);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:06:02 +00:00
|
|
|
public String noSlash(String command) {
|
|
|
|
return command.startsWith("/") ? command.substring(1) : command;
|
2018-09-12 15:53:47 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 16:06:02 +00:00
|
|
|
|
2018-09-12 15:53:47 +00:00
|
|
|
}
|