Added variable %args[from-to]%

This commit is contained in:
rayzsyt committed 2025-07-30 05:18:25 +02:00
1 parent 4ab0791740
commit bccb77667d
7 files changed
+160 -67

No files matched your search

@@ -23,12 +23,65 @@ public class ResponseHandler {
});
}
public static String replaceArgsVariables(String input, String command) {
if (!input.contains(" "))
return input;
String[] commandSplit = command.contains(" ") ? command.split(" ") : new String[]{};
String[] split = input.split(" ");
for (int i = 0; i < split.length; i++) {
String part = split[i];
if (! (part.startsWith("%args[") && part.endsWith("]%")))
continue;
part = part.substring(part.indexOf('[') + 1, part.lastIndexOf(']'));
int length = commandSplit.length, fromIndex, toIndex;
if (part.contains("-")) {
String[] indexSplit = part.split("-");
fromIndex = Integer.parseInt(indexSplit[0]) - 1;
toIndex = Integer.parseInt(indexSplit[1]);
} else {
fromIndex = Integer.parseInt(part) - 1;
toIndex = fromIndex;
}
if (fromIndex < 0 || toIndex > length) {
Logger.warning("Out of bounds! (" + input + ")");
continue;
}
String replacement;
if (fromIndex != toIndex) {
replacement = String.join(" ", Arrays.copyOfRange(commandSplit, fromIndex, toIndex));;
} else {
if (fromIndex >= length) {
Logger.warning("Out of bounds! (" + input + ")");
continue;
}
replacement = commandSplit[fromIndex];
}
split[i] = replacement;
}
return String.join(" ", split);
}
public static List<String> getResponse(UUID uuid, String input) {
return getResponse(uuid, input, Storage.ConfigSections.Settings.CANCEL_COMMAND.SUB_COMMAND_RESPONSE.getLines());
}
public static List<String> getResponse(UUID uuid, String input, List<String> defaultMessage) {
final String finalInput = input.startsWith("/") ? StringUtils.replaceFirst(input, "/", "") : input;
final String finalInput = input.startsWith("/") && input.length() > 1
? input.substring(1)
: input;
final Optional<Response> optionalResponse = RESPONSES.stream().filter(response -> {
for (String trigger : response.triggers)
@@ -39,7 +92,7 @@ public class ResponseHandler {
}).findFirst();
if (uuid != null && optionalResponse.isPresent()) {
optionalResponse.get().executeAction(uuid);
optionalResponse.get().executeAction(uuid, finalInput);
return optionalResponse.get().message;
}
@@ -56,7 +109,7 @@ public class ResponseHandler {
this.actions = (List<String>) Storage.Files.CUSTOM_RESPONSES.getOrSet(key + ".actions", new ArrayList<>());
}
public void executeAction(UUID uuid) {
public void executeAction(UUID uuid, String command) {
String[] split;
for (String action : actions) {
@@ -142,7 +195,7 @@ public class ResponseHandler {
continue;
}
ActionHandler.sendTitle(action, uuid, split[1], split[2], fadeIn, stay, fadeOut);
ActionHandler.sendTitle(action, uuid, command, split[1], split[2], fadeIn, stay, fadeOut);
continue;
case "sound":
@@ -192,7 +245,7 @@ public class ResponseHandler {
Logger.warning(" > e.g: console::say Hello world!");
}
ActionHandler.executeConsoleCommand(action, uuid, split[1]);
ActionHandler.executeConsoleCommand(action, uuid, command, split[1]);
case "execute":
if (split.length == 1) {
@@ -203,7 +256,7 @@ public class ResponseHandler {
Logger.warning(" > e.g: execute::help");
}
ActionHandler.executePlayerCommand(action, uuid, split[1]);
ActionHandler.executePlayerCommand(action, uuid, command, split[1]);
case "actionbar":
if (split.length == 1) {
@@ -214,7 +267,7 @@ public class ResponseHandler {
Logger.warning(" > e.g: actionbar::I like COOKIES");
}
ActionHandler.sendActionbar(action, uuid, split[1]);
ActionHandler.sendActionbar(action, uuid, command, split[1]);
}
}
@@ -4,11 +4,11 @@ import java.util.UUID;
public interface Action {
void executeConsoleCommand(String action, UUID uuid, String command);
void executePlayerCommand(String action, UUID uuid, String command);
void sendTitle(String action, UUID uuid, String title, String subTitle, int fadeIn, int stay, int fadeOut);
void executeConsoleCommand(String action, UUID uuid, String command, String message);
void executePlayerCommand(String action, UUID uuid, String command, String message);
void sendTitle(String action, UUID uuid, String command, String title, String subTitle, int fadeIn, int stay, int fadeOut);
void addPotionEffect(String action, UUID uuid, String potionEffectTypeName, int duration, int amplifier);
void playSound(String action, UUID uuid, String soundName, float volume, float pitch);
void sendActionbar(String action, UUID uuid, String text);
void sendActionbar(String action, UUID uuid, String command, String message);
}
@@ -6,20 +6,23 @@ import java.util.UUID;
public class ActionHandler {
private static final Action ACTION = Reflection.isProxyServer() ? Reflection.isVelocityServer() ? new VelocityAction() : new BungeeAction() : Reflection.getMinor() <= 16 ? new OldBukkitAction() : new ModernBukkitAction();
private static final Action ACTION = Reflection.isProxyServer() ? Reflection.isVelocityServer()
? new VelocityAction() : new BungeeAction()
: Reflection.getMinor() <= 16
? new OldBukkitAction() : new ModernBukkitAction();
public static void initialize() {}
public static void executeConsoleCommand(String action, UUID uuid, String command) {
ACTION.executeConsoleCommand(action, uuid, command);
public static void executeConsoleCommand(String action, UUID uuid, String command, String message) {
ACTION.executeConsoleCommand(action, uuid, command, message);
}
public static void executePlayerCommand(String action, UUID uuid, String command) {
ACTION.executePlayerCommand(action, uuid, command);
public static void executePlayerCommand(String action, UUID uuid, String command, String message) {
ACTION.executePlayerCommand(action, uuid, command, message);
}
public static void sendTitle(String action, UUID uuid, String title, String subTitle, int fadeIn, int stay, int fadeOut) {
ACTION.sendTitle(action, uuid, title, subTitle, fadeIn, stay, fadeOut);
public static void sendTitle(String action, UUID uuid, String command, String title, String subTitle, int fadeIn, int stay, int fadeOut) {
ACTION.sendTitle(action, uuid, command, title, subTitle, fadeIn, stay, fadeOut);
}
public static void addPotionEffect(String action, UUID uuid, String potionEffectType, int duration, int amplifier) {
@@ -30,7 +33,7 @@ public class ActionHandler {
ACTION.playSound(action, uuid, soundName, volume, pitch);
}
public static void sendActionbar(String action, UUID uuid, String text) {
ACTION.sendActionbar(action, uuid, text);
public static void sendActionbar(String action, UUID uuid, String command, String message) {
ACTION.sendActionbar(action, uuid, command, message);
}
}
@@ -2,6 +2,7 @@ package de.rayzs.pat.utils.response.action.impl;
import de.rayzs.pat.plugin.logger.Logger;
import de.rayzs.pat.utils.StringUtils;
import de.rayzs.pat.utils.response.ResponseHandler;
import de.rayzs.pat.utils.response.action.Action;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.ProxyServer;
@@ -14,28 +15,32 @@ import java.util.UUID;
public class BungeeAction implements Action {
@Override
public void executeConsoleCommand(String action, UUID uuid, String command) {
public void executeConsoleCommand(String action, UUID uuid, String command, String message) {
ProxiedPlayer player = ProxyServer.getInstance().getPlayer(uuid);
message = ResponseHandler.replaceArgsVariables(message, command);
if (player != null)
command = command.replace("%player%", player.getName());
message = message.replace("%player%", player.getName());
ProxyServer.getInstance().getPluginManager().dispatchCommand(ProxyServer.getInstance().getConsole(), command);
ProxyServer.getInstance().getPluginManager().dispatchCommand(ProxyServer.getInstance().getConsole(), message);
}
@Override
public void executePlayerCommand(String action, UUID uuid, String command) {
public void executePlayerCommand(String action, UUID uuid, String command, String message) {
ProxiedPlayer player = ProxyServer.getInstance().getPlayer(uuid);
message = ResponseHandler.replaceArgsVariables(message, command);
if (player != null)
command = command.replace("%player%", player.getName());
message = command.replace("%player%", player.getName());
ProxyServer.getInstance().getPluginManager().dispatchCommand(player, command);
ProxyServer.getInstance().getPluginManager().dispatchCommand(player, message);
}
@Override
public void sendTitle(String action, UUID uuid, String title, String subTitle, int fadeIn, int stay, int fadeOut) {
public void sendTitle(String action, UUID uuid, String command, String title, String subTitle, int fadeIn, int stay, int fadeOut) {
ProxiedPlayer player = ProxyServer.getInstance().getPlayer(uuid);
title = ResponseHandler.replaceArgsVariables(title, command);
subTitle = ResponseHandler.replaceArgsVariables(subTitle, command);
if (player == null)
return;
@@ -58,12 +63,13 @@ public class BungeeAction implements Action {
}
@Override
public void sendActionbar(String action, UUID uuid, String text) {
public void sendActionbar(String action, UUID uuid, String command, String message) {
ProxiedPlayer player = ProxyServer.getInstance().getPlayer(uuid);
message = ResponseHandler.replaceArgsVariables(message, command);
if (player == null)
return;
player.sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(StringUtils.replace(text, "%player%", player.getName())));
player.sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(StringUtils.replace(message, "%player%", player.getName())));
}
}
@@ -3,6 +3,7 @@ package de.rayzs.pat.utils.response.action.impl;
import de.rayzs.pat.plugin.logger.Logger;
import de.rayzs.pat.utils.StringUtils;
import de.rayzs.pat.utils.message.replacer.PlaceholderReplacer;
import de.rayzs.pat.utils.response.ResponseHandler;
import de.rayzs.pat.utils.response.action.Action;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
@@ -17,35 +18,39 @@ import java.util.UUID;
public class ModernBukkitAction implements Action {
@Override
public void executeConsoleCommand(String action, UUID uuid, String command) {
public void executeConsoleCommand(String action, UUID uuid, String command, String message) {
Player player = Bukkit.getPlayer(uuid);
message = ResponseHandler.replaceArgsVariables(message, command);
if (player != null)
command = command.replace("%player%", player.getName());
message = message.replace("%player%", player.getName());
if (command.contains("%"))
command = PlaceholderReplacer.replace(player, command);
if (message.contains("%"))
message = PlaceholderReplacer.replace(player, message);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), message);
}
@Override
public void executePlayerCommand(String action, UUID uuid, String command) {
public void executePlayerCommand(String action, UUID uuid, String command, String message) {
Player player = Bukkit.getPlayer(uuid);
message = ResponseHandler.replaceArgsVariables(message, command);
if(player != null)
command = command.replace("%player%", player.getName());
message = message.replace("%player%", player.getName());
if (command.contains("%"))
command = PlaceholderReplacer.replace(player, command);
if (message.contains("%"))
message = PlaceholderReplacer.replace(player, message);
if (player != null)
Bukkit.dispatchCommand(player, command);
Bukkit.dispatchCommand(player, message);
}
@Override
public void sendTitle(String action, UUID uuid, String title, String subTitle, int fadeIn, int stay, int fadeOut) {
public void sendTitle(String action, UUID uuid, String command, String title, String subTitle, int fadeIn, int stay, int fadeOut) {
Player player = Bukkit.getPlayer(uuid);
title = ResponseHandler.replaceArgsVariables(title, command);
subTitle = ResponseHandler.replaceArgsVariables(subTitle, command);
if (player == null)
return;
@@ -98,14 +103,15 @@ public class ModernBukkitAction implements Action {
}
@Override
public void sendActionbar(String action, UUID uuid, String text) {
public void sendActionbar(String action, UUID uuid, String command, String message) {
Player player = Bukkit.getPlayer(uuid);
message = ResponseHandler.replaceArgsVariables(message, command);
if (player == null)
return;
text = PlaceholderReplacer.replace(player, StringUtils.replace(text, "&", "§", "%player%", player.getName()));
message = PlaceholderReplacer.replace(player, StringUtils.replace(message, "&", "§", "%player%", player.getName()));
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(text));
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message));
}
}
@@ -4,6 +4,7 @@ import de.rayzs.pat.plugin.logger.Logger;
import de.rayzs.pat.utils.Reflection;
import de.rayzs.pat.utils.StringUtils;
import de.rayzs.pat.utils.message.replacer.PlaceholderReplacer;
import de.rayzs.pat.utils.response.ResponseHandler;
import de.rayzs.pat.utils.response.action.Action;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
@@ -21,33 +22,40 @@ public class OldBukkitAction implements Action {
private String versionPackage = null;
@Override
public void executeConsoleCommand(String action, UUID uuid, String command) {
public void executeConsoleCommand(String action, UUID uuid, String command, String message) {
Player player = Bukkit.getPlayer(uuid);
if(player != null) command = command.replace("%player%", player.getName());
message = ResponseHandler.replaceArgsVariables(message, command);
if (command.contains("%"))
command = PlaceholderReplacer.replace(player, command);
if(player != null)
message = message.replace("%player%", player.getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
if (message.contains("%"))
message = PlaceholderReplacer.replace(player, message);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), message);
}
@Override
public void executePlayerCommand(String action, UUID uuid, String command) {
public void executePlayerCommand(String action, UUID uuid, String command, String message) {
Player player = Bukkit.getPlayer(uuid);
message = ResponseHandler.replaceArgsVariables(message, command);
if (player != null)
command = command.replace("%player%", player.getName());
message = message.replace("%player%", player.getName());
if (command.contains("%"))
command = PlaceholderReplacer.replace(player, command);
if (message.contains("%"))
message = PlaceholderReplacer.replace(player, message);
if (player != null)
Bukkit.dispatchCommand(player, command);
Bukkit.dispatchCommand(player, message);
}
@Override
public void sendTitle(String action, UUID uuid, String title, String subTitle, int fadeIn, int stay, int fadeOut) {
public void sendTitle(String action, UUID uuid, String command, String title, String subTitle, int fadeIn, int stay, int fadeOut) {
Player player = Bukkit.getPlayer(uuid);
title = ResponseHandler.replaceArgsVariables(title, command);
subTitle = ResponseHandler.replaceArgsVariables(subTitle, command);
if (player == null) return;
title = PlaceholderReplacer.replace(player, StringUtils.replace(title, "&", "§", "%player%", player.getName()));
@@ -98,13 +106,14 @@ public class OldBukkitAction implements Action {
}
@Override
public void sendActionbar(String action, UUID uuid, String text) {
public void sendActionbar(String action, UUID uuid, String command, String message) {
Player player = Bukkit.getPlayer(uuid);
message = ResponseHandler.replaceArgsVariables(message, command);
if (player == null)
return;
text = PlaceholderReplacer.replace(player, StringUtils.replace(text, "&", "§", "%player%", player.getName()));
message = PlaceholderReplacer.replace(player, StringUtils.replace(message, "&", "§", "%player%", player.getName()));
try {
if (versionPackage == null)
@@ -112,7 +121,7 @@ public class OldBukkitAction implements Action {
Object entityPlayer = player.getClass().getMethod("getHandle").invoke(player);
Constructor<?> constructor = (Objects.<Class<?>>requireNonNull(Class.forName("net.minecraft.server." + versionPackage + ".PacketPlayOutChat"))).getConstructor(Class.forName("net.minecraft.server." + versionPackage + ".IChatBaseComponent"), byte.class);
Object iChatBaseComponent = (Objects.requireNonNull(Class.forName("net.minecraft.server." + versionPackage + ".IChatBaseComponent"))).getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + text + "\"}");
Object iChatBaseComponent = (Objects.requireNonNull(Class.forName("net.minecraft.server." + versionPackage + ".IChatBaseComponent"))).getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + message + "\"}");
Object actionbarPacket = constructor.newInstance(iChatBaseComponent, (byte) 2);
Object playerConnection = entityPlayer.getClass().getField("playerConnection").get(entityPlayer);
playerConnection.getClass().getMethod("sendPacket", Class.forName("net.minecraft.server." + versionPackage + ".Packet")).invoke(playerConnection, actionbarPacket);
@@ -123,6 +132,7 @@ public class OldBukkitAction implements Action {
| InvocationTargetException
| NoSuchFieldException
| InstantiationException exception) {
Logger.warning("! Failed to run action: " + action);
Logger.warning(" > Actionbars are not supported in this server version!");
}
@@ -2,6 +2,7 @@ package de.rayzs.pat.utils.response.action.impl;
import de.rayzs.pat.plugin.logger.Logger;
import de.rayzs.pat.utils.message.MessageTranslator;
import de.rayzs.pat.utils.response.ResponseHandler;
import net.kyori.adventure.text.minimessage.MiniMessage;
import de.rayzs.pat.utils.response.action.Action;
import com.velocitypowered.api.proxy.Player;
@@ -14,22 +15,33 @@ import java.time.*;
public class VelocityAction implements Action {
@Override
public void executeConsoleCommand(String action, UUID uuid, String command) {
public void executeConsoleCommand(String action, UUID uuid, String command, String message) {
Optional<Player> optPlayer = VelocityLoader.getServer().getPlayer(uuid);
if (optPlayer.isPresent()) command = command.replace("%player%", optPlayer.get().getUsername());
VelocityLoader.getServer().getCommandManager().executeAsync(VelocityLoader.getServer().getConsoleCommandSource(), command);
message = ResponseHandler.replaceArgsVariables(message, command);
if (optPlayer.isPresent())
message = message.replace("%player%", optPlayer.get().getUsername());
VelocityLoader.getServer().getCommandManager().executeAsync(VelocityLoader.getServer().getConsoleCommandSource(), message);
}
@Override
public void executePlayerCommand(String action, UUID uuid, String command) {
public void executePlayerCommand(String action, UUID uuid, String command, String message) {
Optional<Player> optPlayer = VelocityLoader.getServer().getPlayer(uuid);
if (optPlayer.isPresent()) command = command.replace("%player%", optPlayer.get().getUsername());
VelocityLoader.getServer().getCommandManager().executeAsync(optPlayer.get(), command);
message = ResponseHandler.replaceArgsVariables(message, command);
if (optPlayer.isPresent())
message = command.replace("%player%", optPlayer.get().getUsername());
VelocityLoader.getServer().getCommandManager().executeAsync(optPlayer.get(), message);
}
@Override
public void sendTitle(String action, UUID uuid, String title, String subTitle, int fadeIn, int stay, int fadeOut) {
public void sendTitle(String action, UUID uuid, String command, String title, String subTitle, int fadeIn, int stay, int fadeOut) {
Optional<Player> optPlayer = VelocityLoader.getServer().getPlayer(uuid);
title = ResponseHandler.replaceArgsVariables(title, command);
subTitle = ResponseHandler.replaceArgsVariables(subTitle, command);
if (!optPlayer.isPresent()) return;
Player player = optPlayer.get();
@@ -51,11 +63,14 @@ public class VelocityAction implements Action {
}
@Override
public void sendActionbar(String action, UUID uuid, String text) {
public void sendActionbar(String action, UUID uuid, String command, String message) {
Optional<Player> optPlayer = VelocityLoader.getServer().getPlayer(uuid);
if (!optPlayer.isPresent()) return;
message = ResponseHandler.replaceArgsVariables(message, command);
if (!optPlayer.isPresent())
return;
Player player = optPlayer.get();
player.sendActionBar(MiniMessage.miniMessage().deserialize(MessageTranslator.replaceMessage(StringUtils.replace(text, "%player%", player.getUsername()))));
player.sendActionBar(MiniMessage.miniMessage().deserialize(MessageTranslator.replaceMessage(StringUtils.replace(message, "%player%", player.getUsername()))));
}
}