lousy semicolon support, replace, escape, and ai in entities
This commit is contained in:
1 parent
51a95a241c
commit
22134187d8
5 files changed
+65
-23
No files matched your search
+1
-1
@@ -6,7 +6,7 @@ org.gradle.jvmargs=-Xmx1G
|
|||||||
# or better: https://modmuss50.me/fabric.html
|
# or better: https://modmuss50.me/fabric.html
|
||||||
minecraft_version=1.14.4
|
minecraft_version=1.14.4
|
||||||
yarn_mappings=1.14.4+build.8
|
yarn_mappings=1.14.4+build.8
|
||||||
loader_version=0.4.8+build.158
|
loader_version=0.6.3+build.167
|
||||||
|
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version = 1.2.0+v191024
|
mod_version = 1.2.0+v191024
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ import net.minecraft.item.BlockItem;
|
|||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.item.Items;
|
import net.minecraft.item.Items;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
|
import net.minecraft.nbt.StringTag;
|
||||||
import net.minecraft.nbt.Tag;
|
import net.minecraft.nbt.Tag;
|
||||||
import net.minecraft.particle.ParticleEffect;
|
import net.minecraft.particle.ParticleEffect;
|
||||||
import net.minecraft.predicate.entity.EntityPredicates;
|
import net.minecraft.predicate.entity.EntityPredicates;
|
||||||
@@ -2778,6 +2779,13 @@ public class CarpetExpression
|
|||||||
return (cc, tt) -> ret;
|
return (cc, tt) -> ret;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.expr.addLazyFunction("escape_nbt", 1, (c, t, lv) -> {
|
||||||
|
Value v = lv.get(0).evalValue(c);
|
||||||
|
String string = v.getString();
|
||||||
|
Value ret = new StringValue(StringTag.escape(string));
|
||||||
|
return (cc, tt) -> ret;
|
||||||
|
});
|
||||||
|
|
||||||
//"overridden" native call that prints to stderr
|
//"overridden" native call that prints to stderr
|
||||||
this.expr.addLazyFunction("print", 1, (c, t, lv) ->
|
this.expr.addLazyFunction("print", 1, (c, t, lv) ->
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ import java.util.function.Supplier;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
import static java.lang.Math.abs;
|
import static java.lang.Math.abs;
|
||||||
import static java.lang.Math.max;
|
import static java.lang.Math.max;
|
||||||
@@ -2852,8 +2853,29 @@ public class Expression implements Cloneable
|
|||||||
|
|
||||||
addUnaryFunction("title", v -> new StringValue(WordUtils.capitalizeFully(v.getString())));
|
addUnaryFunction("title", v -> new StringValue(WordUtils.capitalizeFully(v.getString())));
|
||||||
|
|
||||||
// TODO: split, replace, replace_first, escape_special (to make nbt to string)
|
addFunction("replace", (lv) ->
|
||||||
|
{
|
||||||
|
if (lv.size() != 3 && lv.size() !=2)
|
||||||
|
throw new InternalExpressionException("'replace' expects string to read, pattern regex, and optional replacement string");
|
||||||
|
String data = lv.get(0).getString();
|
||||||
|
String regex = lv.get(1).getString();
|
||||||
|
String replacement = "";
|
||||||
|
if (lv.size() == 3)
|
||||||
|
replacement = lv.get(2).getString();
|
||||||
|
return new StringValue(data.replaceAll(regex, replacement));
|
||||||
|
});
|
||||||
|
|
||||||
|
addFunction("replace_first", (lv) ->
|
||||||
|
{
|
||||||
|
if (lv.size() != 3 && lv.size() !=2)
|
||||||
|
throw new InternalExpressionException("'replace_first' expects string to read, pattern regex, and optional replacement string");
|
||||||
|
String data = lv.get(0).getString();
|
||||||
|
String regex = lv.get(1).getString();
|
||||||
|
String replacement = "";
|
||||||
|
if (lv.size() == 3)
|
||||||
|
replacement = lv.get(2).getString();
|
||||||
|
return new StringValue(data.replaceFirst(regex, replacement));
|
||||||
|
});
|
||||||
|
|
||||||
addUnaryFunction("type", v -> new StringValue(v.getTypeString()));
|
addUnaryFunction("type", v -> new StringValue(v.getTypeString()));
|
||||||
|
|
||||||
@@ -2977,10 +2999,7 @@ public class Expression implements Cloneable
|
|||||||
*/
|
*/
|
||||||
public Expression(String expression)
|
public Expression(String expression)
|
||||||
{
|
{
|
||||||
this.expression = expression.trim().
|
this.expression = expression.trim().replaceAll("\\r\\n?", "\n").replaceAll("\\t"," ");
|
||||||
replaceAll("\\r\\n?", "\n").
|
|
||||||
replaceAll("\\t"," ").
|
|
||||||
replaceAll(";+$", "");
|
|
||||||
VariablesAndConstants();
|
VariablesAndConstants();
|
||||||
UserDefinedFunctionsAndControlFlow();
|
UserDefinedFunctionsAndControlFlow();
|
||||||
Operators();
|
Operators();
|
||||||
@@ -2996,20 +3015,27 @@ public class Expression implements Cloneable
|
|||||||
Stack<Tokenizer.Token> stack = new Stack<>();
|
Stack<Tokenizer.Token> stack = new Stack<>();
|
||||||
|
|
||||||
Tokenizer tokenizer = new Tokenizer(this, expression, allowComments, allowNewlineSubstitutions);
|
Tokenizer tokenizer = new Tokenizer(this, expression, allowComments, allowNewlineSubstitutions);
|
||||||
|
// stripping lousy but acceptable semicolons
|
||||||
|
Iterable<Tokenizer.Token> iterable = () -> tokenizer;
|
||||||
|
List<Tokenizer.Token> originalTokens = StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList());
|
||||||
|
List<Tokenizer.Token> cleanedTokens = new ArrayList<>();
|
||||||
|
Tokenizer.Token last = null;
|
||||||
|
while (originalTokens.size() > 0)
|
||||||
|
{
|
||||||
|
Tokenizer.Token current = originalTokens.remove(originalTokens.size()-1);
|
||||||
|
if (current.type != Tokenizer.Token.TokenType.OPERATOR
|
||||||
|
|| !current.surface.equals(";")
|
||||||
|
|| (last != null && last.type != Tokenizer.Token.TokenType.CLOSE_PAREN && last.type != Tokenizer.Token.TokenType.COMMA && !last.surface.equals(";")))
|
||||||
|
cleanedTokens.add(current);
|
||||||
|
if (current.type != Tokenizer.Token.TokenType.MARKER)
|
||||||
|
last = current;
|
||||||
|
}
|
||||||
|
Collections.reverse(cleanedTokens);
|
||||||
|
|
||||||
Tokenizer.Token lastFunction = null;
|
Tokenizer.Token lastFunction = null;
|
||||||
Tokenizer.Token previousToken = null;
|
Tokenizer.Token previousToken = null;
|
||||||
while (tokenizer.hasNext())
|
for (Tokenizer.Token token : cleanedTokens)
|
||||||
{
|
{
|
||||||
Tokenizer.Token token;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
token = tokenizer.next();
|
|
||||||
}
|
|
||||||
catch (StringIndexOutOfBoundsException e)
|
|
||||||
{
|
|
||||||
throw new ExpressionException("Script ended prematurely");
|
|
||||||
}
|
|
||||||
switch (token.type)
|
switch (token.type)
|
||||||
{
|
{
|
||||||
case STRINGPARAM:
|
case STRINGPARAM:
|
||||||
|
|||||||
@@ -193,14 +193,14 @@ public class Tokenizer implements Iterator<Tokenizer.Token>
|
|||||||
token.append(ch);
|
token.append(ch);
|
||||||
pos++;
|
pos++;
|
||||||
linepos++;
|
linepos++;
|
||||||
if (expression != null && previousToken != null && previousToken.type == Token.TokenType.OPERATOR && (ch == ')' || ch == ',') )
|
|
||||||
{
|
if (expression != null && previousToken != null &&
|
||||||
if (previousToken.surface.equalsIgnoreCase(";"))
|
previousToken.type == Token.TokenType.OPERATOR &&
|
||||||
throw new ExpressionException(this.expression, previousToken,
|
(ch == ')' || ch == ',') &&
|
||||||
"Cannot have semicolon at the end of the expression");
|
!previousToken.surface.equalsIgnoreCase(";")
|
||||||
|
)
|
||||||
throw new ExpressionException(this.expression, previousToken,
|
throw new ExpressionException(this.expression, previousToken,
|
||||||
"Can't have operator "+previousToken.surface+" at the end of a subexpression");
|
"Can't have operator " + previousToken.surface + " at the end of a subexpression");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -722,6 +722,14 @@ public class EntityValue extends Value
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
put("ai", (e, v) ->
|
||||||
|
{
|
||||||
|
if (e instanceof MobEntity)
|
||||||
|
{
|
||||||
|
((MobEntity) e).setAiDisabled(!v.getBoolean());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// gamemode
|
// gamemode
|
||||||
// spectate
|
// spectate
|
||||||
// "fire"
|
// "fire"
|
||||||
|
|||||||
Reference in new issue
Block a user