Search in sources :

Example 1 with Optional

use of stanhebben.zenscript.annotations.Optional in project ZenScript by CraftTweaker.

the class JavaMethod method getOptionalValue.

/**
 * Creates the value for the optional Expression
 *
 * @param position    position
 * @param javaMethod  Method to calculate for
 * @param parameterNr which parameter
 * @param environment ZS environment (used for ZS types and Expression creation)
 *
 * @return default optional (0, false, null) or default value according to the @Optional annotation
 */
private static Expression getOptionalValue(ZenPosition position, JavaMethod javaMethod, int parameterNr, IEnvironmentGlobal environment) {
    Parameter parameter = javaMethod.getMethod().getParameters()[parameterNr];
    Optional optional = parameter.getAnnotation(Optional.class);
    // No annotation (not sure how but lets be sure) -> Default value
    if (optional == null)
        return javaMethod.getParameterTypes()[parameterNr].defaultValue(position);
    Class<?> parameterType = parameter.getType();
    // Primitives
    if (parameterType.isPrimitive()) {
        Class<?> clazz = parameter.getType();
        if (clazz == int.class || clazz == short.class || clazz == long.class || clazz == byte.class)
            return new ExpressionInt(position, optional.valueLong(), environment.getType(clazz));
        else if (clazz == boolean.class)
            return new ExpressionBool(position, optional.valueBoolean());
        else if (clazz == float.class || clazz == double.class)
            return new ExpressionFloat(position, optional.valueDouble(), environment.getType(clazz));
        else {
            // Should never happen
            environment.error(position, "Optional Annotation Error, not a known primitive: " + clazz);
            return new ExpressionInvalid(position);
        }
    }
    Class<?> methodClass = optional.methodClass();
    if (methodClass == Optional.class) {
        // Backwards compat!
        return (parameterType == String.class && !optional.value().isEmpty()) ? new ExpressionString(position, optional.value()) : javaMethod.getParameterTypes()[parameterNr].defaultValue(position);
    }
    try {
        Method method = methodClass.getMethod(optional.methodName(), String.class);
        if (!parameterType.isAssignableFrom(method.getReturnType())) {
            environment.error(position, "Optional Annotation Error, cannot assign " + parameterType + " from " + method);
            return new ExpressionInvalid(position);
        }
        return new ExpressionCallStatic(position, environment, new JavaMethod(method, environment.getEnvironment().getTypeRegistry()), new ExpressionString(position, optional.value()));
    } catch (NoSuchMethodException ignored) {
        // Method not found --> Null
        environment.error(position, "Optional Annotation Error, cannot find method " + optional.methodName());
        return new ExpressionInvalid(position);
    }
}
Also used : Optional(stanhebben.zenscript.annotations.Optional) ParsedZenClassMethod(stanhebben.zenscript.definitions.zenclasses.ParsedZenClassMethod) Method(java.lang.reflect.Method) Parameter(java.lang.reflect.Parameter)

Example 2 with Optional

use of stanhebben.zenscript.annotations.Optional in project MrCrayfishFurnitureMod by MrCrayfish.

the class Blender method remove.

