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;
}
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));
}
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();
}
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));
}
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;
}
Aggregations