Search in sources :

Example 21 with AgriSeed

use of com.infinityraider.agricraft.api.v1.seed.AgriSeed in project AgriCraft by AgriCraft.

the class TileEntityCrop method spawn.

// =========================================================================
// IWeedable Methods
// =========================================================================
public boolean spawn() {
    // If in remote world, abort!
    if (this.isRemote()) {
        return false;
    }
    // If already have plant, abort!
    if (this.hasSeed()) {
        return false;
    }
    // Attempt to spawn plant.
    for (IAgriPlant p : AgriApi.getPlantRegistry().all()) {
        if (p.getSpawnChance() > this.getRandom().nextDouble() && this.isFertile(p)) {
            this.setCrossCrop(false);
            this.setSeed(new AgriSeed(p, new PlantStats()));
            return true;
        }
    }
    // The operation was a failure.
    return false;
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) PlantStats(com.infinityraider.agricraft.farming.PlantStats)

Example 22 with AgriSeed

use of com.infinityraider.agricraft.api.v1.seed.AgriSeed in project AgriCraft by AgriCraft.

the class TileEntitySeedStorage method setSlotContents.

@Override
public void setSlotContents(int realSlotId, ItemStack inputStack) {
    if (realSlotId < 0) {
        this.addStackToInventory(inputStack);
        return;
    }
    if (this.isValidForSlot(realSlotId, inputStack)) {
        SeedStorageSlot slotAt = this.slots.get(realSlotId);
        if (slotAt != null) {
            slotAt.count = inputStack.getCount();
            if (slotAt.count <= 0) {
                slots.remove(realSlotId);
                slotsList.remove(slotAt);
            }
        } else {
            final AgriSeed seed = AgriApi.getSeedRegistry().valueOf(inputStack).get();
            slotAt = new SeedStorageSlot(seed, inputStack.getCount(), realSlotId, this.getControllableID());
            if (slotAt.count > 0) {
                this.slots.put(realSlotId, slotAt);
                this.slotsList.add(slotAt);
            }
        }
        if (!this.getWorld().isRemote) {
            this.syncSlotToClient(slotAt);
        } else {
            this.markForUpdate();
        }
    }
}
Also used : AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed)

Example 23 with AgriSeed

use of com.infinityraider.agricraft.api.v1.seed.AgriSeed in project AgriCraft by AgriCraft.

the class AgriCraftFarmerBehavior method tryPlantSeed.

@Override
public FarmerResult tryPlantSeed(ItemStack seed, World world, BlockPos pos, IFarmer farmer) {
    // Attempt to resolve seed and crops instance.
    final Optional<AgriSeed> agriSeed = AgriApi.getSeedRegistry().valueOf(seed);
    final Optional<IAgriCrop> agriCrop = WorldHelper.getTile(world, pos, IAgriCrop.class);
    // If both are there, attempt to plant.
    if (agriSeed.isPresent() && agriCrop.isPresent()) {
        // Ensure enough energy.
        if (farmer.getEnergy() < ActuallyAdditionsPlugin.ENERGY_COST) {
            return FarmerResult.STOP_PROCESSING;
        }
        final MethodResult result = agriCrop.get().onApplySeeds(agriSeed.get(), null);
        if (result == MethodResult.SUCCESS) {
            farmer.extractEnergy(250);
            return FarmerResult.SUCCESS;
        }
    }
    // Otherwise fail.
    return FarmerResult.FAIL;
}
Also used : IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) MethodResult(com.infinityraider.agricraft.api.v1.util.MethodResult)

Example 24 with AgriSeed

use of com.infinityraider.agricraft.api.v1.seed.AgriSeed in project AgriCraft by AgriCraft.

the class MutateStrategy method executeStrategy.