@ZenMethod
@ZenDoc("Remove matching blended drinks.")
public static void remove(@Optional final String name, @Optional final IItemStack[] ingredients, @Optional final Integer food, @Optional final int[] colour) {
    final StringBuilder description = new StringBuilder();
    Predicate<RecipeData> matcher = recipeData -> true;
    boolean first = true;
    description.append("Remove drink(s) matching '");
    if (name != null) {
        matcher = matcher.and((data) -> data.getDrinkName().equals(name));
        if (first)
            first = false;
        else
            description.append(',');
        description.append("name=").append(name);
    }
    if (ingredients != null) {
        matcher = matcher.and((data) -> {
            if (data.getIngredients().size() != ingredients.length)
                return false;
            LinkedList<IItemStack> toCheck = new LinkedList<>();
            Collections.addAll(toCheck, ingredients);
            outer: for (ItemStack stack : data.getIngredients()) {
                for (Iterator<IItemStack> it = toCheck.iterator(); it.hasNext(); ) {
                    IItemStack checker = it.next();
                    if (CraftTweakerMC.matchesExact(checker, stack)) {
                        it.remove();
                        continue outer;
                    }
                }
                return false;
            }
            return true;
        });
        if (first)
            first = false;
        else
            description.append(',');
        description.append("ingredients=").append(Arrays.toString(ingredients));
    }
    if (food != null) {
        matcher = matcher.and((data) -> data.getHealAmount() == food);
        if (first)
            first = false;
        else
            description.append(',');
        description.append("food=").append(food);
    }
    if (colour != null) {
        if (colour.length != 3)
            throw new IllegalArgumentException("colour must have 3 components");
        for (int c : colour) if (c < 0 || c > 255)
            throw new IllegalArgumentException("colour components must be between 0 and 255 inclusive");
        matcher = matcher.and((data) -> data.getRed() == colour[0] && data.getGreen() == colour[1] && data.getBlue() == colour[2]);
        if (first)
            first = false;
        else
            description.append(',');
        description.append("colour=").append(Arrays.toString(colour));
    }
    description.append("' from Blender");
    if (first) {
        description.setLength(0);
        description.append("Remove all drinks from Blender");
    }
    final Predicate<RecipeData> finalMatcher = matcher;
    CraftTweakerIntegration.defer(description.toString(), () -> {
        if (!Recipes.localBlenderRecipes.removeIf((data) -> {
            if (finalMatcher.test(data)) {
                CraftTweakerAPI.logInfo("Blender: Removed blended drink " + data);
                return true;
            }
            return false;
        })) {
            CraftTweakerAPI.logError("Blender: No blended drinks matched");
        }
    });
}
Also used : Optional(stanhebben.zenscript.annotations.Optional) Arrays(java.util.Arrays) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) ZenDoc(crafttweaker.annotations.ZenDoc) ZenClass(stanhebben.zenscript.annotations.ZenClass) ItemStack(net.minecraft.item.ItemStack) CraftTweakerAPI(crafttweaker.CraftTweakerAPI) IItemStack(crafttweaker.api.item.IItemStack) CraftTweakerMC(crafttweaker.api.minecraft.CraftTweakerMC) ZenMethod(stanhebben.zenscript.annotations.ZenMethod) LinkedList(java.util.LinkedList) RecipeData(com.mrcrayfish.furniture.api.RecipeData) Recipes(com.mrcrayfish.furniture.api.Recipes) Nonnull(javax.annotation.Nonnull) Collections(java.util.Collections) ZenRegister(crafttweaker.annotations.ZenRegister) IItemStack(crafttweaker.api.item.IItemStack) ItemStack(net.minecraft.item.ItemStack) IItemStack(crafttweaker.api.item.IItemStack) LinkedList(java.util.LinkedList) RecipeData(com.mrcrayfish.furniture.api.RecipeData) ZenDoc(crafttweaker.annotations.ZenDoc) ZenMethod(stanhebben.zenscript.annotations.ZenMethod)

Example 3 with Optional

use of stanhebben.zenscript.annotations.Optional in project MrCrayfishFurnitureMod by MrCrayfish.

the class OneToNoneRecipes method remove.

public void remove(@Optional IIngredient item) {
    if (item == null)
        item = IngredientAny.INSTANCE;
    final IIngredient finalItem = item;
    CraftTweakerIntegration.defer("Remove item(s) matching " + item + " from " + this.name, () -> {
        if (!this.recipes.removeIf((data) -> {
            if (finalItem.matchesExact(new MCItemStack(data.getInput()))) {
                CraftTweakerAPI.logInfo(this.name + ": Removed item " + data);
                return true;
            }
            return false;
        })) {
            CraftTweakerAPI.logError(name + ": No item matched");
        }
    });
}
Also used : Optional(stanhebben.zenscript.annotations.Optional) CraftTweakerIntegration(com.mrcrayfish.furniture.integration.crafttweaker.CraftTweakerIntegration) CraftTweakerAPI(crafttweaker.CraftTweakerAPI) IIngredient(crafttweaker.api.item.IIngredient) IngredientAny(crafttweaker.api.item.IngredientAny) IItemStack(crafttweaker.api.item.IItemStack) CraftTweakerMC(crafttweaker.api.minecraft.CraftTweakerMC) Collection(java.util.Collection) RecipeData(com.mrcrayfish.furniture.api.RecipeData) MCItemStack(crafttweaker.mc1120.item.MCItemStack) Nonnull(javax.annotation.Nonnull) IIngredient(crafttweaker.api.item.IIngredient) MCItemStack(crafttweaker.mc1120.item.MCItemStack)

Example 4 with Optional

use of stanhebben.zenscript.annotations.Optional in project MrCrayfishFurnitureMod by MrCrayfish.

the class OneToOneRecipes method remove.

