Search in sources :

Example 1 with IAgriCrop

use of com.infinityraider.agricraft.api.v1.crop.IAgriCrop in project AgriCraft by AgriCraft.

the class ItemClipping method onItemUseFirst.

//this is called when you right click with this item in hand
@Override
public EnumActionResult onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
    TileEntity te = world.getTileEntity(pos);
    if (world.isRemote || !StackHelper.hasTag(stack) || !(te instanceof IAgriCrop)) {
        return EnumActionResult.PASS;
    }
    IAgriCrop crop = (IAgriCrop) te;
    AgriSeed seed = AgriApi.getSeedRegistry().valueOf(stack).orElse(null);
    if (!crop.acceptsSeed(seed) || seed == null) {
        return EnumActionResult.FAIL;
    }
    stack.stackSize = stack.stackSize - 1;
    if (world.rand.nextInt(10) <= seed.getStat().getStrength()) {
        crop.setSeed(seed);
    }
    return EnumActionResult.SUCCESS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed)

Example 2 with IAgriCrop

use of com.infinityraider.agricraft.api.v1.crop.IAgriCrop in project AgriCraft by AgriCraft.

the class StatCalculatorBase method calculateSpreadStats.

@Override
public IAgriStat calculateSpreadStats(IAgriPlant child, Collection<IAgriCrop> parents) {
    // Validate parameters.
    Objects.requireNonNull(child, "The child plant to calculate the stats for must not be null!");
    Objects.requireNonNull(parents, "The set of parents to calculate the child's stats from must not be null!");
    // Variables
    int invalidParents = 0;
    int validParents = 0;
    int growth = 0;
    int gain = 0;
    int strength = 0;
    // Sum values
    for (IAgriCrop parent : parents) {
        // Skip parent if null.
        if (parent == null) {
            continue;
        }
        // Fetch the seed associated with the parent.
        final AgriSeed parentSeed = parent.getSeed();
        // Skip if parent seed is null.
        if (parentSeed == null) {
            continue;
        }
        // If the parent is not mature, counts as invalid parent.
        if (!parent.isMature()) {
            invalidParents++;
            continue;
        }
        // If the parent plant does not match the child plant, invalid parent.
        if (!Objects.equals(child, parentSeed.getPlant())) {
            invalidParents++;
            continue;
        }
        // Otherwise everything is aok.
        validParents++;
        growth += parentSeed.getStat().getGrowth();
        gain += parentSeed.getStat().getGain();
        strength += parentSeed.getStat().getStrength();
    }
    // Determine the stat divisor.
    final int meanDivisor = calculateStatMeanDivisor(validParents, invalidParents);
    // Perform averages.
    growth = growth / meanDivisor;
    gain = gain / meanDivisor;
    strength = strength / meanDivisor;
    // Return the new plant stat.
    return new PlantStats(calculateStat(growth, validParents, 1), calculateStat(gain, validParents, 1), calculateStat(strength, validParents, 1));
}
Also used : IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) PlantStats(com.infinityraider.agricraft.farming.PlantStats)

Example 3 with IAgriCrop

use of com.infinityraider.agricraft.api.v1.crop.IAgriCrop in project AgriCraft by AgriCraft.

the class SpreadStrategy method executeStrategy.

@Override
public Optional<AgriSeed> executeStrategy(IAgriCrop crop, Random rand) {
    List<IAgriCrop> matureNeighbours = WorldHelper.getTileNeighbors(crop.getWorld(), crop.getPos(), IAgriCrop.class);
    matureNeighbours.removeIf(c -> !c.isMature());
    if (!matureNeighbours.isEmpty()) {
        int index = rand.nextInt(matureNeighbours.size());
        AgriSeed seed = matureNeighbours.get(index).getSeed();
        if (seed != null && rand.nextDouble() < seed.getPlant().getSpreadChance()) {
            return AgriApi.getStatCalculatorRegistry().valueOf(seed.getPlant()).map(calc -> calc.calculateSpreadStats(seed.getPlant(), matureNeighbours)).map(stat -> new AgriSeed(seed.getPlant(), stat));
        }
    }
    return Optional.empty();
}
Also used : List(java.util.List) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) IAgriCrossStrategy(com.infinityraider.agricraft.api.v1.mutation.IAgriCrossStrategy) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) AgriCraftConfig(com.infinityraider.agricraft.reference.AgriCraftConfig) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) WorldHelper(com.infinityraider.infinitylib.utility.WorldHelper) Optional(java.util.Optional) Random(java.util.Random) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed)

Example 4 with IAgriCrop

use of com.infinityraider.agricraft.api.v1.crop.IAgriCrop in project AgriCraft by AgriCraft.

the class StatCalculatorBase method calculateMutationStats.

