T2C-AutoResponse/src/main/java/net/t2code/autoresponse/Spigot/event/ResponseListener.java

126 lines
5.5 KiB
Java

package net.t2code.autoresponse.Spigot.event;
import net.md_5.bungee.api.chat.ClickEvent;
import net.t2code.autoresponse.Util;
import net.t2code.autoresponse.Spigot.config.config.SelectConfig;
import net.t2code.autoresponse.Spigot.objects.ResponsesObject;
import net.t2code.autoresponse.Spigot.system.BCommand_Sender_Reciver;
import net.t2code.autoresponse.Spigot.Main;
import net.t2code.lib.Spigot.Lib.commands.Cmd;
import net.t2code.lib.Spigot.Lib.messages.TextBuilder;
import net.t2code.lib.Spigot.Lib.messages.send;
import net.t2code.lib.Spigot.Lib.plugins.PluginCheck;
import net.t2code.lib.Spigot.Lib.replace.Replace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.scheduler.BukkitRunnable;
public class ResponseListener implements Listener {
@EventHandler
public static void ChatListener(AsyncPlayerChatEvent e) {
Player player = e.getPlayer();
boolean bool = false;
for (String s : Main.allResponse) {
if (e.getMessage().contains(s)) bool = true;
}
if (!bool) return;
for (ResponsesObject response : Main.allResponses) {
if (!response.permNecessary || player.hasPermission(response.permission)) {
if (response.contains) {
for (String responseKey : response.responseKeys){
if (e.getMessage().contains(responseKey)) {
execute(e, player, response, responseKey);
return;
}
}
} else {
for (String responseKey : response.responseKeys){
if (e.getMessage().equals(responseKey)) {
execute(e, player, response, responseKey);
return;
}
}
}
}
}
}
private static void execute(AsyncPlayerChatEvent e, Player player, ResponsesObject response, String responseKey) {
if (response.protectionGameModeEnable) {
if (response.protectionGameModeMode.equalsIgnoreCase("blacklist") && response.protectionGameModeList.contains(player.getGameMode().toString())) {
return;
}
if (response.protectionGameModeMode.equalsIgnoreCase("whitelist") && !response.protectionGameModeList.contains(player.getGameMode().toString())) {
return;
}
}
if (response.protectionWorldEnable) {
if (response.protectionWorldMode.equalsIgnoreCase("blacklist") && response.protectionWorldList.contains(player.getWorld().getName())) {
return;
}
if (response.protectionWorldMode.equalsIgnoreCase("whitelist") && !response.protectionWorldList.contains(player.getWorld().getName())) {
return;
}
}
use(e, player, responseKey);
if (response.commandEnable) {
for (String cmd : response.command) {
if (response.bungeeCommand) {
if (SelectConfig.bungee) {
BCommand_Sender_Reciver.sendToBungee(player, cmd.replace("[player]", player.getName()), response.commandAsConsole);
} else {
send.console(Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config.");
send.sender(player, Util.getPrefix() + " §4To use bungee commands, enable the Bungee option in the config.");
}
} else {
if (response.commandAsConsole) {
new BukkitRunnable() {
@Override
public void run() {
Cmd.console(cmd.replace("[player]", player.getName()));
}
}.runTaskLater(Main.plugin, 1L);
} else {
Cmd.player(player, cmd.replace("[player]", player.getName()));
}
}
}
}
if (response.messageEnable) {
for (String msg : response.messages) {
String text;
String hover;
if (PluginCheck.papi()) {
text = Replace.replace(Util.getPrefix(), player, msg);
hover = Replace.replace(Util.getPrefix(), player, response.hover);
} else {
text = Replace.replace(Util.getPrefix(), msg);
hover = Replace.replace(Util.getPrefix(), response.hover);
}
if (response.textBuilder) {
TextBuilder textBuilder = new TextBuilder(text);
textBuilder.addHover(hover);
if (response.clickEvent) {
textBuilder.addClickEvent(ClickEvent.Action.valueOf(response.action), response.actionValue);
}
player.spigot().sendMessage(textBuilder.build());
} else {
send.player(player, text);
}
}
}
}
private static void use(AsyncPlayerChatEvent e, Player player, String responseKey) {
e.setCancelled(true);
if (SelectConfig.logConsole) send.console(Util.getPrefix() + " §6Player: §e" + player.getName() + " §6use AutoResponse: §e" +responseKey);
}
}