Skip to content

Commit

Permalink
chore(tsl): add detailed info on item parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoodie committed Jan 1, 2020
1 parent 9723dda commit 55df8d6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 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=0.4.15
mod_version=0.4.16

minecraft_version=1.12.2
forge_version=14.23.3.2655
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ private void parseItem(List<String> itemPart) throws TSLSyntaxError {
String randomItem = ExpressionEvaluator.replaceExpressions(this.itemRaw, expression -> {
return ExpressionEvaluator.fromArgs(expression, randomEvent);
});
if (!new ItemParser(randomItem).isValid())
throw new TSLSyntaxError("Invalid item text");
ItemParser itemParser = new ItemParser(randomItem);
if (!itemParser.isValid()) {
throw new TSLSyntaxError("Invalid item text (%s)", itemParser.getError().getMessage());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public DropAction(List<String> words) throws TSLSyntaxError {
String randomItem = ExpressionEvaluator.replaceExpressions(this.itemRaw, expression -> {
return ExpressionEvaluator.fromArgs(expression, randomEvent);
});
if (!new ItemParser(randomItem).isValid())
throw new TSLSyntaxError("Invalid item text");
ItemParser itemParser = new ItemParser(randomItem);
if (!itemParser.isValid()) {
throw new TSLSyntaxError("Invalid item text (%s)", itemParser.getError().getMessage());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.programmer.igoodie.twitchspawn.tslanguage.parser.TSLSyntaxError;

public class ItemParser {

Expand Down Expand Up @@ -43,6 +44,19 @@ public boolean isValid() {
return true;
}

public TSLSyntaxError getError() {
if (getItem() == null)
return new TSLSyntaxError("Unknown item name");

try {
JsonToNBT.getTagFromJson(itemNbt);
return null;

} catch (NBTException e) {
return new TSLSyntaxError(e.getMessage());
}
}

public ItemStack generateItemStack(int amount) {
if (!isValid())
throw new IllegalArgumentException("Cannot generate item stack from invalid item text");
Expand Down

0 comments on commit 55df8d6

Please sign in to comment.