Search in sources :

Example 1 with AgriSeed

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

the class BlockCrop method onBlockActivated.

/*
     * Handles right-clicks from the player (a.k.a usage).
     * <br>
     * When the block is right clicked, the behaviour depends on the crop, and
     * what item it was clicked with.
     */
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
    // Step 0. Abort if remote.
    if (world.isRemote) {
        // I'm not sure if this is right, but oh well.
        return true;
    }
    // Step 1. Fetch the crop.
    TileEntityCrop crop = WorldHelper.getTile(world, pos, TileEntityCrop.class).orElse(null);
    // Step 2. Give up if the crop doesn't exist;
    if (crop == null) {
        // Allow others to use the click event.
        return false;
    }
    // Step 3. If the player is not holding anything, then harvest the crop.
    if (heldItem == null) {
        crop.onHarvest(player);
        return true;
    }
    // Step 4. If the held item is excluded from handling, skip it.
    if (TypeHelper.isAnyType(heldItem.getItem(), ITEM_EXCLUDES)) {
        // Allow the excludes to do their things.
        return false;
    }
    // Step 5. If the held item is a type of fertilizer, apply it.
    if (AgriApi.getFertilizerRegistry().hasAdapter(heldItem)) {
        Optional<IAgriFertilizer> fert = AgriApi.getFertilizerRegistry().valueOf(heldItem);
        return fert.isPresent() && fert.get().applyFertilizer(player, world, pos, crop, heldItem, crop.getRandom());
    }
    // Step 6. If the held item is crops, attempt to make cross-crops.
    if (heldItem.getItem() == AgriItems.getInstance().CROPS) {
        // Attempt to apply crop-sticks to crop.
        if (crop.onApplyCrops(player) == MethodResult.SUCCESS) {
            // If player isn't in creative remove an item from the stack.
            if (!player.isCreative()) {
                heldItem.stackSize--;
            }
            // The application was a success!
            return true;
        }
    }
    // Step 7. Attempt to resolve held item as a seed.
    final Optional<AgriSeed> seed = AgriApi.getSeedRegistry().valueOf(heldItem);
    // Step 8. If held item is a seed, attempt to plant it in the crop.
    if (seed.isPresent()) {
        if (crop.onApplySeeds(player, seed.get()) == MethodResult.SUCCESS) {
            // The planting was a success!
            return true;
        }
    }
    // Step 8. If we can't do anything else, give up and attemp to harvest instead.
    crop.onHarvest(player);
    //Returning true will prevent other things from happening
    return true;
}
Also used : TileEntityCrop(com.infinityraider.agricraft.tiles.TileEntityCrop) IAgriFertilizer(com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizer) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed)

Example 2 with AgriSeed

use of com.infinityraider.agricraft.api.v1.seed.AgriSeed 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 3 with AgriSeed

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

the class ItemAgriSeed method valueOf.

@Override
public Optional<AgriSeed> valueOf(Object obj) {
    NBTTagCompound tag = NBTHelper.asTag(obj);
    if (tag == null) {
        return Optional.empty();
    }
    IAgriPlant plant = AgriApi.getPlantRegistry().get(tag.getString(AgriNBT.SEED)).orElse(null);
    IAgriStat stat = AgriApi.getStatRegistry().valueOf(tag).orElse(null);
    if (plant != null && stat != null) {
        return Optional.of(new AgriSeed(plant, stat));
    } else {
        return Optional.empty();
    }
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) IAgriStat(com.infinityraider.agricraft.api.v1.stat.IAgriStat) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed)

Example 4 with AgriSeed

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

the class ContainerSeedStorageBase method transferStackInSlot.

