Skip to content

Commit

Permalink
Proper Runtime Errors
Browse files Browse the repository at this point in the history
Now dropping maintenance
  • Loading branch information
TheAbsolutionism committed Jan 3, 2025
1 parent 4647ac1 commit 3897e40
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.config.Node;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
Expand All @@ -28,6 +29,7 @@
import org.skriptlang.skript.bukkit.recipes.CreateRecipeEvent.CraftingRecipeEvent.ShapedRecipeEvent;
import org.skriptlang.skript.bukkit.recipes.CreateRecipeEvent.SmithingRecipeEvent.SmithingTransformRecipeEvent;
import org.skriptlang.skript.bukkit.recipes.CreateRecipeEvent.SmithingRecipeEvent.SmithingTrimRecipeEvent;
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer;

import java.util.*;

Expand Down Expand Up @@ -67,7 +69,7 @@
"\tset the recipe transmute item to nether star named \"Free Upgrade\"",
"\tset the recipe result to netherite helmet"
})
public class ExprRecipeIngredients extends PropertyExpression<Recipe, ItemStack> {
public class ExprRecipeIngredients extends PropertyExpression<Recipe, ItemStack> implements SyntaxRuntimeErrorProducer {

enum RecipePattern {
INGREDIENTS("[recipe] ingredients", "recipe ingredients", CraftingRecipeEvent.class,
Expand Down Expand Up @@ -121,9 +123,10 @@ enum RecipePattern {

private boolean isEvent = false;
private RecipePattern selectedPattern;
private Node node;
private String rawExpr;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
selectedPattern = recipePatterns[matchedPattern / 2];
if (exprs[0].isDefault()) {
Expand All @@ -137,6 +140,8 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}
//noinspection unchecked
setExpr((Expression<? extends Recipe>) exprs[0]);
node = getParser().getNode();
rawExpr = parseResult.expr;
return true;
}

Expand Down Expand Up @@ -187,7 +192,7 @@ private List<ItemStack> getterIngredients(Recipe recipe) {
} else if (recipe instanceof ShapelessRecipe shapelessRecipe) {
ingredients.addAll(shapelessRecipe.getIngredientList());
} else {
Skript.error("You can only get the ingredients of a Shaped or Shapeless Recipe.");
error("You can only get the ingredients of a Shaped or Shapeless Recipe.");
}
return ingredients;
}
Expand All @@ -211,7 +216,7 @@ private List<ItemStack> getterRows(Recipe recipe) {
ingredients.add(stack);
}
} else {
Skript.error("You can only get the ingredients of a row for a Shaped Recipe.");
error("You can only get the ingredients of a row for a Shaped Recipe.");
}
return ingredients;
}
Expand Down Expand Up @@ -253,7 +258,7 @@ else if (recipe instanceof SmithingTrimRecipe trimRecipe)
);
}
} else {
Skript.error("You can only get the base items of a Smithing, Smithing Transform and Smithing Trim Recipe.");
error("You can only get the base items of a Smithing, Smithing Transform and Smithing Trim Recipe.");
}
return ingredients;
}
Expand All @@ -277,7 +282,7 @@ private List<ItemStack> getterInput(Recipe recipe) {
} else if (recipe instanceof TransmuteRecipe transmuteRecipe) {
choice = transmuteRecipe.getInput();
} else {
Skript.error("You can only get the input item of a Cooking, Blasting, Furnace, Campfire, Smoking and Stonecutting Recipe.");
error("You can only get the input item of a Cooking, Blasting, Furnace, Campfire, Smoking and Stonecutting Recipe.");
}
if (choice instanceof ExactChoice exactChoice) {
ingredients.addAll(exactChoice.getChoices());
Expand Down Expand Up @@ -306,7 +311,7 @@ private List<ItemStack> getterTransmute(Recipe recipe) {
@Override
public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
if (!isEvent) {
Skript.error("You can not set the " + selectedPattern.toString + " of existing recipes.");
error("You can not set the " + selectedPattern.toString + " of existing recipes.");
} else if (mode == ChangeMode.SET) {
return CollectionUtils.array(ItemType[].class);
}
Expand Down Expand Up @@ -354,15 +359,15 @@ private void changerIngredients(Map<Integer, ItemStack[]> items, MutableRecipe m
return;

if (items.size() > 9) {
Skript.error("You can only provide up to 9 items when setting the ingredients for a '" + recipeEvent.getRecipeType() + "' recipe.");
error("You can only provide up to 9 items when setting the ingredients for a '" + recipeEvent.getRecipeType() + "' recipe.");
recipeEvent.setErrorInSection();
return;
}
for (Map.Entry<Integer, ItemStack[]> entry : items.entrySet()) {
ItemStack[] ingredients = entry.getValue();
if (Arrays.stream(ingredients).anyMatch(itemStack -> itemStack.getType().isAir())) {
if (ingredients.length > 1) {
Skript.error("You can not provide air with a list of other items.");
error("You can not provide air with a list of other items.");
recipeEvent.setErrorInSection();
return;
} else {
Expand All @@ -378,15 +383,15 @@ private void changerRows(Map<Integer, ItemStack[]> items, MutableRecipe mutableR
if (!(mutableRecipe instanceof MutableShapedRecipe mutableShapedRecipe))
return;
if (items.size() > 3) {
Skript.error("You can only provide up to 3 items when setting the ingredients of a row for a '" + recipeEvent.getRecipeType() + "' recipe.");
error("You can only provide up to 3 items when setting the ingredients of a row for a '" + recipeEvent.getRecipeType() + "' recipe.");
recipeEvent.setErrorInSection();
return;
}
for (Map.Entry<Integer, ItemStack[]> entry : items.entrySet()) {
ItemStack[] ingredients = entry.getValue();
if (Arrays.stream(ingredients).anyMatch(itemStack -> itemStack.getType().isAir())) {
if (ingredients.length > 1) {
Skript.error("You can not provide 'air' with a list of other items.");
error("You can not provide 'air' with a list of other items.");
recipeEvent.setErrorInSection();
return;
} else {
Expand All @@ -404,7 +409,7 @@ private void changerSmithing(Map<Integer, ItemStack[]> items, MutableRecipe muta
List<ItemStack> stackList = new ArrayList<>();
items.entrySet().stream().forEach(entry -> stackList.addAll(Arrays.asList(entry.getValue())));
if (stackList.stream().anyMatch(itemStack -> itemStack.getType().isAir())) {
Skript.error("You can not provide 'air' with this expression.");
error("You can not provide 'air' with this expression.");
recipeEvent.setErrorInSection();
return;
}
Expand All @@ -420,7 +425,7 @@ private void changerInput(Map<Integer, ItemStack[]> items, MutableRecipe mutable
List<ItemStack> stackList = new ArrayList<>();
items.entrySet().stream().forEach(entry -> stackList.addAll(Arrays.asList(entry.getValue())));
if (stackList.stream().anyMatch(itemStack -> itemStack.getType().isAir())) {
Skript.error("You can not provide 'air' with this expression.");
error("You can not provide 'air' with this expression.");
recipeEvent.setErrorInSection();
return;
}
Expand All @@ -438,7 +443,7 @@ private void changerTransmute(Map<Integer, ItemStack[]> items, MutableRecipe mut
List<ItemStack> stackList = new ArrayList<>();
items.entrySet().stream().forEach(entry -> stackList.addAll(Arrays.asList(entry.getValue())));
if (stackList.stream().anyMatch(itemStack -> itemStack.getType().isAir())) {
Skript.error("You can not provide 'air' with this expression.");
error("You can not provide 'air' with this expression.");
recipeEvent.setErrorInSection();
return;
}
Expand All @@ -453,6 +458,16 @@ public Class<ItemStack> getReturnType() {
return ItemStack.class;
}

@Override
public Node getNode() {
return node;
}

@Override
public @Nullable String toHighlight() {
return rawExpr;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the " + selectedPattern.toString + " of " + getExpr().toString(event, debug);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.skriptlang.skript.bukkit.recipes.elements;

import ch.njol.skript.Skript;
import ch.njol.skript.config.Node;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
Expand All @@ -19,6 +20,7 @@
import org.skriptlang.skript.bukkit.recipes.CreateRecipeEvent;
import org.skriptlang.skript.bukkit.recipes.MutableRecipe;
import org.skriptlang.skript.bukkit.recipes.RecipeUtils.RecipeType;
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -79,7 +81,7 @@
"\tset the recipe result to netherite helmet"
})
@Since("INSERT VERSION")
public class ExprSecCreateRecipe extends SectionExpression<Recipe> {
public class ExprSecCreateRecipe extends SectionExpression<Recipe> implements SyntaxRuntimeErrorProducer {

private static final boolean SUPPORT_SMITHING = !Skript.isRunningMinecraft(1, 20, 0);

Expand All @@ -92,6 +94,8 @@ public class ExprSecCreateRecipe extends SectionExpression<Recipe> {
private RecipeType providedType;
private Expression<String> providedName;
private Trigger trigger;
private Node node;
private String rawExpr;

@Override
public boolean init(Expression<?>[] exprs, int pattern, Kleenean isDelayed, ParseResult result, @Nullable SectionNode node, @Nullable List<TriggerItem> triggerItems) {
Expand Down Expand Up @@ -121,19 +125,21 @@ public boolean init(Expression<?>[] exprs, int pattern, Kleenean isDelayed, Pars
Skript.error("Delays cannot be used within a 'create recipe' section.");
return false;
}
this.node = getParser().getNode();
rawExpr = result.expr;
return true;
}

@Override
protected Recipe @Nullable [] get(Event event) {
String name = providedName.getSingle(event);
if (name == null || name.isEmpty()) {
Skript.error("The id for a recipe must not be null nor empty.");
error("The id for a recipe must not be null nor empty.");
return null;
}
NamespacedKey key = NamespacedKey.fromString(name, Skript.getInstance());
if (key == null) {
Skript.error("The provided id is invalid.");
error("The provided id is invalid.");
return null;
}
CreateRecipeEvent recipeEvent = providedType.createRecipeEvent(key);
Expand All @@ -149,7 +155,7 @@ public boolean init(Expression<?>[] exprs, int pattern, Kleenean isDelayed, Pars
Recipe recipe = recipeWrapper.create();
// If the recipe failed to build
if (recipe == null) {
Skript.error(recipeWrapper.getErrors().toString());
error(recipeWrapper.getErrors().toString());
return null;
}
return new Recipe[]{recipe};
Expand All @@ -165,6 +171,16 @@ public Class<Recipe> getReturnType() {
return Recipe.class;
}

@Override
public Node getNode() {
return node;
}

@Override
public @Nullable String toHighlight() {
return rawExpr;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "a new " + providedType + " recipe with the key " + providedName.toString(event, debug);
Expand Down

0 comments on commit 3897e40

Please sign in to comment.