use of net.minecraft.block.FarmlandBlock in project BluePower by Qmunity.
the class ItemCropSeed method useOn.
@Override
public ActionResultType useOn(ItemUseContext itemUseContext) {
PlayerEntity player = itemUseContext.getPlayer();
Hand hand = itemUseContext.getHand();
Direction facing = itemUseContext.getClickedFace();
World world = itemUseContext.getLevel();
BlockPos pos = itemUseContext.getClickedPos();
ItemStack itemStack = player.getItemInHand(hand);
if (facing.ordinal() != 1) {
return ActionResultType.PASS;
} else if (player.mayUseItemAt(pos, facing, itemStack) && player.mayUseItemAt(pos.above(), facing, itemStack)) {
if (world.getBlockState(pos).getBlock().canSustainPlant(world.getBlockState(pos), world, pos, Direction.UP, this) && world.isEmptyBlock(pos.above()) && world.getBlockState(pos).getBlock() instanceof FarmlandBlock) {
world.setBlock(pos.above(), field_150925_a.defaultBlockState(), 2);
itemStack.setCount(itemStack.getCount() - 1);
player.setItemInHand(hand, itemStack);
return ActionResultType.SUCCESS;
} else {
return ActionResultType.PASS;
}
} else {
return ActionResultType.PASS;
}
}
use of net.minecraft.block.FarmlandBlock in project AgriCraft by AgriCraft.
the class TileEntitySprinkler method irrigateColumn.
/**
* This method will search through a vertical column of positions, starting from the top. It
* will stop searching any lower once it hits anything other than air or plants. Any plant found
* has an independent chance for a growth tick. That percentage is controlled by
* AgriCraftConfig. Farmland also ends the search, but it first has its moisture set to max (7)
* if it isn't already. The lowest position is special: a plant this far away is not helped.
* Only farmland is currently.
*/
@SuppressWarnings("deprecation")
protected void irrigateColumn(final int targetX, final int targetZ, final int highestY, final int lowestY) {
if (this.getWorld() == null || !(this.getWorld() instanceof ServerWorld)) {
return;
}
for (int targetY = highestY; targetY >= lowestY; targetY -= 1) {
BlockPos target = new BlockPos(targetX, targetY, targetZ);
BlockState state = this.getWorld().getBlockState(target);
Block block = state.getBlock();
// Option A: Skip empty/air blocks.
if (block.isAir(state, this.getWorld(), target)) {
continue;
}
// Option B: Give plants a chance to grow, and then continue onward to irrigate the farmland too.
if ((block instanceof IPlantable || block instanceof IGrowable) && targetY != lowestY) {
if (this.getRandom().nextDouble() < AgriCraft.instance.getConfig().sprinkleGrowthChance()) {
block.randomTick(state, (ServerWorld) this.getWorld(), target, this.getRandom());
}
continue;
}
// Option C: Dry farmland gets set as moist.
if (block instanceof FarmlandBlock) {
if (state.get(FarmlandBlock.MOISTURE) < 7) {
this.getWorld().setBlockState(target, state.with(FarmlandBlock.MOISTURE, 7), 2);
}
// Explicitly expresses the intent to stop.
break;
}
// Option D: If it's none of the above, it blocks the sprinkler's irrigation. Stop.
break;
}
}
Aggregations