/**
     * Handles shift clicking in the inventory, return the stack that was
     * transferred
     */
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int clickedSlot) {
    ItemStack originalStackInSlot = null;
    Slot slot = this.inventorySlots.get(clickedSlot);
    if (slot != null && slot.getHasStack()) {
        ItemStack notMergedStack = slot.getStack();
        originalStackInSlot = notMergedStack.copy();
        //try to move item from the player's inventory into the container
        AgriSeed seed = AgriApi.getSeedRegistry().valueOf(notMergedStack).orElse(null);
        if (seed != null && seed.getStat().isAnalyzed()) {
            ISeedStorageControllable controllable = this.getControllable(notMergedStack).orElse(null);
            if (controllable != null && controllable.hasLockedSeed()) {
                ItemStack locked = controllable.getLockedSeed().map(s -> s.toStack()).orElse(null);
                if (notMergedStack.getItem() != locked.getItem() || notMergedStack.getItemDamage() != locked.getItemDamage()) {
                    return null;
                }
            }
            if (this.addSeedToStorage(notMergedStack)) {
                notMergedStack.stackSize = 0;
            } else {
                return null;
            }
        }
        if (notMergedStack.stackSize == 0) {
            slot.putStack(null);
        } else {
            slot.onSlotChanged();
        }
        if (notMergedStack.stackSize == originalStackInSlot.stackSize) {
            return null;
        }
        slot.onPickupFromSlot(player, notMergedStack);
    }
    return originalStackInSlot;
}
Also used : MessageContainerSeedStorage(com.infinityraider.agricraft.network.MessageContainerSeedStorage) ISeedStorageControllable(com.infinityraider.agricraft.tiles.storage.ISeedStorageControllable) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) ContainerBase(com.infinityraider.infinitylib.container.ContainerBase) SeedStorageSlot(com.infinityraider.agricraft.tiles.storage.SeedStorageSlot) FMLCommonHandler(net.minecraftforge.fml.common.FMLCommonHandler) InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) ItemStack(net.minecraft.item.ItemStack) List(java.util.List) Side(net.minecraftforge.fml.relauncher.Side) EntityPlayer(net.minecraft.entity.player.EntityPlayer) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) ISeedStorageController(com.infinityraider.agricraft.tiles.storage.ISeedStorageController) Slot(net.minecraft.inventory.Slot) Optional(java.util.Optional) TileEntity(net.minecraft.tileentity.TileEntity) ISeedStorageControllable(com.infinityraider.agricraft.tiles.storage.ISeedStorageControllable) SeedStorageSlot(com.infinityraider.agricraft.tiles.storage.SeedStorageSlot) Slot(net.minecraft.inventory.Slot) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) ItemStack(net.minecraft.item.ItemStack)

Example 5 with AgriSeed

use of com.infinityraider.agricraft.api.v1.seed.AgriSeed 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)

Aggregations

AgriSeed (com.infinityraider.agricraft.api.v1.seed.AgriSeed)18 IAgriCrop (com.infinityraider.agricraft.api.v1.crop.IAgriCrop)8 IAgriPlant (com.infinityraider.agricraft.api.v1.plant.IAgriPlant)6 AgriApi (com.infinityraider.agricraft.api.v1.AgriApi)5 Optional (java.util.Optional)5 IAgriStat (com.infinityraider.agricraft.api.v1.stat.IAgriStat)4 PlantStats (com.infinityraider.agricraft.farming.PlantStats)4 List (java.util.List)4 TileEntity (net.minecraft.tileentity.TileEntity)4 ItemStack (net.minecraft.item.ItemStack)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 IAgriCrossStrategy (com.infinityraider.agricraft.api.v1.mutation.IAgriCrossStrategy)2 AgriCraftConfig (com.infinityraider.agricraft.reference.AgriCraftConfig)2 WorldHelper (com.infinityraider.infinitylib.utility.WorldHelper)2 Random (java.util.Random)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 Side (net.minecraftforge.fml.relauncher.Side)2 AgriCore (com.agricraft.agricore.core.AgriCore)1 IAgriAdapter (com.infinityraider.agricraft.api.v1.adapter.IAgriAdapter)1 IAgriFertilizer (com.infinityraider.agricraft.api.v1.fertilizer.IAgriFertilizer)1