Skip to content

Skip - prefix variable names when saving. (Ephemeral variables) #7495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions src/main/java/ch/njol/skript/lang/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class Variable<T> implements Expression<T>, KeyReceiverExpression<T>, Key
private final static String SINGLE_SEPARATOR_CHAR = ":";
public final static String SEPARATOR = SINGLE_SEPARATOR_CHAR + SINGLE_SEPARATOR_CHAR;
public final static String LOCAL_VARIABLE_TOKEN = "_";
private static final char[] reservedTokens = {'~', '.', '+', '$', '!', '&', '^', '*', '-'};
private static final char[] reservedTokens = {'~', '.', '+', '$', '!', '&', '^', '*'};

/**
* Script this variable was created in.
Expand Down Expand Up @@ -113,21 +113,10 @@ private Variable(VariableString name, Class<? extends T>[] types, boolean local,
public static boolean isValidVariableName(String name, boolean allowListVariable, boolean printErrors) {
assert !name.isEmpty(): "Variable name should not be empty";
char first = name.charAt(0);
check_reserved_tokens:
for (char token : reservedTokens) {
if (first == token && printErrors) {
/*
A lot of people already use '-' so we want to skip this warning iff they're using it here
*/
if (first == '-') {
for (VariablesStorage store : Variables.getStores()) {
@Nullable Pattern pattern = store.getNamePattern();
if (pattern != null && pattern.pattern().equals("(?!-).*"))
continue check_reserved_tokens;
}
}
Skript.warning("The character '" + token + "' is reserved at the start of variable names, " +
"and may be restricted in future versions");
Skript.warning("The character '" + token + "' is reserved at the start of variable names, "
+ "and may be restricted in future versions");
}
}
name = name.startsWith(LOCAL_VARIABLE_TOKEN) ? name.substring(LOCAL_VARIABLE_TOKEN.length()).trim() : name.trim();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ch/njol/skript/variables/Variables.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class Variables {
*/
private static final String CONFIGURATION_SERIALIZABLE_PREFIX = "ConfigurationSerializable_";

private static final String EPHEMERAL_VARIABLE_PREFIX = "-";

private final static Multimap<Class<? extends VariablesStorage>, String> TYPES = HashMultimap.create();

// Register some things with Yggdrasil
Expand Down Expand Up @@ -887,6 +889,8 @@ public static SerializedVariable serialize(String name, @Nullable Object value)
* @param value the value of the variable.
*/
private static void saveVariableChange(String name, @Nullable Object value) {
if (name.startsWith(Variables.EPHEMERAL_VARIABLE_PREFIX))
return;
saveQueue.add(serialize(name, value));
}

Expand Down