Skip to content

Commit

Permalink
Add more placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
NotRyken committed Oct 6, 2024
1 parent f419ea0 commit 668dec5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,24 @@ A powerful command macro mod.

- **Placeholders**
- Simple Placeholders
- `%lastsent%`: The most recently-sent message or command in history, if any.
- `%lastcmd%`: The most recently-sent command in history, if any.
- `%clipboard%`: The contents of the clipboard, if any.
- `%myname%`: Minecraft username.
- `%pmsender%`: The name of the sender of the most recently-received private message, if any. (Searches max of 50).
- `%pos%`: The integer coordinates of the player (`x y z`).
- `%x%`: The integer X coordinate of the player. Also available for Y and Z.

- Regex Placeholders
- `%clipboard#(.*)%`: The contents of the clipboard, if any, verified to match a regex pattern following the
`#`. This can be used to prevent unintentional exposure of clipboard contents. E.g. use
`%clipboard#^-?\d+ -?\d+ -?\d+$%` to only send the clipboard contents if it's formatted like a set of coordinates.
- `%pos([FBLR])(\d+)%`: `%pos%` but offset some number of blocks in a direction relative to the cardinal direction
that the player is nearest to facing. E.g. use `%posL20%` to get the coordinates 20 blocks to the player's left.
- `%x([+-]\d+)%`: `%x%` but offset some number of blocks. E.g. use `%x-10%` to get the X coordinate 10 blocks to
the player's West. Also available for Y and Z.
- `%#(.*)%`: Regex Group 1 from the most recently-received message matching the regex following the `#`,
if any. Note: regex pattern must have at least 1 capturing group.
- `%clipboard#(.*)%`: The contents of the clipboard, if any, verified to match a regex pattern following the
`#`. This can be used to prevent unintentional exposure of clipboard contents. E.g. use
`%clipboard#^-?\d+ -?\d+ -?\d+$%` to only send the clipboard contents if it's formatted like a set of coordinates.
- `%pos([FBLR])(\d+)%`: `%pos%` but offset some number of blocks in a direction relative to the cardinal direction
that the player is nearest to facing. E.g. use `%posL20%` to get the coordinates 20 blocks to the player's left.
- `%x([+-]\d+)%`: `%x%` but offset some number of blocks. E.g. use `%x-10%` to get the X coordinate 10 blocks to
the player's West. Also available for Y and Z.

### Dependencies

Expand Down
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Added %placeholders%, check description for more info.
- Added %lastsent%, %lastcmd% and %#(.*)%
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.util.ArrayListDeque;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;
Expand All @@ -28,6 +30,8 @@ public class PlaceholderUtil {
private static @Nullable String pmSenderName;

private static final SimplePlaceholder[] SIMPLE_PLACEHOLDERS = {
new SimplePlaceholder("%lastsent%", PlaceholderUtil::getLastMessage),
new SimplePlaceholder("%lastcmd%", PlaceholderUtil::getLastCommand),
new SimplePlaceholder("%clipboard%", () -> getClipboard(null)),
new SimplePlaceholder("%myname%", PlaceholderUtil::getPlayerName),
new SimplePlaceholder("%pmsender%", PlaceholderUtil::getPmSenderName),
Expand All @@ -38,6 +42,7 @@ public class PlaceholderUtil {
};

private static final Placeholder[] REGEX_PLACEHOLDERS = {
new Placeholder(Pattern.compile("%#(.*)%"), 1, PlaceholderUtil::getRecentChat),
new Placeholder(Pattern.compile("%clipboard#(.*)%"), 1, PlaceholderUtil::getClipboard),
new Placeholder(Pattern.compile("%pos([FBLR])(\\d+)%"), 2, PlaceholderUtil::getPlayerBlockPos),
new Placeholder(Pattern.compile("%x([+-]\\d+)%"), 1, PlaceholderUtil::getPlayerBlockX),
Expand All @@ -62,6 +67,7 @@ private static void clearCache() {
playerBlockPos = null;
playerLookingAngle = null;
pmSenderName = null;

}

private record SimplePlaceholder(String string, Supplier<String> supplier) {
Expand All @@ -84,6 +90,36 @@ public String process(String message) {
}
}

// Incoming message

private static String getRecentChat(@NotNull String[] pattern) {
try {
Pattern regex = Pattern.compile(pattern[0]);

int i = 0;
for (GuiMessage guiMsg : ((ChatComponentAccessor)
Minecraft.getInstance().gui.getChat()).getAllMessages()) {
if (++i > 50) break;

Matcher matcher = regex.matcher(guiMsg.content().getString());
if (matcher.find()) {
try {
return matcher.group(1);
} catch (IndexOutOfBoundsException e) {
CommandKeys.LOG.error("Recent chat placeholder failed: Group 1 not available: " + e);
return "?";
}
}
}

CommandKeys.LOG.warn("Recent chat placeholder failed: No message found: Checked " + i);
} catch (PatternSyntaxException e) {
CommandKeys.LOG.error("Recent chat placeholder failed: Invalid regex: " + e);
}

return "?";
}

// Clipboard

private static String getClipboard(@Nullable String[] pattern) {
Expand All @@ -106,6 +142,24 @@ private static String getClipboard(@Nullable String[] pattern) {
return clipboard;
}

// Message history

private static String getLastMessage() {
String lastMsg = Minecraft.getInstance().gui.getChat().getRecentChat().peekLast();
if (lastMsg == null) return "?";
return lastMsg;
}

private static String getLastCommand() {
if (Minecraft.getInstance().commandHistory().history() instanceof ArrayListDeque<String> deque) {
String lastCmd = deque.peekLast();
if (lastCmd != null) return lastCmd;
} else {
CommandKeys.LOG.error("Command history not ArrayListDeque");
}
return "?";
}

// Player name

private static String getPlayerName() {
Expand Down
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Neo/Forge version ranges: https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html

# Project
mod_version=2.2.0-beta.01
mod_version=2.2.0-beta.02
mod_group=dev.terminalmc
mod_id=commandkeys
mod_name=CommandKeys
Expand Down Expand Up @@ -91,4 +91,3 @@ grgitservice_version=5.2.2
# Gradle
org.gradle.jvmargs=-Xmx4G
org.gradle.daemon=false

0 comments on commit 668dec5

Please sign in to comment.