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-27 19:50:08 +00:00
|
|
|
import org.bukkit.event.server.ServerCommandEvent;
|
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;
|
2018-09-23 19:49:17 +00:00
|
|
|
import org.dynmap.DynmapAPI;
|
2018-09-12 15:53:47 +00:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2018-09-23 03:27:02 +00:00
|
|
|
import java.util.UUID;
|
2018-09-26 20:11:36 +00:00
|
|
|
import java.util.Collection;
|
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-23 19:49:17 +00:00
|
|
|
private Map<UUID, Boolean> dynmapVisibleStatusMap = new HashMap<>();
|
2018-09-12 15:53:47 +00:00
|
|
|
|
|
|
|
public CommandPreprocessListener(QoL plugin) {
|
|
|
|
this.plugin = plugin;
|
2018-09-23 19:49:17 +00:00
|
|
|
}
|
2018-09-12 15:53:47 +00:00
|
|
|
|
2018-09-27 19:50:08 +00:00
|
|
|
@EventHandler
|
2018-09-27 20:29:59 +00:00
|
|
|
public void onServerCommand(ServerCommandEvent event) {
|
2018-09-27 19:50:08 +00:00
|
|
|
if (plugin.getConfig().getBoolean("audit.enabled")) {
|
|
|
|
String command = event.getCommand();
|
|
|
|
|
|
|
|
if (commandIsAuditable(command)) {
|
|
|
|
sendAuditWebhook("Console", null, command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 15:53:47 +00:00
|
|
|
@EventHandler
|
|
|
|
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
|
|
|
|
|
|
|
|
String command = event.getMessage();
|
|
|
|
String base = command.split(" ")[0].substring(1).toLowerCase(); // Strip the slash
|
2018-09-25 18:36:11 +00:00
|
|
|
|
2018-09-12 15:53:47 +00:00
|
|
|
Player sender = event.getPlayer();
|
2018-10-24 19:19:03 +00:00
|
|
|
DynmapAPI dynmap = plugin.getDynmap();
|
2018-09-12 15:53:47 +00:00
|
|
|
|
2018-09-23 19:49:17 +00:00
|
|
|
// Spec dynmap hide
|
2018-10-16 02:32:13 +00:00
|
|
|
if (base.equals("spec") && sender.hasPermission("SafeSpectate.spectate") && dynmap != null) {
|
2018-10-15 22:07:01 +00:00
|
|
|
String message;
|
2018-09-23 19:49:17 +00:00
|
|
|
boolean visibleStatus;
|
|
|
|
if (sender.getGameMode() != GameMode.SPECTATOR){
|
2018-09-23 22:19:22 +00:00
|
|
|
dynmapVisibleStatusMap.put(sender.getUniqueId(), dynmap.getPlayerVisbility(sender.getName()));
|
2018-09-23 19:49:17 +00:00
|
|
|
visibleStatus = false;
|
2018-10-15 22:07:01 +00:00
|
|
|
|
2018-10-26 17:13:20 +00:00
|
|
|
message = "You are now being hidden on Dynmap.";
|
2018-10-15 22:07:01 +00:00
|
|
|
|
2018-09-23 19:49:17 +00:00
|
|
|
}
|
|
|
|
else if (dynmapVisibleStatusMap.containsKey(sender.getUniqueId())){
|
|
|
|
visibleStatus = dynmapVisibleStatusMap.remove(sender.getUniqueId());
|
2018-10-15 22:07:01 +00:00
|
|
|
|
|
|
|
String status = ((visibleStatus) ? "shown" : "hidden");
|
|
|
|
message = "Your " + status + " status has been restored on Dynmap.";
|
2018-09-23 19:49:17 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-10-15 22:07:01 +00:00
|
|
|
message = "You are now visible on the Dynmap.";
|
2018-09-23 19:49:17 +00:00
|
|
|
visibleStatus = true;
|
|
|
|
}
|
|
|
|
|
2018-10-15 22:07:01 +00:00
|
|
|
sender.spigot().sendMessage(new TextComponent(ChatColor.GREEN + message));
|
|
|
|
dynmap.setPlayerVisiblity(sender.getName(), visibleStatus);
|
2018-09-23 19:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Spec TP confirmation
|
2018-09-23 12:34:25 +00:00
|
|
|
if (sender.hasPermission("qol.tpconfirm")) {
|
2018-09-23 03:27:02 +00:00
|
|
|
//check if the command is a tp command
|
2018-09-23 22:19:22 +00:00
|
|
|
if (noSlash(base).equals("tp") || noSlash(base).toLowerCase().startsWith("tele") || noSlash(command).toLowerCase().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-23 12:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Command Auditing
|
2018-09-26 20:40:59 +00:00
|
|
|
if (sender.hasPermission("qol.auditable") && plugin.getConfig().getBoolean("audit.enabled")) {
|
|
|
|
if (sender.hasPermission("qol.auditor") && targetIsAuditable(command)) {
|
|
|
|
return;
|
|
|
|
} else {
|
2018-09-27 18:51:36 +00:00
|
|
|
if (commandIsAuditable(command)) {
|
|
|
|
sendAuditWebhook(sender.getName(), sender.getGameMode(), command);
|
2018-09-26 20:11:36 +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-27 18:51:36 +00:00
|
|
|
public boolean commandIsAuditable(String command) {
|
|
|
|
for (String audit : QoL.getAudits()) {
|
|
|
|
if (noSlash(command).startsWith(noSlash(audit))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendAuditWebhook(String commandSource, GameMode gamemode, String command) {
|
|
|
|
|
|
|
|
String content = "";
|
|
|
|
|
|
|
|
if (gamemode != null) {
|
|
|
|
content += "[" + StringUtils.capitalize(gamemode.name().toLowerCase()) + "] ";
|
|
|
|
}
|
|
|
|
|
|
|
|
content += commandSource + " executed command: " + command;
|
|
|
|
|
|
|
|
sendWebhook("QoL Auditor", content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendWebhook(String username, String content) {
|
|
|
|
String webhook = plugin.getConfig().getString("audit.webhook");
|
|
|
|
|
|
|
|
if (StringUtils.isNotEmpty(webhook)) {
|
|
|
|
Map<String, String> post = new HashMap<>();
|
|
|
|
post.put("username", username);
|
|
|
|
post.put("content", content);
|
|
|
|
CoreWeb.asyncPost(plugin, webhook, post);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:11:36 +00:00
|
|
|
public boolean targetIsAuditable(String command) {
|
|
|
|
String[] commandSplit = command.split(" ");
|
|
|
|
Collection<? extends Player> players = plugin.getServer().getOnlinePlayers();
|
|
|
|
|
|
|
|
for (String param : commandSplit) {
|
2018-09-26 20:40:59 +00:00
|
|
|
String lowerCaseParam = param.toLowerCase();
|
2018-09-26 20:11:36 +00:00
|
|
|
for (Player p : players) {
|
2018-09-26 20:40:59 +00:00
|
|
|
|
|
|
|
if (p.getName().toLowerCase().equals(lowerCaseParam)) {
|
2018-09-26 20:11:36 +00:00
|
|
|
return p.hasPermission("qol.auditable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|