Search in sources :

Example 1 with ItemValidator

use of com.witchworks.api.recipe.ItemValidator in project Witchworks by Um-Mitternacht.

the class TileKettle method processingLogic.

//------------------------------------Crafting Logic------------------------------------//
@SuppressWarnings("ConstantConditions")
public boolean processingLogic(ItemStack stack) {
    if (!isBoiling() || hasIngredients() || stack.getCount() > 64)
        return false;
    Map<Item, ItemValidator<ItemStack>> processing = KettleRegistry.getKettleProcessing(inv.getInnerFluid());
    if (processing != null && processing.containsKey(stack.getItem())) {
        ItemValidator<ItemStack> validator = processing.get(stack.getItem());
        Optional<ItemStack> optional = validator.getMatchFor(stack);
        if (optional.isPresent()) {
            ItemStack out = optional.get().copy();
            if (stack.isItemDamaged() && out.isItemStackDamageable())
                out.setItemDamage(stack.getItemDamage());
            int fluidAmount = inv.getFluidAmount();
            int fluidTaken = 250;
            out.setCount(0);
            if (stack.getCount() <= 16) {
                out.setCount(stack.getCount());
                stack.setCount(0);
            } else {
                while (stack.getCount() > 0 && fluidTaken <= fluidAmount) {
                    stack.shrink(1);
                    out.grow(1);
                    if (out.getCount() % 16 == 0) {
                        if (fluidTaken >= fluidAmount) {
                            fluidTaken = fluidAmount;
                            break;
                        }
                        fluidTaken += 250;
                    }
                }
            }
            if (out.getCount() > 0) {
                final double x = getPos().getX();
                final double y = getPos().getY() + 1D;
                final double z = getPos().getZ();
                final EntityItem item = new EntityItem(world, x + 0.5D, y, z + 0.5D, out);
                item.motionX = world.rand.nextDouble() * 2 - 1;
                item.motionZ = world.rand.nextDouble() * 2 - 1;
                item.motionY = 0.1D;
                item.setPickupDelay(0);
                world.spawnEntity(item);
                play(SoundEvents.BLOCK_FIRE_EXTINGUISH, 1F, 1F);
                for (int i = 0; i < 4; i++) {
                    PacketHandler.spawnParticle(ParticleF.STEAM, world, x + world.rand.nextFloat(), getParticleLevel(), z + world.rand.nextFloat(), 5, 0, 0, 0);
                }
                inv.drain(fluidTaken, true);
                return true;
            }
        }
    }
    return false;
}
Also used : Item(net.minecraft.item.Item) EntityItem(net.minecraft.entity.item.EntityItem) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) ItemValidator(com.witchworks.api.recipe.ItemValidator) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 2 with ItemValidator

use of com.witchworks.api.recipe.ItemValidator in project Witchworks by Um-Mitternacht.

the class TileKettle method getBrewData.

@Nullable
public NBTTagCompound getBrewData() {
    final Map<Item, ItemValidator<Object>> brewEffect = KettleRegistry.getBrewEffect();
    final Map<Item, ItemValidator<BrewModifier>> brewModifier = KettleRegistry.getBrewModifier();
    List<Object> effects = new ArrayList<>();
    for (int i = 0; i < ingredients.length; i++) {
        ItemStack ingredient = ingredients[i];
        if (ingredient.isEmpty())
            break;
        Item effect = ingredient.getItem();
        boolean add = true;
        if (!brewEffect.containsKey(effect)) {
            failHorribly();
            return null;
        }
        ItemValidator<Object> validator = brewEffect.get(effect);
        Optional<Object> optional = validator.getMatchFor(ingredient);
        if (!optional.isPresent()) {
            failHorribly();
            return null;
        }
        Object brew = copyBrew(optional.get());
        if (i + 1 < ingredients.length) {
            while (i + 1 < ingredients.length) {
                ItemStack modifier = ingredients[i + 1];
                if (!brewModifier.containsKey(modifier.getItem())) {
                    if (brewEffect.containsKey(modifier.getItem()) || modifier.isEmpty())
                        break;
                    failHorribly();
                    return null;
                }
                ItemValidator<BrewModifier> val = brewModifier.get(modifier.getItem());
                Optional<BrewModifier> opt = val.getMatchFor(modifier);
                if (opt.isPresent()) {
                    for (int j = 0, size = modifier.getCount(); j < size; j++) {
                        add = opt.get().apply(effects, brew);
                    }
                } else {
                    failHorribly();
                    return null;
                }
                ++i;
            }
        }
        if (add)
            effects.add(brew);
    }
    return BrewUtils.serialize(effects);
}
Also used : ArrayList(java.util.ArrayList) BrewModifier(com.witchworks.api.recipe.BrewModifier) Item(net.minecraft.item.Item) EntityItem(net.minecraft.entity.item.EntityItem) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) ItemValidator(com.witchworks.api.recipe.ItemValidator) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable)

Example 3 with ItemValidator

use of com.witchworks.api.recipe.ItemValidator in project Witchworks by Um-Mitternacht.

the class KettleRegistry method addKettleProcessing.

/**
	 * Register an Item to the Processing factory.
	 *
	 * @param fluid  The fluid this Item needs
	 * @param in     The Item you throw in
	 * @param out    The Item that comes out
	 * @param strict If the Item must be identical
	 */
public static void addKettleProcessing(Fluid fluid, ItemStack in, ItemStack out, boolean strict) {
    if (KETTLE_PROCESSING.containsKey(fluid)) {
        Map<Item, ItemValidator<ItemStack>> map = KETTLE_PROCESSING.get(fluid);
        Item item = in.getItem();
        if (map.containsKey(item)) {
            map.get(item).add(in, out, strict);
        } else {
            map.put(item, new ItemValidator<ItemStack>().add(in, out, strict));
        }
    } else {
        Map<Item, ItemValidator<ItemStack>> map = new HashMap<>();
        map.put(in.getItem(), new ItemValidator<ItemStack>().add(in, out, strict));
        KETTLE_PROCESSING.put(fluid, map);
    }
}
Also used : Item(net.minecraft.item.Item) HashMap(java.util.HashMap) ItemValidator(com.witchworks.api.recipe.ItemValidator)

Aggregations

ItemValidator (com.witchworks.api.recipe.ItemValidator)3 Item (net.minecraft.item.Item)3 EntityItem (net.minecraft.entity.item.EntityItem)2 ItemStack (net.minecraft.item.ItemStack)2 IFluidHandlerItem (net.minecraftforge.fluids.capability.IFluidHandlerItem)2 BrewModifier (com.witchworks.api.recipe.BrewModifier)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Nullable (javax.annotation.Nullable)1