Search in sources :

Example 6 with IAgriCrop

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

the class TileEntityCrop method spread.

@Override
public boolean spread() {
    // If remote, abort.
    if (this.isRemote()) {
        return false;
    }
    // Fetch the seed;
    final AgriSeed seed = this.getSeed();
    // If don't have plant, abort.
    if (seed == null) {
        return false;
    }
    final IAgriPlant plant = seed.getPlant();
    // If don't roll a spread event, abort.
    if (plant.getSpreadChance() <= this.getRandom().nextDouble()) {
        for (IAgriCrop crop : WorldHelper.getTileNeighbors(worldObj, pos, IAgriCrop.class)) {
            final AgriSeed other = crop.getSeed();
            if (other == null) {
                if (!crop.isCrossCrop()) {
                    crop.setSeed(seed);
                    return true;
                }
            } else if (canOvertake(seed, other, this.getRandom())) {
                crop.setCrossCrop(false);
                crop.setSeed(seed);
                return true;
            }
        }
    }
    // The spreading failed.
    return false;
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed)

Example 7 with IAgriCrop

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

the class PlayerInteractEventHandler method vanillaSeedPlanting.

/**
     * Event handler to disable vanilla farming
     */
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void vanillaSeedPlanting(PlayerInteractEvent.RightClickBlock event) {
    // If not disabled, don't bother.
    if (!AgriCraftConfig.disableVanillaFarming) {
        return;
    }
    // Fetch the event itemstack.
    final ItemStack stack = event.getItemStack();
    // If the stack is null, or otherwise invalid, who cares?
    if (!StackHelper.isValid(stack)) {
        return;
    }
    // If the item in the player's hand is not a seed, who cares?
    if (!AgriApi.getSeedRegistry().hasAdapter(stack)) {
        return;
    }
    // Fetch world information.
    final BlockPos pos = event.getPos();
    final World world = event.getWorld();
    final IBlockState state = world.getBlockState(pos);
    // Fetch the block at the location.
    final Block block = state.getBlock();
    // If clicking crop block, who cares?
    if (block instanceof IAgriCrop) {
        return;
    }
    // If the item is an instance of IPlantable we need to perfom an extra check.
    if (stack.getItem() instanceof IPlantable) {
        // If the clicked block cannot support the given plant, then who cares?
        if (!block.canSustainPlant(state, world, pos, EnumFacing.UP, (IPlantable) stack.getItem())) {
            return;
        }
    }
    // If clicking crop tile, who cares?
    if (WorldHelper.getTile(event.getWorld(), event.getPos(), IAgriCrop.class).isPresent()) {
        return;
    }
    // The player is attempting to plant a seed, which is simply unacceptable.
    // We must deny this event.
    event.setUseItem(Event.Result.DENY);
    // If we are on the client side we are done.
    if (event.getSide().isClient()) {
        return;
    }
    // Should the server notify the player that vanilla farming has been disabled?
    if (AgriCraftConfig.showDisabledVanillaFarmingWarning) {
        MessageUtil.messagePlayer(event.getEntityPlayer(), ChatFormatting.GRAY + "Vanilla planting is disabled!");
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) IPlantable(net.minecraftforge.common.IPlantable) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) World(net.minecraft.world.World) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 8 with IAgriCrop

use of com.infinityraider.agricraft.api.v1.crop.IAgriCrop 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.getWorld(), crop.getPos(), 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).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)

Example 9 with IAgriCrop

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

the class ItemClipper method onItemUse.

//this is called when you right click with this item in hand
@Override
public EnumActionResult onItemUse(ItemStack item, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitx, float hity, float hitz) {
    if (world.isRemote) {
        return EnumActionResult.SUCCESS;
    }
    TileEntity te = world.getTileEntity(pos);
    if (te instanceof IAgriCrop) {
        IAgriCrop crop = (IAgriCrop) te;
        if (crop.hasSeed() && crop.getGrowthStage() > 1) {
            crop.setGrowthStage(crop.getGrowthStage() - 1);
            AgriSeed seed = crop.getSeed();
            seed = seed.withStat(seed.getStat());
            world.spawnEntityInWorld(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), ItemClipping.getClipping(seed, 1)));
            return EnumActionResult.SUCCESS;
        }
        return EnumActionResult.FAIL;
    }
    //return PASS or else no other use methods will be called (for instance "onBlockActivated" on the crops block)
    return EnumActionResult.PASS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) EntityItem(net.minecraft.entity.item.EntityItem)

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