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