public void remove(@Optional IIngredient output, @Optional IIngredient input) {
    if (output == null)
        output = IngredientAny.INSTANCE;
    if (input == null)
        input = IngredientAny.INSTANCE;
    final IIngredient finalOutput = output;
    final IIngredient finalInput = input;
    CraftTweakerIntegration.defer("Remove recipe(s) matching " + output + " given " + input + " from " + this.name, () -> {
        if (!this.recipes.removeIf((data) -> {
            if (finalOutput.matchesExact(new MCItemStack(data.getOutput())) && finalInput.matchesExact(new MCItemStack(data.getInput()))) {
                CraftTweakerAPI.logInfo(this.name + ": Removed recipe " + data);
                return true;
            }
            return false;
        })) {
            CraftTweakerAPI.logError(name + ": No recipe matched");
        }
    });
}
Also used : Optional(stanhebben.zenscript.annotations.Optional) CraftTweakerIntegration(com.mrcrayfish.furniture.integration.crafttweaker.CraftTweakerIntegration) CraftTweakerAPI(crafttweaker.CraftTweakerAPI) IIngredient(crafttweaker.api.item.IIngredient) IngredientAny(crafttweaker.api.item.IngredientAny) IItemStack(crafttweaker.api.item.IItemStack) CraftTweakerMC(crafttweaker.api.minecraft.CraftTweakerMC) Collection(java.util.Collection) RecipeData(com.mrcrayfish.furniture.api.RecipeData) MCItemStack(crafttweaker.mc1120.item.MCItemStack) Nonnull(javax.annotation.Nonnull) IIngredient(crafttweaker.api.item.IIngredient) MCItemStack(crafttweaker.mc1120.item.MCItemStack)

Example 5 with Optional

use of stanhebben.zenscript.annotations.Optional in project MrCrayfishFurnitureMod by MrCrayfish.

the class MineBay method remove.

@ZenMethod
@ZenDoc("Remove matching trades.")
public static void remove(@Optional IIngredient item) {
    if (item == null)
        item = IngredientAny.INSTANCE;
    final IIngredient finalItem = item;
    CraftTweakerIntegration.defer("Remove trade(s) matching " + item + " from MineBay", () -> {
        if (!Recipes.localMineBayRecipes.removeIf((data) -> {
            if (finalItem.matchesExact(new MCItemStack(data.getInput()))) {
                CraftTweakerAPI.logInfo("MineBay: Removed trade " + data);
                return true;
            }
            return false;
        })) {
            CraftTweakerAPI.logError("MineBay: No trades matched " + finalItem);
        }
    });
}
Also used : Optional(stanhebben.zenscript.annotations.Optional) ZenDoc(crafttweaker.annotations.ZenDoc) ZenClass(stanhebben.zenscript.annotations.ZenClass) CraftTweakerAPI(crafttweaker.CraftTweakerAPI) IIngredient(crafttweaker.api.item.IIngredient) IngredientAny(crafttweaker.api.item.IngredientAny) IItemStack(crafttweaker.api.item.IItemStack) CraftTweakerMC(crafttweaker.api.minecraft.CraftTweakerMC) ZenMethod(stanhebben.zenscript.annotations.ZenMethod) RecipeData(com.mrcrayfish.furniture.api.RecipeData) Recipes(com.mrcrayfish.furniture.api.Recipes) MCItemStack(crafttweaker.mc1120.item.MCItemStack) Nonnull(javax.annotation.Nonnull) ZenRegister(crafttweaker.annotations.ZenRegister) IIngredient(crafttweaker.api.item.IIngredient) MCItemStack(crafttweaker.mc1120.item.MCItemStack) ZenDoc(crafttweaker.annotations.ZenDoc) ZenMethod(stanhebben.zenscript.annotations.ZenMethod)

Aggregations

Optional (stanhebben.zenscript.annotations.Optional)5 RecipeData (com.mrcrayfish.furniture.api.RecipeData)4 CraftTweakerAPI (crafttweaker.CraftTweakerAPI)4 IItemStack (crafttweaker.api.item.IItemStack)4 CraftTweakerMC (crafttweaker.api.minecraft.CraftTweakerMC)4 Nonnull (javax.annotation.Nonnull)4 IIngredient (crafttweaker.api.item.IIngredient)3 IngredientAny (crafttweaker.api.item.IngredientAny)3 MCItemStack (crafttweaker.mc1120.item.MCItemStack)3 Recipes (com.mrcrayfish.furniture.api.Recipes)2 CraftTweakerIntegration (com.mrcrayfish.furniture.integration.crafttweaker.CraftTweakerIntegration)2 ZenDoc (crafttweaker.annotations.ZenDoc)2 ZenRegister (crafttweaker.annotations.ZenRegister)2 Collection (java.util.Collection)2 ZenClass (stanhebben.zenscript.annotations.ZenClass)2 ZenMethod (stanhebben.zenscript.annotations.ZenMethod)2 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1