@Override
public Optional<AgriSeed> executeStrategy(IAgriCrop crop, Random rand) {
    // Validate the parameters.
    Objects.requireNonNull(crop, "You cannot execute a mutation on a null crop!");
    Objects.requireNonNull(rand, "The random passed to a mutation strategy should not be null!");
    // Fetch all neighboring crop instances.
    final List<IAgriCrop> neighbors = WorldHelper.getTileNeighbors(crop.getCropWorld(), crop.getCropPos(), IAgriCrop.class);
    // Determine all possible parents.
    final List<IAgriPlant> parents = neighbors.stream().filter(IAgriCrop::isMature).map(IAgriCrop::getSeed).filter(Objects::nonNull).map(AgriSeed::getPlant).collect(Collectors.toList());
    // If we have less than two parents, might as well as abort.
    if (parents.size() < 2) {
        return Optional.empty();
    }
    // Determine the list of possible cross-over mutations.
    final List<IAgriMutation> mutations = AgriApi.getMutationRegistry().stream().filter(m -> m.areParentsIn(parents)).filter(m -> crop.isFertile(m.getChild())).collect(Collectors.toList());
    // If we didn't find any valid mutations, might as well as abort.
    if (mutations.isEmpty()) {
        return Optional.empty();
    }
    // Choose a random index in the list.
    final int index = rand.nextInt(mutations.size());
    // Fetch the chosen mutation from the list.
    final IAgriMutation mutation = mutations.get(index);
    // Determine if we should actually go through with this.
    if (mutation.getChance() <= rand.nextDouble()) {
        return Optional.empty();
    }
    // Calculate the stat associated with the new plant.
    Optional<IAgriStat> stat = AgriApi.getStatCalculatorRegistry().valueOf(mutation.getChild()).map(c -> c.calculateMutationStats(mutation, neighbors));
    // Return the mutation result.
    return stat.map(s -> new AgriSeed(mutation.getChild(), s));
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) IAgriCrossStrategy(com.infinityraider.agricraft.api.v1.mutation.IAgriCrossStrategy) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) Random(java.util.Random) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) AgriCraftConfig(com.infinityraider.agricraft.reference.AgriCraftConfig) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) IAgriMutation(com.infinityraider.agricraft.api.v1.mutation.IAgriMutation) WorldHelper(com.infinityraider.infinitylib.utility.WorldHelper) Optional(java.util.Optional) IAgriStat(com.infinityraider.agricraft.api.v1.stat.IAgriStat) IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) IAgriStat(com.infinityraider.agricraft.api.v1.stat.IAgriStat) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) Objects(java.util.Objects) IAgriMutation(com.infinityraider.agricraft.api.v1.mutation.IAgriMutation) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed)

Aggregations

AgriSeed (com.infinityraider.agricraft.api.v1.seed.AgriSeed)24 IAgriCrop (com.infinityraider.agricraft.api.v1.crop.IAgriCrop)12 ItemStack (net.minecraft.item.ItemStack)7 IAgriPlant (com.infinityraider.agricraft.api.v1.plant.IAgriPlant)6 TileEntity (net.minecraft.tileentity.TileEntity)5 AgriApi (com.infinityraider.agricraft.api.v1.AgriApi)4 IAgriStat (com.infinityraider.agricraft.api.v1.stat.IAgriStat)4 PlantStats (com.infinityraider.agricraft.farming.PlantStats)4 Optional (java.util.Optional)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 List (java.util.List)3 IAgriCrossStrategy (com.infinityraider.agricraft.api.v1.mutation.IAgriCrossStrategy)2 AgriCraftConfig (com.infinityraider.agricraft.reference.AgriCraftConfig)2 TileEntityCrop (com.infinityraider.agricraft.tiles.TileEntityCrop)2 WorldHelper (com.infinityraider.infinitylib.utility.WorldHelper)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 Nonnull (javax.annotation.Nonnull)2 EntityItem (net.minecraft.entity.item.EntityItem)2 AgriCore (com.agricraft.agricore.core.AgriCore)1