@Override
public IAgriStat calculateMutationStats(IAgriMutation mutation, Collection<IAgriCrop> parents) {
    // Validate parameters.
    Objects.requireNonNull(mutation, "The mutation to calculate the stats for must not be null!");
    Objects.requireNonNull(parents, "The set of parents to calculate the mutation result stats from must not be null!");
    // Variables
    int invalidParents = 0;
    int validParents = 0;
    int growth = 0;
    int gain = 0;
    int strength = 0;
    // Sum values
    for (IAgriCrop parent : parents) {
        // Skip parent if null.
        if (parent == null) {
            continue;
        }
        // Fetch the seed associated with the parent.
        final AgriSeed parentSeed = parent.getSeed();
        // Skip if parent seed is null.
        if (parentSeed == null) {
            continue;
        }
        // If the parent is not mature, counts as invalid parent.
        if (!parent.isMature()) {
            invalidParents++;
            continue;
        }
        // If the parent plant does not match the child plant, invalid parent.
        if (!mutation.hasParent(parentSeed.getPlant())) {
            invalidParents++;
            continue;
        }
        // Otherwise everything is aok.
        validParents++;
        growth += parentSeed.getStat().getGrowth();
        gain += parentSeed.getStat().getGain();
        strength += parentSeed.getStat().getStrength();
    }
    // Determine the stat divisor.
    final int meanDivisor = calculateStatMeanDivisor(validParents, invalidParents);
    // Perform averages.
    growth = growth / meanDivisor;
    gain = gain / meanDivisor;
    strength = strength / meanDivisor;
    // Return the new plant stat.
    return new PlantStats(calculateStat(growth, validParents, AgriCraftConfig.cropStatDivisor), calculateStat(gain, validParents, AgriCraftConfig.cropStatDivisor), calculateStat(strength, validParents, AgriCraftConfig.cropStatDivisor));
}
Also used : IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) PlantStats(com.infinityraider.agricraft.farming.PlantStats)

Example 5 with IAgriCrop

use of com.infinityraider.agricraft.api.v1.crop.IAgriCrop in project AgriCraft by AgriCraft.

the class ItemTrowel method onItemUse.

// this is called when you right click with this item in hand
@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitx, float hity, float hitz) {
    TileEntity te = world.getTileEntity(pos);
    if (te instanceof IAgriCrop) {
        IAgriCrop crop = (IAgriCrop) te;
        Optional<AgriSeed> seed = AgriApi.getSeedRegistry().valueOf(stack);
        if (crop.hasSeed() && !seed.isPresent()) {
            seed = Optional.ofNullable(crop.getSeed());
            crop.setSeed(null);
            if (seed.isPresent()) {
                NBTTagCompound tag = new NBTTagCompound();
                tag.setString(AgriNBT.SEED, seed.get().getPlant().getId());
                seed.get().getStat().writeToNBT(tag);
                stack.setTagCompound(tag);
                stack.setItemDamage(1);
                return EnumActionResult.SUCCESS;
            } else {
                return EnumActionResult.FAIL;
            }
        } else if (seed.isPresent() && !crop.hasSeed()) {
            if (crop.setSeed(seed.get())) {
                stack.setTagCompound(new NBTTagCompound());
                stack.setItemDamage(0);
                return EnumActionResult.SUCCESS;
            } else {
                return EnumActionResult.FAIL;
            }
        }
    }
    return EnumActionResult.PASS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed)

Aggregations

IAgriCrop (com.infinityraider.agricraft.api.v1.crop.IAgriCrop)9 AgriSeed (com.infinityraider.agricraft.api.v1.seed.AgriSeed)8 TileEntity (net.minecraft.tileentity.TileEntity)3 AgriApi (com.infinityraider.agricraft.api.v1.AgriApi)2 IAgriCrossStrategy (com.infinityraider.agricraft.api.v1.mutation.IAgriCrossStrategy)2 IAgriPlant (com.infinityraider.agricraft.api.v1.plant.IAgriPlant)2 PlantStats (com.infinityraider.agricraft.farming.PlantStats)2 AgriCraftConfig (com.infinityraider.agricraft.reference.AgriCraftConfig)2 WorldHelper (com.infinityraider.infinitylib.utility.WorldHelper)2 List (java.util.List)2 Optional (java.util.Optional)2 Random (java.util.Random)2 IAgriMutation (com.infinityraider.agricraft.api.v1.mutation.IAgriMutation)1 IAgriStat (com.infinityraider.agricraft.api.v1.stat.IAgriStat)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Block (net.minecraft.block.Block)1 IBlockState (net.minecraft.block.state.IBlockState)1 EntityItem (net.minecraft.entity.item.EntityItem)1 ItemStack (net.minecraft.item.ItemStack)1