add user and uuid match (superCB) in CommandBlockCommand
This commit is contained in:
parent
f095a92008
commit
e84bfa0e42
1 changed files with 83 additions and 10 deletions
|
@ -5,16 +5,29 @@ import me.chayapak1.chomens_bot.command.Command;
|
||||||
import me.chayapak1.chomens_bot.command.CommandContext;
|
import me.chayapak1.chomens_bot.command.CommandContext;
|
||||||
import me.chayapak1.chomens_bot.command.CommandException;
|
import me.chayapak1.chomens_bot.command.CommandException;
|
||||||
import me.chayapak1.chomens_bot.command.TrustLevel;
|
import me.chayapak1.chomens_bot.command.TrustLevel;
|
||||||
|
import me.chayapak1.chomens_bot.data.PlayerEntry;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
// based off of HBot's supercb command
|
||||||
public class CommandBlockCommand extends Command {
|
public class CommandBlockCommand extends Command {
|
||||||
|
private static final Pattern USER_PATTERN = Pattern.compile("\\{username\\{(.*?)}}");
|
||||||
|
private static final Pattern UUID_PATTERN = Pattern.compile("\\{uuid\\{(.*?)}}");
|
||||||
|
|
||||||
public CommandBlockCommand () {
|
public CommandBlockCommand () {
|
||||||
super(
|
super(
|
||||||
"cb",
|
"cb",
|
||||||
"Executes a command in the command core and return its output",
|
"Executes a command in the command core and return its output",
|
||||||
new String[] { "<command>" },
|
new String[] {
|
||||||
|
"<command>",
|
||||||
|
"..{username}..",
|
||||||
|
"..{uuid}..",
|
||||||
|
"..{username{regex}}..",
|
||||||
|
"..{uuid{regex}}.."
|
||||||
|
},
|
||||||
new String[] { "cmd", "commandblock", "run", "core" },
|
new String[] { "cmd", "commandblock", "run", "core" },
|
||||||
TrustLevel.PUBLIC,
|
TrustLevel.PUBLIC,
|
||||||
false
|
false
|
||||||
|
@ -25,16 +38,76 @@ public class CommandBlockCommand extends Command {
|
||||||
public Component execute(CommandContext context) throws CommandException {
|
public Component execute(CommandContext context) throws CommandException {
|
||||||
final Bot bot = context.bot;
|
final Bot bot = context.bot;
|
||||||
|
|
||||||
final CompletableFuture<Component> future = bot.core.runTracked(context.getString(true, true));
|
runCommand(bot, context, context.getString(true, true));
|
||||||
|
|
||||||
if (future == null) return null;
|
|
||||||
|
|
||||||
future.thenApply(output -> {
|
|
||||||
context.sendOutput(output);
|
|
||||||
|
|
||||||
return output;
|
|
||||||
});
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void runCommand (Bot bot, CommandContext context, String command) {
|
||||||
|
final Matcher userMatcher = USER_PATTERN.matcher(command);
|
||||||
|
final Matcher uuidMatcher = UUID_PATTERN.matcher(command);
|
||||||
|
|
||||||
|
final boolean userFound = userMatcher.find();
|
||||||
|
final boolean uuidFound = uuidMatcher.find();
|
||||||
|
|
||||||
|
if (userFound || uuidFound) {
|
||||||
|
Pattern pattern;
|
||||||
|
|
||||||
|
if (userFound) pattern = Pattern.compile(userMatcher.group(1));
|
||||||
|
else pattern = Pattern.compile(uuidMatcher.group(1));
|
||||||
|
|
||||||
|
for (PlayerEntry entry : bot.players.list) {
|
||||||
|
final String username = entry.profile.getName();
|
||||||
|
final String uuid = entry.profile.getIdAsString();
|
||||||
|
|
||||||
|
if (!pattern.matcher(userFound ? username : uuid).matches()) continue;
|
||||||
|
|
||||||
|
String replacedCommand;
|
||||||
|
|
||||||
|
if (userFound) replacedCommand = new StringBuilder(command).replace(userMatcher.start(), userMatcher.end(), username).toString();
|
||||||
|
else replacedCommand = new StringBuilder(command).replace(uuidMatcher.start(), uuidMatcher.end(), uuid).toString();
|
||||||
|
|
||||||
|
replacedCommand = replacedCommand
|
||||||
|
.replace("{username}", username)
|
||||||
|
.replace("{uuid}", uuid);
|
||||||
|
|
||||||
|
if (
|
||||||
|
!replacedCommand.contains("{username}") &&
|
||||||
|
!replacedCommand.contains("{uuid}") &&
|
||||||
|
!USER_PATTERN.matcher(username).find() &&
|
||||||
|
!UUID_PATTERN.matcher(username).find()
|
||||||
|
) {
|
||||||
|
runCommand(bot, context, replacedCommand);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (command.contains("{username}") || command.contains("{uuid}")) {
|
||||||
|
for (PlayerEntry entry : bot.players.list) {
|
||||||
|
final String username = entry.profile.getName();
|
||||||
|
final String uuid = entry.profile.getIdAsString();
|
||||||
|
|
||||||
|
final String replacedCommand = command
|
||||||
|
.replace("{username}", username)
|
||||||
|
.replace("{uuid}", uuid);
|
||||||
|
|
||||||
|
if (
|
||||||
|
!replacedCommand.contains("{username}") &&
|
||||||
|
!replacedCommand.contains("{Uuuid}") &&
|
||||||
|
!USER_PATTERN.matcher(username).find() &&
|
||||||
|
!UUID_PATTERN.matcher(username).find()
|
||||||
|
) {
|
||||||
|
runCommand(bot, context, replacedCommand);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
final CompletableFuture<Component> future = bot.core.runTracked(command);
|
||||||
|
|
||||||
|
if (future == null) return;
|
||||||
|
|
||||||
|
future.thenApply(output -> {
|
||||||
|
context.sendOutput(output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue