-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tsl): implement FOR meta-action
- Loading branch information
Showing
3 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/java/net/programmer/igoodie/twitchspawn/tslanguage/action/ForAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package net.programmer.igoodie.twitchspawn.tslanguage.action; | ||
|
||
import net.minecraft.entity.player.ServerPlayerEntity; | ||
import net.programmer.igoodie.twitchspawn.tslanguage.EventArguments; | ||
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 java.util.List; | ||
|
||
public class ForAction extends TSLAction { | ||
|
||
private TSLAction action; | ||
private int iterationCount; | ||
|
||
/* | ||
* FOR 5 TIMES | ||
* SUMMON zombie | ||
* ON ... | ||
* | ||
* Word#0 -> 5 | ||
* Word#1 -> TIMES | ||
* Word#[2,n) -> <action> | ||
*/ | ||
public ForAction(List<String> words) throws TSLSyntaxError { | ||
this.message = TSLParser.parseMessage(words); | ||
List<String> actionWords = actionPart(words); | ||
|
||
if (actionWords.size() < 3) | ||
throw new TSLSyntaxError("Invalid length of words: " + actionWords); | ||
|
||
if (!actionWords.get(1).equalsIgnoreCase("TIMES")) | ||
throw new TSLSyntaxError("Expected TIMES, but found -> %s", actionWords.get(1)); | ||
|
||
try { | ||
this.iterationCount = Integer.parseInt(actionWords.get(0)); | ||
this.action = TSLParser.parseAction(words.get(2), words.subList(3, words.size())); | ||
|
||
} catch (NumberFormatException e) { | ||
throw new TSLSyntaxError("Malformed number word -> %s", actionWords.get(0)); | ||
} | ||
} | ||
|
||
@Override | ||
protected void performAction(ServerPlayerEntity player, EventArguments args) { | ||
for (int i = 0; i < iterationCount; i++) { | ||
action.performAction(player, args); | ||
} | ||
} | ||
|
||
@Override | ||
protected String associatedSubtitleAction() { | ||
return TSLActionKeyword.ofClass(action.getClass()); | ||
} | ||
|
||
@Override | ||
protected String subtitleEvaluator(String expression, EventArguments args) { | ||
if(expression.equals("loopCount")) | ||
return String.valueOf(iterationCount); | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters