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