Search in sources :

Example 6 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 = ItemStack.EMPTY;
    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(AgriSeed::toStack).orElse(ItemStack.EMPTY);
                if (notMergedStack.getItem() != locked.getItem() || notMergedStack.getItemDamage() != locked.getItemDamage()) {
                    return ItemStack.EMPTY;
                }
            }
            if (this.addSeedToStorage(notMergedStack)) {
                notMergedStack.setCount(0);
            } else {
                return ItemStack.EMPTY;
            }
        }
        if (notMergedStack.getCount() == 0) {
            slot.putStack(ItemStack.EMPTY);
        } else {
            slot.onSlotChanged();
        }
        if (notMergedStack.getCount() == originalStackInSlot.getCount()) {
            return ItemStack.EMPTY;
        }
        slot.onTake(player, notMergedStack);
    }
    return originalStackInSlot;
}
Also used : 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 7 with AgriSeed

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

the class SpreadStrategy method executeStrategy.

@Override
@Nonnull
public Optional<AgriSeed> executeStrategy(@Nonnull IAgriCrop crop, @Nonnull Random rand) {
    List<IAgriCrop> allNeighbours = WorldHelper.getTileNeighbors(crop.getCropWorld(), crop.getCropPos(), IAgriCrop.class);
    List<IAgriCrop> matureNeighbours = new ArrayList<>(allNeighbours);
    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(), allNeighbours)).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) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) ArrayList(java.util.ArrayList) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) Nonnull(javax.annotation.Nonnull)

Example 8 with AgriSeed

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

Example 9 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(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
    // If in creative or remote, skip.
    if (world.isRemote) {
        return EnumActionResult.PASS;
    }
    // Get the item & seed.
    final ItemStack stack = player.getHeldItem(hand);
    final AgriSeed seed = AgriApi.getSeedRegistry().valueOf(stack).orElse(null);
    // If seed is missing, error and pass.
    if (seed == null) {
        AgriCore.getLogger("agricraft").info("Unable to resolve an ItemClipping to an instance of an AgriSeed!");
        return EnumActionResult.PASS;
    }
    // Look for a crop instance at the given location.
    final IAgriCrop crop = WorldHelper.getTile(world, pos, IAgriCrop.class).orElse(null);
    // If the crop is missing, does not accept the given seed, or is not fertile for the seed, pass.
    if (crop == null || !crop.acceptsSeed(seed) || !crop.isFertile(seed)) {
        return EnumActionResult.PASS;
    }
    // Destroy the seed if needed.
    if (world.rand.nextInt(10) > seed.getStat().getStrength()) {
        // Message the player as to explain.
        MessageUtil.messagePlayer(player, "`7The clipping did not take...`r");
        // Decrease the stack size.
        StackHelper.decreaseStackSize(player, stack, 1);
        // Return that the action was a success (or moreso a failure...).
        return EnumActionResult.FAIL;
    }
    // Return that nothing happened.
    return EnumActionResult.PASS;
}
Also used : IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) AgriSeed(com.infinityraider.agricraft.api.v1.seed.AgriSeed) ItemStack(net.minecraft.item.ItemStack)

Example 10 with AgriSeed

use of com.infinityraider.agricraft.api.v1.seed.AgriSeed 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(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitx, float hity, float hitz) {
    ItemStack stack = player.getHeldItem(hand);
    TileEntity te = world.getTileEntity(pos);
    if (te instanceof IAgriCrop) {
        IAgriCrop crop = (IAgriCrop) te;
        Optional<AgriSeed> seed = AgriApi.getSeedRegistry().valueOf(stack);
        if (crop.isCrossCrop()) {
            // Cross-crops cannot hold seeds, so the trowel cannot extract from or insert into them.
            return EnumActionResult.FAIL;
        } else 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) ItemStack(net.minecraft.item.ItemStack)

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