From cf77cae518cc76a512968cdc5117fac1e87abfe4 Mon Sep 17 00:00:00 2001 From: RayzsYT <51991564+RayzsYT@users.noreply.github.com> Date: Sun, 10 Nov 2024 04:52:21 +0100 Subject: [PATCH] Added customizable actions --- .../de/rayzs/pat/utils/subargs/Responses.java | 111 +++++++++++++++++- 1 file changed, 105 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/rayzs/pat/utils/subargs/Responses.java b/src/main/java/de/rayzs/pat/utils/subargs/Responses.java index 4c94c898..4d2faac6 100644 --- a/src/main/java/de/rayzs/pat/utils/subargs/Responses.java +++ b/src/main/java/de/rayzs/pat/utils/subargs/Responses.java @@ -1,8 +1,10 @@ package de.rayzs.pat.utils.subargs; import de.rayzs.pat.plugin.modules.subargs.SubArgsModule; +import de.rayzs.pat.plugin.logger.Logger; import de.rayzs.pat.api.storage.Storage; -import de.rayzs.pat.utils.StringUtils; +import de.rayzs.pat.plugin.*; +import de.rayzs.pat.utils.*; import java.util.*; public class Responses { @@ -20,11 +22,11 @@ public class Responses { }); } - public static List getResponse(String input) { - return getResponse(input, Storage.ConfigSections.Settings.CANCEL_COMMAND.SUB_COMMAND_RESPONSE.getLines()); + public static List getResponse(UUID uuid, String input) { + return getResponse(uuid, input, Storage.ConfigSections.Settings.CANCEL_COMMAND.SUB_COMMAND_RESPONSE.getLines()); } - public static List getResponse(String input, List defaultMessage) { + public static List getResponse(UUID uuid, String input, List defaultMessage) { final String finalInput = input.startsWith("/") ? StringUtils.replaceFirst(input, "/", "") : input; final Optional optionalResponse = RESPONSES.stream().filter(response -> { @@ -34,17 +36,114 @@ public class Responses { return false; }).findFirst(); - return optionalResponse.isPresent() ? optionalResponse.get().message : defaultMessage; + + if(uuid != null && optionalResponse.isPresent()) { + optionalResponse.get().executeAction(uuid); + return optionalResponse.get().message; + } + + return defaultMessage; } private static class Response { - private final List triggers, message; + private final List triggers, message, actions; public Response(final String key) { this.triggers = (List) Storage.Files.CUSTOM_RESPONSES.get(key + ".triggers"); this.message = (List) Storage.Files.CUSTOM_RESPONSES.getOrSet(key + ".message", SubArgsModule.BLOCKED_MESSAGE); + this.actions = (List) Storage.Files.CUSTOM_RESPONSES.getOrSet(key + ".actions", new ArrayList<>()); } + public void executeAction(UUID uuid) { + String[] split; + for (String action : actions) { + if(!action.contains("::")) { + Logger.warning("Could not recognise action: " + action); + Logger.warning("Syntax does not match at all. Have you used the splitters (::) correctly?"); + continue; + } + + split = action.split("::"); + + switch (split[0].toLowerCase()) { + case "title": + if(split.length != 3) { + Logger.warning("Could not recognise action: " + action); + Logger.warning("> Syntax does not match at all. Here's an example to compare with:"); + Logger.warning("> title::My title::My subtitle"); + continue; + } + + String title = StringUtils.replace(split[1], "&", "§"), + subTitle = StringUtils.replace(split[2], "&", "§"); + int fadeIn = 300, stay = 3000, fadeOut = 300; + + if(!Reflection.isProxyServer()) + BukkitLoader.sendTitle(uuid, title, subTitle, fadeIn, stay, fadeOut); + else { + if(Reflection.isVelocityServer()) + VelocityLoader.sendTitle(uuid, title, subTitle, fadeIn, stay, fadeOut); + /*else + BungeeLoader.*/ + + } + continue; + + case "sound": + + if(Reflection.isProxyServer()) { + Logger.warning("Could not execute action: " + action); + Logger.warning("> Sounds cannot be played on the proxy side!"); + continue; + } + + if(split.length != 4) { + Logger.warning("Could not recognise action: " + action); + Logger.warning("> Syntax does not match at all. Here's an example to compare with:"); + Logger.warning("> sound::ENTITY_ENDER_DRAGON_GROWL::1.0::1.0"); + continue; + } + + float volume, pitch; + + try { + volume = Float.parseFloat(split[2]); + } catch (Exception exception) { + Logger.warning("Could not recognise action: " + action); + Logger.warning("> Volume " + split[2].toUpperCase() + " is not a valid float! (e.g: 1.0f)"); + continue; + } + + try { + pitch = Float.parseFloat(split[3]); + } catch (Exception exception) { + Logger.warning("Could not recognise action: " + action); + Logger.warning("> Pitch " + split[3].toUpperCase() + " is not a valid float (e.g: 1.0f)!"); + continue; + } + + BukkitLoader.playSound(uuid, split[1].toUpperCase(), volume, pitch); + continue; + + case "console": + if(split.length == 1) { + Logger.warning("Could not recognise action: " + action); + Logger.warning("> Syntax does not match at all. Here's an example to compare with:"); + Logger.warning("> console::say Hello world!"); + } + + if(!Reflection.isProxyServer()) + BukkitLoader.executeConsoleCommand(split[1]); + else { + if (Reflection.isVelocityServer()) + VelocityLoader.executeConsoleCommand(split[1]); + /*else + BungeeLoader.*/ + } + } + + } + } } }