Skip to content

Commit

Permalink
feat(tsl): implement FOR meta-action
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoodie committed Aug 27, 2019
1 parent f639547 commit 3e13df4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
@@ -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;
}

}
Expand Up @@ -78,8 +78,6 @@ protected void performAction(ServerPlayerEntity player, EventArguments args) {
rawCoordZ,
(nbt == null ? "{}" : nbt.toString()));

System.out.printf("Running %s\n", command);

player.getServer().getCommandManager().handleCommand(player.getCommandSource()
.withPermissionLevel(9999).withFeedbackDisabled(), command);
}
Expand Down
Expand Up @@ -13,6 +13,7 @@ public enum TSLActionKeyword {

EITHER(false, EitherAction.class),
BOTH(false, BothAction.class),
FOR(false, ForAction.class),
;

public static boolean exists(String actionName) {
Expand Down

0 comments on commit 3e13df4

Please sign in to comment.