Skip to content

Commit

Permalink
feat(tsl): add placeholder evaluation on FOR action
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoodie committed Oct 24, 2019
1 parent 7d4a2b3 commit afaa14a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.daemon=false

mod_id=twitchspawn
mod_group=net.programmer.igoodie
mod_version=1.4.6
mod_version=1.4.7

minecraft_version=1.14.4
forge_version=28.0.83
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import net.programmer.igoodie.twitchspawn.tslanguage.keyword.TSLActionKeyword;
import net.programmer.igoodie.twitchspawn.tslanguage.parser.TSLParser;
import net.programmer.igoodie.twitchspawn.tslanguage.parser.TSLSyntaxError;
import net.programmer.igoodie.twitchspawn.util.ExpressionEvaluator;

import java.util.List;

public class ForAction extends TSLAction {

private TSLAction action;
private int iterationCount;
private String iterationCount;

/*
* FOR 5 TIMES
Expand All @@ -33,10 +34,14 @@ public ForAction(List<String> words) throws TSLSyntaxError {
throw new TSLSyntaxError("Expected TIMES, but found -> %s", actionWords.get(1));

try {
this.iterationCount = Integer.parseInt(actionWords.get(0));
this.iterationCount = actionWords.get(0);
this.action = TSLParser.parseAction(words.get(2), words.subList(3, words.size()));
this.action.silent = true;

// Check if given iteration count is parse-able
EventArguments randomEvent = EventArguments.createRandom("RandomStreamer");
evaluateIterationCount(randomEvent);

} catch (NumberFormatException e) {
throw new TSLSyntaxError("Malformed number word -> %s", actionWords.get(0));
}
Expand All @@ -46,6 +51,8 @@ public ForAction(List<String> words) throws TSLSyntaxError {
protected void performAction(ServerPlayerEntity player, EventArguments args) {
action.reflectedUser = this.reflectedUser;

int iterationCount = evaluateIterationCount(args);

for (int i = 0; i < iterationCount; i++) {
action.performAction(player, args);
}
Expand All @@ -59,8 +66,16 @@ protected String associatedSubtitleAction() {
@Override
protected String subtitleEvaluator(String expression, EventArguments args) {
if (expression.equals("loopCount"))
return String.valueOf(iterationCount);
return String.valueOf(evaluateIterationCount(args));
return null;
}

private int evaluateIterationCount(EventArguments args) {
String iterationCountEvaluated = ExpressionEvaluator.replaceExpressions(this.iterationCount, expression -> {
return ExpressionEvaluator.fromArgs(expression, args);
});

return Integer.parseInt(iterationCountEvaluated);
}

}

0 comments on commit afaa14a

Please sign in to comment.